@@ -77,7 +77,7 @@ public void run() {
77
77
long now = System .currentTimeMillis ();
78
78
if ((now - state .getLastActivity ()) > state .getConnection ().getHeartbeat () * 1000 * 2 ) {
79
79
try {
80
- state . getConnection (). handleHeartbeatFailure ();
80
+ handleHeartbeatFailure (state );
81
81
} catch (Exception e ) {
82
82
LOGGER .warn ("Error after heartbeat failure of connection {}" , state .getConnection ());
83
83
} finally {
@@ -267,33 +267,50 @@ protected void handleIoError(SocketChannelFrameHandlerState state, Throwable ex)
267
267
}
268
268
}
269
269
270
+ protected void handleHeartbeatFailure (SocketChannelFrameHandlerState state ) {
271
+ if (needToDispatchIoError (state )) {
272
+ dispatchShutdownToConnection (
273
+ () -> state .getConnection ().handleHeartbeatFailure (),
274
+ state .getConnection ().toString ()
275
+ );
276
+ } else {
277
+ try {
278
+ state .close ();
279
+ } catch (IOException e ) {
280
+
281
+ }
282
+ }
283
+ }
284
+
270
285
protected boolean needToDispatchIoError (final SocketChannelFrameHandlerState state ) {
271
286
return state .getConnection ().isOpen ();
272
287
}
273
288
274
289
protected void dispatchIoErrorToConnection (final SocketChannelFrameHandlerState state , final Throwable ex ) {
275
- // In case of recovery after the shutdown,
276
- // the new connection shouldn't be initialized in
277
- // the NIO thread, to avoid a deadlock.
278
- Runnable shutdown = () -> state .getConnection ().handleIoError (ex );
279
- if (executorService () == null ) {
280
- String name = "rabbitmq-connection-shutdown-" + state .getConnection ();
281
- Thread shutdownThread = Environment .newThread (threadFactory (), shutdown , name );
282
- shutdownThread .start ();
283
- } else {
284
- executorService ().submit (shutdown );
285
- }
290
+ dispatchShutdownToConnection (
291
+ () -> state .getConnection ().handleIoError (ex ),
292
+ state .getConnection ().toString ()
293
+ );
286
294
}
287
295
288
296
protected void dispatchShutdownToConnection (final SocketChannelFrameHandlerState state ) {
289
- Runnable shutdown = () -> state .getConnection ().doFinalShutdown ();
297
+ dispatchShutdownToConnection (
298
+ () -> state .getConnection ().doFinalShutdown (),
299
+ state .getConnection ().toString ()
300
+ );
301
+ }
302
+
303
+ protected void dispatchShutdownToConnection (Runnable connectionShutdownRunnable , String connectionName ) {
304
+ // In case of recovery after the shutdown,
305
+ // the new connection shouldn't be initialized in
306
+ // the NIO thread, to avoid a deadlock.
290
307
if (this .connectionShutdownExecutor != null ) {
291
- connectionShutdownExecutor .execute (shutdown );
308
+ connectionShutdownExecutor .execute (connectionShutdownRunnable );
292
309
} else if (executorService () != null ) {
293
- executorService ().execute (shutdown );
310
+ executorService ().execute (connectionShutdownRunnable );
294
311
} else {
295
- String name = "rabbitmq-connection-shutdown-" + state . getConnection () ;
296
- Thread shutdownThread = Environment .newThread (threadFactory (), shutdown , name );
312
+ String name = "rabbitmq-connection-shutdown-" + connectionName ;
313
+ Thread shutdownThread = Environment .newThread (threadFactory (), connectionShutdownRunnable , name );
297
314
shutdownThread .start ();
298
315
}
299
316
}
0 commit comments