Refactor and polish spring-messaging
Remove base class for STOMP-related message handler classes (AbstractSimpMessageHandler), polish subclasses and fix issues with more significant updates to STOMP broker relay. Introduce base class for SubscribableChannel implementations providing consistent logging for all channel implementations.
This commit is contained in:
@@ -25,7 +25,6 @@ import org.junit.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessageType;
|
||||
import org.springframework.messaging.simp.handler.DefaultSubscriptionRegistry;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
@@ -49,30 +48,30 @@ public class DefaultSubscriptionRegistryTests {
|
||||
|
||||
|
||||
@Test
|
||||
public void addSubscriptionInvalidInput() {
|
||||
public void registerSubscriptionInvalidInput() {
|
||||
|
||||
String sessId = "sess01";
|
||||
String subsId = "subs01";
|
||||
String dest = "/foo";
|
||||
|
||||
this.registry.addSubscription(subscribeMessage(null, subsId, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(null, subsId, dest));
|
||||
assertEquals(0, this.registry.findSubscriptions(message(dest)).size());
|
||||
|
||||
this.registry.addSubscription(subscribeMessage(sessId, null, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, null, dest));
|
||||
assertEquals(0, this.registry.findSubscriptions(message(dest)).size());
|
||||
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, null));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, null));
|
||||
assertEquals(0, this.registry.findSubscriptions(message(dest)).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSubscription() {
|
||||
public void registerSubscription() {
|
||||
|
||||
String sessId = "sess01";
|
||||
String subsId = "subs01";
|
||||
String dest = "/foo";
|
||||
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message(dest));
|
||||
|
||||
assertEquals("Expected one element " + actual, 1, actual.size());
|
||||
@@ -80,14 +79,14 @@ public class DefaultSubscriptionRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSubscriptionOneSession() {
|
||||
public void registerSubscriptionOneSession() {
|
||||
|
||||
String sessId = "sess01";
|
||||
List<String> subscriptionIds = Arrays.asList("subs01", "subs02", "subs03");
|
||||
String dest = "/foo";
|
||||
|
||||
for (String subId : subscriptionIds) {
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subId, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subId, dest));
|
||||
}
|
||||
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message(dest));
|
||||
@@ -97,7 +96,7 @@ public class DefaultSubscriptionRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSubscriptionMultipleSessions() {
|
||||
public void registerSubscriptionMultipleSessions() {
|
||||
|
||||
List<String> sessIds = Arrays.asList("sess01", "sess02", "sess03");
|
||||
List<String> subscriptionIds = Arrays.asList("subs01", "subs02", "subs03");
|
||||
@@ -105,7 +104,7 @@ public class DefaultSubscriptionRegistryTests {
|
||||
|
||||
for (String sessId : sessIds) {
|
||||
for (String subsId : subscriptionIds) {
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,14 +117,14 @@ public class DefaultSubscriptionRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSubscriptionWithDestinationPattern() {
|
||||
public void registerSubscriptionWithDestinationPattern() {
|
||||
|
||||
String sessId = "sess01";
|
||||
String subsId = "subs01";
|
||||
String destPattern = "/topic/PRICE.STOCK.*.IBM";
|
||||
String dest = "/topic/PRICE.STOCK.NASDAQ.IBM";
|
||||
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, destPattern));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, destPattern));
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message(dest));
|
||||
|
||||
assertEquals("Expected one element " + actual, 1, actual.size());
|
||||
@@ -133,13 +132,13 @@ public class DefaultSubscriptionRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addSubscriptionWithDestinationPatternRegex() {
|
||||
public void registerSubscriptionWithDestinationPatternRegex() {
|
||||
|
||||
String sessId = "sess01";
|
||||
String subsId = "subs01";
|
||||
String destPattern = "/topic/PRICE.STOCK.*.{ticker:(IBM|MSFT)}";
|
||||
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, destPattern));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, destPattern));
|
||||
Message<?> message = message("/topic/PRICE.STOCK.NASDAQ.IBM");
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message);
|
||||
|
||||
@@ -159,7 +158,7 @@ public class DefaultSubscriptionRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeSubscription() {
|
||||
public void unregisterSubscription() {
|
||||
|
||||
List<String> sessIds = Arrays.asList("sess01", "sess02", "sess03");
|
||||
List<String> subscriptionIds = Arrays.asList("subs01", "subs02", "subs03");
|
||||
@@ -167,13 +166,13 @@ public class DefaultSubscriptionRegistryTests {
|
||||
|
||||
for (String sessId : sessIds) {
|
||||
for (String subsId : subscriptionIds) {
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
}
|
||||
}
|
||||
|
||||
this.registry.removeSubscription(unsubscribeMessage(sessIds.get(0), subscriptionIds.get(0)));
|
||||
this.registry.removeSubscription(unsubscribeMessage(sessIds.get(0), subscriptionIds.get(1)));
|
||||
this.registry.removeSubscription(unsubscribeMessage(sessIds.get(0), subscriptionIds.get(2)));
|
||||
this.registry.unregisterSubscription(unsubscribeMessage(sessIds.get(0), subscriptionIds.get(0)));
|
||||
this.registry.unregisterSubscription(unsubscribeMessage(sessIds.get(0), subscriptionIds.get(1)));
|
||||
this.registry.unregisterSubscription(unsubscribeMessage(sessIds.get(0), subscriptionIds.get(2)));
|
||||
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message(dest));
|
||||
|
||||
@@ -183,7 +182,7 @@ public class DefaultSubscriptionRegistryTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeSessionSubscriptions() {
|
||||
public void unregisterAllSubscriptions() {
|
||||
|
||||
List<String> sessIds = Arrays.asList("sess01", "sess02", "sess03");
|
||||
List<String> subscriptionIds = Arrays.asList("subs01", "subs02", "subs03");
|
||||
@@ -191,12 +190,12 @@ public class DefaultSubscriptionRegistryTests {
|
||||
|
||||
for (String sessId : sessIds) {
|
||||
for (String subsId : subscriptionIds) {
|
||||
this.registry.addSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
this.registry.registerSubscription(subscribeMessage(sessId, subsId, dest));
|
||||
}
|
||||
}
|
||||
|
||||
this.registry.removeSessionSubscriptions(sessIds.get(0));
|
||||
this.registry.removeSessionSubscriptions(sessIds.get(1));
|
||||
this.registry.unregisterAllSubscriptions(sessIds.get(0));
|
||||
this.registry.unregisterAllSubscriptions(sessIds.get(1));
|
||||
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message(dest));
|
||||
|
||||
@@ -204,6 +203,12 @@ public class DefaultSubscriptionRegistryTests {
|
||||
assertEquals(subscriptionIds, sort(actual.get(sessIds.get(2))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unregisterAllSubscriptionsNoMatch() {
|
||||
this.registry.unregisterAllSubscriptions("bogus");
|
||||
// no exceptions
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findSubscriptionsNoMatches() {
|
||||
MultiValueMap<String, String> actual = this.registry.findSubscriptions(message("/foo"));
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.messaging.simp.handler;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -30,7 +28,6 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessageType;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
||||
@@ -57,26 +54,19 @@ public class SimpleBrokerWebMessageHandlerTests {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getSupportedMessageTypes() {
|
||||
assertEquals(Arrays.asList(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE,
|
||||
SimpMessageType.UNSUBSCRIBE, SimpMessageType.DISCONNECT),
|
||||
this.messageHandler.getSupportedMessageTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subcribePublish() {
|
||||
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage("sess1", "sub1", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage("sess1", "sub2", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage("sess1", "sub3", "/bar"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage("sess1", "sub1", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage("sess1", "sub2", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage("sess1", "sub3", "/bar"));
|
||||
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage("sess2", "sub1", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage("sess2", "sub2", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage("sess2", "sub3", "/bar"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage("sess2", "sub1", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage("sess2", "sub2", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage("sess2", "sub3", "/bar"));
|
||||
|
||||
this.messageHandler.handlePublish(createMessage("/foo", "message1"));
|
||||
this.messageHandler.handlePublish(createMessage("/bar", "message2"));
|
||||
this.messageHandler.handleMessage(createMessage("/foo", "message1"));
|
||||
this.messageHandler.handleMessage(createMessage("/bar", "message2"));
|
||||
|
||||
verify(this.clientChannel, times(6)).send(this.messageCaptor.capture());
|
||||
assertCapturedMessage("sess1", "sub1", "/foo");
|
||||
@@ -93,21 +83,21 @@ public class SimpleBrokerWebMessageHandlerTests {
|
||||
String sess1 = "sess1";
|
||||
String sess2 = "sess2";
|
||||
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage(sess1, "sub1", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage(sess1, "sub2", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage(sess1, "sub3", "/bar"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub1", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub2", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage(sess1, "sub3", "/bar"));
|
||||
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage(sess2, "sub1", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage(sess2, "sub2", "/foo"));
|
||||
this.messageHandler.handleSubscribe(createSubscriptionMessage(sess2, "sub3", "/bar"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub1", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub2", "/foo"));
|
||||
this.messageHandler.handleMessage(createSubscriptionMessage(sess2, "sub3", "/bar"));
|
||||
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.DISCONNECT);
|
||||
headers.setSessionId(sess1);
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build();
|
||||
this.messageHandler.handleDisconnect(message);
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
this.messageHandler.handlePublish(createMessage("/foo", "message1"));
|
||||
this.messageHandler.handlePublish(createMessage("/bar", "message2"));
|
||||
this.messageHandler.handleMessage(createMessage("/foo", "message1"));
|
||||
this.messageHandler.handleMessage(createMessage("/bar", "message2"));
|
||||
|
||||
verify(this.clientChannel, times(3)).send(this.messageCaptor.capture());
|
||||
assertCapturedMessage(sess2, "sub1", "/foo");
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.messaging.support.channel;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -26,18 +24,19 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.messaging.support.channel.PublishSubscribeChannel;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests for {@link PublishSubscribeChannel}.
|
||||
* Tests for {@link TaskExecutorSubscribableChannel}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@@ -47,7 +46,7 @@ public class PublishSubscibeChannelTests {
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
private PublishSubscribeChannel channel = new PublishSubscribeChannel();
|
||||
private TaskExecutorSubscribableChannel channel = new TaskExecutorSubscribableChannel();
|
||||
|
||||
@Mock
|
||||
private MessageHandler handler;
|
||||
@@ -89,8 +88,8 @@ public class PublishSubscibeChannelTests {
|
||||
|
||||
@Test
|
||||
public void sendWithExecutor() throws Exception {
|
||||
Executor executor = mock(Executor.class);
|
||||
this.channel = new PublishSubscribeChannel(executor);
|
||||
TaskExecutor executor = mock(TaskExecutor.class);
|
||||
this.channel = new TaskExecutorSubscribableChannel(executor);
|
||||
this.channel.subscribe(this.handler);
|
||||
this.channel.send(this.message);
|
||||
verify(executor).execute(this.runnableCaptor.capture());
|
||||
|
||||
Reference in New Issue
Block a user