View Javadoc

1   /**
2    *Copyright [2009-2010] [dennis zhuang(killme2008@gmail.com)]
3    *Licensed under the Apache License, Version 2.0 (the "License");
4    *you may not use this file except in compliance with the License. 
5    *You may obtain a copy of the License at 
6    *             http://www.apache.org/licenses/LICENSE-2.0 
7    *Unless required by applicable law or agreed to in writing, 
8    *software distributed under the License is distributed on an "AS IS" BASIS, 
9    *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
10   *either express or implied. See the License for the specific language governing permissions and limitations under the License
11   */
12  /**
13   *Copyright [2009-2010] [dennis zhuang(killme2008@gmail.com)]
14   *Licensed under the Apache License, Version 2.0 (the "License");
15   *you may not use this file except in compliance with the License.
16   *You may obtain a copy of the License at
17   *             http://www.apache.org/licenses/LICENSE-2.0
18   *Unless required by applicable law or agreed to in writing,
19   *software distributed under the License is distributed on an "AS IS" BASIS,
20   *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
21   *either express or implied. See the License for the specific language governing permissions and limitations under the License
22   */
23  package net.rubyeye.xmemcached.impl;
24  
25  import java.util.concurrent.Delayed;
26  import java.util.concurrent.TimeUnit;
27  
28  import net.rubyeye.xmemcached.utils.InetSocketAddressWrapper;
29  
30  
31  /**
32   * A auto reconnect request,associating a socket address for reconnecting
33   * 
34   * @author dennis
35   * 
36   */
37  public final class ReconnectRequest implements Delayed {
38  
39      private InetSocketAddressWrapper inetSocketAddressWrapper;
40      private int tries;
41  
42      private static final long MIN_RECONNECT_INTERVAL = 1000;
43  
44      private static final long MAX_RECONNECT_INTERVAL = 60 * 1000;
45  
46      private volatile long nextReconnectTimestamp;
47  
48  
49      public ReconnectRequest(InetSocketAddressWrapper inetSocketAddressWrapper, int tries, long reconnectInterval) {
50          super();
51          this.setInetSocketAddressWrapper(inetSocketAddressWrapper);
52          this.setTries(tries); // record reconnect times
53          reconnectInterval = this.normalInterval(reconnectInterval);
54          this.nextReconnectTimestamp = System.currentTimeMillis() + reconnectInterval;
55      }
56  
57  
58      private long normalInterval(long reconnectInterval) {
59          if (reconnectInterval < MIN_RECONNECT_INTERVAL) {
60              reconnectInterval = MIN_RECONNECT_INTERVAL;
61          }
62          if (reconnectInterval > MAX_RECONNECT_INTERVAL) {
63              reconnectInterval = MAX_RECONNECT_INTERVAL;
64          }
65          return reconnectInterval;
66      }
67  
68  
69      public long getDelay(TimeUnit unit) {
70          return unit.convert(this.nextReconnectTimestamp - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
71      }
72  
73  
74      public int compareTo(Delayed o) {
75          ReconnectRequest other = (ReconnectRequest) o;
76          if (this.nextReconnectTimestamp > other.nextReconnectTimestamp) {
77              return 1;
78          }
79          else {
80              return -1;
81          }
82      }
83  
84  
85      /**
86       * Returns a reconnect socket address wrapper
87       * 
88       * @see InetSocketAddressWrapper
89       * @return
90       */
91      public final InetSocketAddressWrapper getInetSocketAddressWrapper() {
92          return this.inetSocketAddressWrapper;
93      }
94  
95  
96      public void updateNextReconnectTimeStamp(long interval) {
97          interval = this.normalInterval(interval);
98          this.nextReconnectTimestamp = System.currentTimeMillis() + interval;
99      }
100 
101 
102     public final void setInetSocketAddressWrapper(InetSocketAddressWrapper inetSocketAddressWrapper) {
103         this.inetSocketAddressWrapper = inetSocketAddressWrapper;
104     }
105 
106 
107     public final void setTries(int tries) {
108         this.tries = tries;
109     }
110 
111 
112     /**
113      * Returns retry times
114      * 
115      * @return retry times,it is zero if it does not retry to connect
116      */
117     public final int getTries() {
118         return this.tries;
119     }
120 
121 }