diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java index 9be712ba47..edc6ccfcad 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java @@ -166,6 +166,9 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler @Override protected void stopInternal() { + for (StompRelaySession session: this.relaySessions.values()) { + session.disconnect(); + } try { this.tcpClient.close().await(); } @@ -268,6 +271,10 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler }); } + public void disconnect() { + this.stompConnection.setDisconnected(); + } + protected Composable, Message>> initConnection() { return tcpClient.open(); } @@ -315,7 +322,6 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler protected void connected(StompHeaderAccessor headers, StompConnection stompConnection) { this.stompConnection.setReady(); - publishBrokerAvailableEvent(); } protected void handleTcpClientFailure(String message, Throwable ex) { @@ -328,7 +334,6 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler protected void disconnected(String errorMessage) { this.stompConnection.setDisconnected(); sendError(errorMessage); - publishBrokerUnavailableEvent(); } private void sendError(String errorText) { @@ -535,6 +540,13 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler } super.connected(headers, stompConnection); + publishBrokerAvailableEvent(); + } + + @Override + protected void disconnected(String errorMessage) { + super.disconnected(errorMessage); + publishBrokerUnavailableEvent(); } @Override diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java index 8f0a9fa747..2740d2cbdd 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java @@ -78,10 +78,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests { this.eventPublisher = new ExpectationMatchingEventPublisher(); - this.relay = new StompBrokerRelayMessageHandler(this.responseChannel, Arrays.asList("/queue/", "/topic/")); - this.relay.setRelayPort(port); - this.relay.setApplicationEventPublisher(this.eventPublisher); - this.relay.start(); + createAndStartRelay(); } private void createAndStartBroker() throws Exception { @@ -92,6 +89,16 @@ public class StompBrokerRelayMessageHandlerIntegrationTests { this.activeMQBroker.start(); } + private void createAndStartRelay() throws InterruptedException { + this.relay = new StompBrokerRelayMessageHandler(this.responseChannel, Arrays.asList("/queue/", "/topic/")); + this.relay.setRelayPort(port); + this.relay.setApplicationEventPublisher(this.eventPublisher); + + this.eventPublisher.expect(true); + this.relay.start(); + this.eventPublisher.awaitAndAssert(); + } + @After public void tearDown() throws Exception { try { @@ -146,8 +153,9 @@ public class StompBrokerRelayMessageHandlerIntegrationTests { @Test(expected=MessageDeliveryException.class) public void messageDeliverExceptionIfSystemSessionForwardFails() throws Exception { + stopBrokerAndAwait(); StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND); - this.relay.handleMessage(MessageBuilder.withPayloadAndHeaders("test", headers).build()); + this.relay.handleMessage(MessageBuilder.withPayloadAndHeaders("test".getBytes(), headers).build()); } @Test @@ -169,11 +177,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests { } @Test - public void brokerAvailabilityEvents() throws Exception { - - this.eventPublisher.expect(true); - this.eventPublisher.awaitAndAssert(); - + public void brokerAvailabilityEventWhenStopped() throws Exception { this.eventPublisher.expect(false); stopBrokerAndAwait(); this.eventPublisher.awaitAndAssert(); @@ -203,7 +207,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests { this.responseHandler.awaitAndAssert(); - this.eventPublisher.expect(true, false); + this.eventPublisher.expect(false); this.eventPublisher.awaitAndAssert(); this.eventPublisher.expect(true);