08c27a7d01f14b347ac5c00ff20598c5496ca137
[msb/apigateway.git] /
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package org.onap.msb.apiroute.wrapper.consulextend.expose;
17
18 import java.math.BigInteger;
19 import java.util.concurrent.atomic.AtomicReference;
20
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.orbitz.consul.model.ConsulResponse;
25
26 public class ConsulIndexFilter<T> implements WatchTask.Filter<T> {
27
28         private final static Logger LOGGER = LoggerFactory
29                         .getLogger(ConsulIndexFilter.class);
30
31         private final AtomicReference<BigInteger> latestIndex = new AtomicReference<BigInteger>(
32                         null);
33
34         @Override
35         public boolean filter(final ConsulResponse<T> object) {
36                 // TODO Auto-generated method stub
37                 return isChanged(object);
38         }
39
40         private boolean isChanged(final ConsulResponse<T> consulResponse) {
41
42                 if (consulResponse != null && consulResponse.getIndex() != null
43                                 && !consulResponse.getIndex().equals(latestIndex.get())) {
44                         
45                         if(LOGGER.isDebugEnabled()){
46                                 //第一次不打印
47                                 if (latestIndex.get()!=null) {
48                                         LOGGER.debug("consul index compare:new-"
49                                                         + consulResponse.getIndex() + "  old-"
50                                                         + latestIndex.get());
51                                 }
52                                 
53                         }
54
55                         this.latestIndex.set(consulResponse.getIndex());
56                         return true;
57                 }
58
59                 return false;
60         }
61         
62         
63 }