@ -30,6 +30,11 @@ import org.springframework.messaging.Message;
@@ -30,6 +30,11 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel ;
import org.springframework.messaging.MessageHandler ;
import org.springframework.messaging.SubscribableChannel ;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor ;
import org.springframework.messaging.simp.SimpMessageType ;
import org.springframework.messaging.support.AbstractMessageChannel ;
import org.springframework.messaging.support.ChannelInterceptor ;
import org.springframework.messaging.support.ChannelInterceptorAdapter ;
import org.springframework.util.Assert ;
import org.springframework.util.CollectionUtils ;
@ -69,6 +74,8 @@ public abstract class AbstractBrokerMessageHandler
@@ -69,6 +74,8 @@ public abstract class AbstractBrokerMessageHandler
private final Object lifecycleMonitor = new Object ( ) ;
private ChannelInterceptor unsentDisconnectInterceptor = new UnsentDisconnectChannelInterceptor ( ) ;
/ * *
* Constructor with no destination prefixes ( matches all destinations ) .
@ -165,6 +172,9 @@ public abstract class AbstractBrokerMessageHandler
@@ -165,6 +172,9 @@ public abstract class AbstractBrokerMessageHandler
}
this . clientInboundChannel . subscribe ( this ) ;
this . brokerChannel . subscribe ( this ) ;
if ( this . clientInboundChannel instanceof AbstractMessageChannel ) {
( ( AbstractMessageChannel ) this . clientInboundChannel ) . addInterceptor ( 0 , this . unsentDisconnectInterceptor ) ;
}
startInternal ( ) ;
this . running = true ;
if ( logger . isInfoEnabled ( ) ) {
@ -185,6 +195,9 @@ public abstract class AbstractBrokerMessageHandler
@@ -185,6 +195,9 @@ public abstract class AbstractBrokerMessageHandler
stopInternal ( ) ;
this . clientInboundChannel . unsubscribe ( this ) ;
this . brokerChannel . unsubscribe ( this ) ;
if ( this . clientInboundChannel instanceof AbstractMessageChannel ) {
( ( AbstractMessageChannel ) this . clientInboundChannel ) . removeInterceptor ( this . unsentDisconnectInterceptor ) ;
}
this . running = false ;
if ( logger . isDebugEnabled ( ) ) {
logger . info ( "Stopped." ) ;
@ -264,4 +277,21 @@ public abstract class AbstractBrokerMessageHandler
@@ -264,4 +277,21 @@ public abstract class AbstractBrokerMessageHandler
}
}
/ * *
* Detect unsent DISCONNECT messages and process them anyway .
* /
private class UnsentDisconnectChannelInterceptor extends ChannelInterceptorAdapter {
@Override
public void afterSendCompletion ( Message < ? > message , MessageChannel channel , boolean sent , Exception ex ) {
if ( ! sent ) {
SimpMessageType messageType = SimpMessageHeaderAccessor . getMessageType ( message . getHeaders ( ) ) ;
if ( SimpMessageType . DISCONNECT . equals ( messageType ) ) {
logger . debug ( "Detected unsent DISCONNECT message. Processing anyway." ) ;
handleMessage ( message ) ;
}
}
}
}
}