@@ -154,24 +154,18 @@ else if(batchSize != -1 && throttle != -1 || batchSize != -1 && secondThrottle !
154
154
155
155
@ Override
156
156
protected boolean doReconnect (Site site ) {
157
- synchronized ( this ) {
158
- log . info ( "ThreadedClient doReconnect: " + site . host + ":" + site . port );
157
+ if (! AbstractClient . ifUseBackupSite ) {
158
+ // not enable backupSite, use original logic
159
159
String topicStr = site .host + ":" + site .port + "/" + site .tableName + "/" + site .actionName ;
160
160
Thread handlerLopper = null ;
161
- if (! handlerLoppers . containsKey ( topicStr ) ) {
162
- if (!AbstractClient . ifUseBackupSite ) {
161
+ synchronized ( handlerLoppers ) {
162
+ if (!handlerLoppers . containsKey ( topicStr ))
163
163
throw new RuntimeException ("Subscribe thread is not started" );
164
- }
165
- } else {
166
164
handlerLopper = handlerLoppers .get (topicStr );
167
- handlerLopper .interrupt ();
168
165
}
169
-
166
+ handlerLopper . interrupt ();
170
167
try {
171
- // System.out.println("doReconnect 尝试切换节点:" + site.host + ":" + site.port);
172
- // System.out.println("site msg id: " +site.msgId);
173
- subscribe (site .host , site .port , site .tableName , site .actionName , site .handler , site .msgId + 1 , true , site .filter , site .deserializer , site .allowExistTopic , site .userName , site .passWord , false );
174
- // System.out.println("doReconnect 尝试切换节点成功:" + site.host + ":" + site.port);
168
+ subscribe (site .host , site .port , site .tableName , site .actionName , site .handler , site .msgId + 1 , true , site .filter , site .deserializer , site .allowExistTopic , site .userName , site .passWord );
175
169
Date d = new Date ();
176
170
DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
177
171
log .info (df .format (d ) + " Successfully reconnected and subscribed " + site .host + ":" + site .port + "/" + site .tableName + "/" + site .actionName );
@@ -183,6 +177,29 @@ protected boolean doReconnect(Site site) {
183
177
ex .printStackTrace ();
184
178
return false ;
185
179
}
180
+ } else {
181
+ // enable backupSite, try to switch site and subscribe.
182
+ synchronized (this ) {
183
+ log .info ("ThreadedClient doReconnect: " + site .host + ":" + site .port );
184
+ try {
185
+ System .out .println ("doReconnect 尝试切换节点:" + site .host + ":" + site .port );
186
+ // System.out.println("site msg id: " +site.msgId);
187
+ subscribe (site .host , site .port , site .tableName , site .actionName , site .handler , site .msgId + 1 , true , site .filter , site .deserializer , site .allowExistTopic , site .userName , site .passWord , false );
188
+ System .out .println ("doReconnect 尝试切换节点成功:" + site .host + ":" + site .port );
189
+ // System.out.println("切换后 handlerLoppers: " + handlerLoppers.get(topicStr).getName());
190
+ // System.out.println("切换成功后,handlerLoppers size: " + handlerLoppers.size());
191
+ Date d = new Date ();
192
+ DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
193
+ log .info (df .format (d ) + " Successfully reconnected and subscribed " + site .host + ":" + site .port + "/" + site .tableName + "/" + site .actionName );
194
+ return true ;
195
+ } catch (Exception ex ) {
196
+ Date d = new Date ();
197
+ DateFormat df = new SimpleDateFormat ("yyyy/MM/dd HH:mm:ss" );
198
+ log .error (df .format (d ) + " Unable to subscribe table. Will try again after 1 seconds." + site .host + ":" + site .port + "/" + site .tableName + "/" + site .actionName );
199
+ ex .printStackTrace ();
200
+ return false ;
201
+ }
202
+ }
186
203
}
187
204
}
188
205
@@ -198,16 +215,11 @@ public void subscribe(String host, int port, String tableName, String actionName
198
215
}
199
216
}
200
217
201
- public void subscribe (String host , int port , String tableName , String actionName , MessageHandler handler , long offset , boolean reconnect , Vector filter , StreamDeserializer deserializer , boolean allowExistTopic , String userName , String password , boolean createSubInfo ) throws IOException {
218
+ /**
219
+ * This internal subscribe method only use for when enable backupSite, try to switch site and subscribe.
220
+ */
221
+ protected void subscribe (String host , int port , String tableName , String actionName , MessageHandler handler , long offset , boolean reconnect , Vector filter , StreamDeserializer deserializer , boolean allowExistTopic , String userName , String password , boolean createSubInfo ) throws IOException {
202
222
BlockingQueue <List <IMessage >> queue = subscribeInternal (host , port , tableName , actionName , handler , offset , reconnect , filter , deserializer , allowExistTopic , userName , password , false , createSubInfo );
203
- HandlerLopper handlerLopper = new HandlerLopper (queue , handler );
204
- handlerLopper .start ();
205
- String topicStr = host + ":" + port + "/" + tableName + "/" + actionName ;
206
- List <String > usr = Arrays .asList (userName , password );
207
- synchronized (handlerLoppers ) {
208
- handlerLoppers .put (topicStr , handlerLopper );
209
- // users.put(topicStr, usr);
210
- }
211
223
}
212
224
213
225
public void subscribe (String host , int port , String tableName , String actionName , MessageHandler handler , long offset , boolean reconnect , Vector filter , StreamDeserializer deserializer , boolean allowExistTopic , int batchSize , int throttle , String userName , String password ) throws IOException {
0 commit comments