Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #17 from nickmbailey/master

Make reconnecting feature optional.
  • Loading branch information...
commit dad87474e7d2200506d9e01f9b3640f06566d87f 2 parents 804ded0 + df2bcc2
Bela Ban authored

Showing 1 changed file with 45 additions and 39 deletions. Show diff stats Hide diff stats

  1. 84  src/org/jgroups/client/StompConnection.java
84  src/org/jgroups/client/StompConnection.java
@@ -54,23 +54,26 @@
54 54
 
55 55
     protected String password;
56 56
 
  57
+    protected boolean reconnect;
  58
+
57 59
     protected final Log log=LogFactory.getLog(getClass());
58 60
 
59 61
     /**
60 62
      * @param dest IP address + ':' + port, e.g. "192.168.1.5:8787"
61 63
      */
62 64
     public StompConnection(String dest) {
63  
-        this(dest, null, null, false);
  65
+        this(dest, null, null, false, false);
64 66
     }
65 67
 
66  
-    public StompConnection(String dest, boolean ssl) {
67  
-        this(dest, null, null, ssl);
  68
+    public StompConnection(String dest, boolean reconnect, boolean ssl) {
  69
+        this(dest, null, null, reconnect, ssl);
68 70
     }
69 71
 
70  
-    public StompConnection(String dest, String userid, String password, boolean ssl) {
  72
+    public StompConnection(String dest, String userid, String password, boolean reconnect, boolean ssl) {;
71 73
         server_destinations.add(dest);
72 74
         this.userid = userid;
73 75
         this.password = password;
  76
+        this.reconnect = reconnect;
74 77
         if (ssl)
75 78
             socket_factory = SSLSocketFactory.getDefault();
76 79
         else
@@ -89,10 +92,6 @@ public void removeListener(Listener listener) {
89 92
             listeners.remove(listener);
90 93
     }
91 94
 
92  
-    public void connect() {
93  
-        startRunner();
94  
-    }
95  
-
96 95
     protected synchronized void startRunner() {
97 96
         if(runner == null || !runner.isAlive()) {
98 97
             running = true;
@@ -110,21 +109,16 @@ protected void sendConnect() {
110 109
             sb.append("passcode: ").append(password).append("\n");
111 110
         sb.append("\n");
112 111
 
113  
-        try{
  112
+        try {
114 113
             out.write(sb.toString().getBytes());
115 114
             out.write(STOMP.NULL_BYTE);
116 115
             out.flush();
117 116
         }
118 117
         catch(IOException ex) {
119  
-            log.error("failed to send connect message: " + ex);
  118
+            log.error("failed to send connect message:", ex);
120 119
         }
121 120
     }
122 121
 
123  
-    public void disconnect() {
124  
-        running=false;
125  
-        close();
126  
-    }
127  
-
128 122
     public void subscribe(String destination) {
129 123
         if(destination == null)
130 124
             return;
@@ -147,7 +141,7 @@ protected void sendSubscribe(String destination) {
147 141
             out.flush();
148 142
         }
149 143
         catch(IOException ex) {
150  
-            log.error("failed subscribing to " + destination + ": " + ex);
  144
+            log.error("failed subscribing to " + destination + ": ", ex);
151 145
         }
152 146
     }
153 147
 
@@ -173,14 +167,11 @@ protected void sendUnsubscribe(String destination) {
173 167
             out.flush();
174 168
         }
175 169
         catch(IOException ex) {
176  
-            log.error("failed unsubscribing from " + destination + ": " + ex);
  170
+            log.error("failed unsubscribing from " + destination + ": ", ex);
177 171
         }
178 172
     }
179 173
 
180 174
     public void send(String destination, byte[] buf, int offset, int length, String ... headers) {
181  
-        if(!isConnected())
182  
-            return;
183  
-
184 175
         StringBuilder sb=new StringBuilder();
185 176
         sb.append(STOMP.ClientVerb.SEND.name()).append("\n");
186 177
         if(destination != null)
@@ -200,8 +191,8 @@ public void send(String destination, byte[] buf, int offset, int length, String
200 191
             out.write(STOMP.NULL_BYTE);
201 192
             out.flush();
202 193
         }
203  
-        catch(IOException ex) {
204  
-            log.error("failed sending message to server: " + ex);
  194
+        catch (IOException e) {
  195
+            log.error("failed sending message to " + destination + ": ", e);
205 196
         }
206 197
     }
207 198
 
@@ -224,8 +215,17 @@ public void run() {
224 215
         int timeout = 1;
225 216
         while(running) {
226 217
             try {
227  
-                if(!isConnected()) {
228  
-                    setupConnection();
  218
+                if (!isConnected() && reconnect) {
  219
+                    log.error("Reconnecting in "+timeout+"s.");
  220
+                    try {
  221
+                        Thread.sleep(timeout * 1000);
  222
+                    }
  223
+                    catch (InterruptedException e1) {
  224
+                        // pass
  225
+                    }
  226
+                    timeout = timeout*2 > 60 ? 60 : timeout*2;
  227
+
  228
+                    connect();
229 229
                 }
230 230
 
231 231
                 // reset the connection backoff when we successfully connect.
@@ -269,15 +269,13 @@ public void run() {
269 269
                 }
270 270
             }
271 271
             catch(IOException e) {
272  
-                log.error("Connection closed unexpectedly, will attempt reconnect in "+timeout+"s.", e);
273  
-                close();
274  
-                try {
275  
-                    Thread.sleep(timeout * 1000);
  272
+                log.error("Connection closed unexpectedly:", e);
  273
+                if (reconnect) {
  274
+                    closeConnections();
276 275
                 }
277  
-                catch (InterruptedException e1) {
278  
-                    // pass
  276
+                else {
  277
+                    disconnect();
279 278
                 }
280  
-                timeout = timeout*2 > 60 ? 60 : timeout*2;
281 279
             }
282 280
             catch(Throwable t) {
283 281
                 log.error("failure reading frame", t);
@@ -307,7 +305,7 @@ protected void notifyListeners(Map<String,String> info) {
307 305
         }
308 306
     }
309 307
 
310  
-    protected void setupConnection() throws IOException{
  308
+    public void connect() throws IOException{
311 309
         for (String dest : server_destinations) {
312 310
             try {
313 311
                 connectToDestination(dest);
@@ -320,12 +318,19 @@ protected void setupConnection() throws IOException{
320 318
             }
321 319
             catch(IOException ex) {
322 320
                 if(log.isErrorEnabled())
323  
-                    log.error("failed connecting to " + dest);
324  
-                close();
  321
+                    log.error("failed connecting to " + dest, ex);
  322
+                closeConnections();
325 323
             }
326 324
         }
  325
+
327 326
         if(!isConnected())
328 327
             throw new IOException("no target server available");
  328
+
  329
+        startRunner();
  330
+    }
  331
+
  332
+    public void startReconnectingClient() {
  333
+        startRunner();
329 334
     }
330 335
 
331 336
     protected void connectToDestination(String dest) throws IOException {
@@ -340,7 +345,12 @@ protected void connectToDestination(String dest) throws IOException {
340 345
         out=new DataOutputStream(sock.getOutputStream());
341 346
     }
342 347
 
343  
-    protected void close() {
  348
+    public void disconnect() {
  349
+        running = false;
  350
+        closeConnections();
  351
+    }
  352
+
  353
+    protected void closeConnections() {
344 354
         Util.close(in);
345 355
         Util.close(out);
346 356
         Util.close(sock);
@@ -383,10 +393,6 @@ public void onInfo(Map<String, String> information) {
383 393
             }
384 394
         });
385 395
 
386  
-        if(!conn.isConnected()) {
387  
-            conn.setupConnection();
388  
-        }
389  
-
390 396
         conn.connect();
391 397
 
392 398
         while(conn.isConnected()) {

0 notes on commit dad8747

Please sign in to comment.
Something went wrong with that request. Please try again.