Skip to content

Commit e1a46bb

Browse files
committed
Add tests to spring-messaging
1 parent d202573 commit e1a46bb

13 files changed

+584
-123
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131

3232
/**
3333
* A {@link HandlerMethodReturnValueHandler} for replying directly to a subscription. It
34-
* supports methods annotated with {@link SubscribeEvent} that do not also annotated with
35-
* neither {@link ReplyTo} nor {@link ReplyToUser}.
36-
*
37-
* <p>The value returned from the method is converted, and turned to a {@link Message} and
34+
* supports methods annotated with {@link SubscribeEvent} unless they're also annotated
35+
* with {@link ReplyTo} or {@link ReplyToUser}.
36+
* <p>
37+
* The value returned from the method is converted, and turned to a {@link Message} and
3838
* then enriched with the sessionId, subscriptionId, and destination of the input message.
3939
* The message is then sent directly back to the connected client.
4040
*
@@ -73,8 +73,7 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
7373
String destination = inputHeaders.getDestination();
7474

7575
Assert.state(inputHeaders.getSubscriptionId() != null,
76-
"No subsriptiondId in input message. Add @ReplyTo or @ReplyToUser to method: "
77-
+ returnType.getMethod());
76+
"No subsriptiondId in input message to method " + returnType.getMethod());
7877

7978
MessagePostProcessor postProcessor = new SubscriptionHeaderPostProcessor(sessionId, subscriptionId);
8079
this.messagingTemplate.convertAndSend(destination, returnValue, postProcessor);

spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractStompEndpointRegistration.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030

3131

3232
/**
33-
* A helper class for configuring STOMP protocol handling over WebSocket
34-
* with optional SockJS fallback options.
33+
* An abstract base class class for configuring STOMP over WebSocket/SockJS endpoints.
3534
*
3635
* @author Rossen Stoyanchev
3736
* @since 4.0
@@ -46,33 +45,36 @@ public abstract class AbstractStompEndpointRegistration<M> implements StompEndpo
4645

4746
private StompSockJsServiceRegistration sockJsServiceRegistration;
4847

49-
private final TaskScheduler defaultSockJsTaskScheduler;
48+
private final TaskScheduler sockJsTaskScheduler;
5049

5150

5251
public AbstractStompEndpointRegistration(String[] paths, SubProtocolWebSocketHandler webSocketHandler,
53-
TaskScheduler defaultSockJsTaskScheduler) {
52+
TaskScheduler sockJsTaskScheduler) {
5453

5554
Assert.notEmpty(paths, "No paths specified");
5655
this.paths = paths;
5756
this.wsHandler = webSocketHandler;
58-
this.defaultSockJsTaskScheduler = defaultSockJsTaskScheduler;
57+
this.sockJsTaskScheduler = sockJsTaskScheduler;
5958
}
6059

6160

62-
protected SubProtocolWebSocketHandler getWsHandler() {
63-
return this.wsHandler;
64-
}
65-
61+
/**
62+
* Provide a custom or pre-configured {@link HandshakeHandler}. This property is
63+
* optional.
64+
*/
6665
@Override
6766
public StompEndpointRegistration setHandshakeHandler(HandshakeHandler handshakeHandler) {
6867
this.handshakeHandler = handshakeHandler;
6968
return this;
7069
}
7170

71+
/**
72+
* Enable SockJS fallback options.
73+
*/
7274
@Override
7375
public SockJsServiceRegistration withSockJS() {
7476

75-
this.sockJsServiceRegistration = new StompSockJsServiceRegistration(this.defaultSockJsTaskScheduler);
77+
this.sockJsServiceRegistration = new StompSockJsServiceRegistration(this.sockJsTaskScheduler);
7678

7779
if (this.handshakeHandler != null) {
7880
WebSocketTransportHandler transportHandler = new WebSocketTransportHandler(this.handshakeHandler);
@@ -82,7 +84,7 @@ public SockJsServiceRegistration withSockJS() {
8284
return this.sockJsServiceRegistration;
8385
}
8486

85-
protected M getMappings() {
87+
protected final M getMappings() {
8688

8789
M mappings = createMappings();
8890

spring-messaging/src/main/java/org/springframework/messaging/simp/config/MessageBrokerConfigurer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
public class MessageBrokerConfigurer {
3535

36-
private final MessageChannel webSocketReplyChannel;
36+
private final MessageChannel webSocketResponseChannel;
3737

3838
private SimpleBrokerRegistration simpleBroker;
3939

@@ -42,18 +42,18 @@ public class MessageBrokerConfigurer {
4242
private String[] annotationMethodDestinationPrefixes;
4343

4444

45-
public MessageBrokerConfigurer(MessageChannel webSocketReplyChannel) {
46-
Assert.notNull(webSocketReplyChannel);
47-
this.webSocketReplyChannel = webSocketReplyChannel;
45+
public MessageBrokerConfigurer(MessageChannel webSocketResponseChannel) {
46+
Assert.notNull(webSocketResponseChannel);
47+
this.webSocketResponseChannel = webSocketResponseChannel;
4848
}
4949

5050
public SimpleBrokerRegistration enableSimpleBroker(String... destinationPrefixes) {
51-
this.simpleBroker = new SimpleBrokerRegistration(this.webSocketReplyChannel, destinationPrefixes);
51+
this.simpleBroker = new SimpleBrokerRegistration(this.webSocketResponseChannel, destinationPrefixes);
5252
return this.simpleBroker;
5353
}
5454

5555
public StompBrokerRelayRegistration enableStompBrokerRelay(String... destinationPrefixes) {
56-
this.stompRelay = new StompBrokerRelayRegistration(this.webSocketReplyChannel, destinationPrefixes);
56+
this.stompRelay = new StompBrokerRelayRegistration(this.webSocketResponseChannel, destinationPrefixes);
5757
return this.stompRelay;
5858
}
5959

@@ -69,7 +69,7 @@ protected AbstractBrokerMessageHandler getSimpleBroker() {
6969

7070
protected void initSimpleBrokerIfNecessary() {
7171
if ((this.simpleBroker == null) && (this.stompRelay == null)) {
72-
this.simpleBroker = new SimpleBrokerRegistration(this.webSocketReplyChannel, null);
72+
this.simpleBroker = new SimpleBrokerRegistration(this.webSocketResponseChannel, null);
7373
}
7474
}
7575

spring-messaging/src/main/java/org/springframework/messaging/simp/config/StompBrokerRelayRegistration.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class StompBrokerRelayRegistration extends AbstractBrokerRegistration {
3737

3838
private String applicationPasscode = "guest";
3939

40+
private boolean autoStartup = true;
41+
4042

4143
public StompBrokerRelayRegistration(MessageChannel webSocketReplyChannel, String[] destinationPrefixes) {
4244
super(webSocketReplyChannel, destinationPrefixes);
@@ -52,13 +54,6 @@ public StompBrokerRelayRegistration setRelayHost(String relayHost) {
5254
return this;
5355
}
5456

55-
/**
56-
* @return the STOMP message broker host.
57-
*/
58-
protected String getRelayHost() {
59-
return this.relayHost;
60-
}
61-
6257
/**
6358
* Set the STOMP message broker port.
6459
*/
@@ -67,13 +62,6 @@ public StompBrokerRelayRegistration setRelayPort(int relayPort) {
6762
return this;
6863
}
6964

70-
/**
71-
* @return the STOMP message broker port.
72-
*/
73-
protected int getRelayPort() {
74-
return this.relayPort;
75-
}
76-
7765
/**
7866
* Set the login for a "system" TCP connection used to send messages to the STOMP
7967
* broker without having a client session (e.g. REST/HTTP request handling method).
@@ -84,13 +72,6 @@ public StompBrokerRelayRegistration setApplicationLogin(String login) {
8472
return this;
8573
}
8674

87-
/**
88-
* @return the login for a shared, "system" connection to the STOMP message broker.
89-
*/
90-
protected String getApplicationLogin() {
91-
return this.applicationLogin;
92-
}
93-
9475
/**
9576
* Set the passcode for a "system" TCP connection used to send messages to the STOMP
9677
* broker without having a client session (e.g. REST/HTTP request handling method).
@@ -102,10 +83,14 @@ public StompBrokerRelayRegistration setApplicationPasscode(String passcode) {
10283
}
10384

10485
/**
105-
* @return the passcode for a shared, "system" connection to the STOMP message broker.
86+
* Configure whether the {@link StompBrokerRelayMessageHandler} should start
87+
* automatically when the Spring ApplicationContext is refreshed.
88+
* <p>
89+
* The default setting is {@code true}.
10690
*/
107-
protected String getApplicationPasscode() {
108-
return this.applicationPasscode;
91+
public StompBrokerRelayRegistration setAutoStartup(boolean autoStartup) {
92+
this.autoStartup = autoStartup;
93+
return this;
10994
}
11095

11196

@@ -116,6 +101,7 @@ protected StompBrokerRelayMessageHandler getMessageHandler() {
116101
handler.setRelayPort(this.relayPort);
117102
handler.setSystemLogin(this.applicationLogin);
118103
handler.setSystemPasscode(this.applicationPasscode);
104+
handler.setAutoStartup(this.autoStartup);
119105
return handler;
120106
}
121107

spring-messaging/src/main/java/org/springframework/messaging/simp/config/WebSocketMessageBrokerConfigurationSupport.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public HandlerMapping brokerWebSocketHandlerMapping() {
6767
@Bean
6868
public SubProtocolWebSocketHandler subProtocolWebSocketHandler() {
6969
SubProtocolWebSocketHandler wsHandler = new SubProtocolWebSocketHandler(webSocketRequestChannel());
70-
webSocketReplyChannel().subscribe(wsHandler);
70+
webSocketResponseChannel().subscribe(wsHandler);
7171
return wsHandler;
7272
}
7373

@@ -109,7 +109,7 @@ public SubscribableChannel webSocketRequestChannel() {
109109
}
110110

111111
@Bean
112-
public SubscribableChannel webSocketReplyChannel() {
112+
public SubscribableChannel webSocketResponseChannel() {
113113
return new ExecutorSubscribableChannel(webSocketChannelExecutor());
114114
}
115115

@@ -125,7 +125,7 @@ public ThreadPoolTaskExecutor webSocketChannelExecutor() {
125125
@Bean
126126
public AnnotationMethodMessageHandler annotationMethodMessageHandler() {
127127
AnnotationMethodMessageHandler handler =
128-
new AnnotationMethodMessageHandler(brokerMessagingTemplate(), webSocketReplyChannel());
128+
new AnnotationMethodMessageHandler(brokerMessagingTemplate(), webSocketResponseChannel());
129129
handler.setDestinationPrefixes(getMessageBrokerConfigurer().getAnnotationMethodDestinationPrefixes());
130130
handler.setMessageConverter(brokerMessageConverter());
131131
webSocketRequestChannel().subscribe(handler);
@@ -140,7 +140,7 @@ public AbstractBrokerMessageHandler simpleBrokerMessageHandler() {
140140
}
141141
else {
142142
webSocketRequestChannel().subscribe(handler);
143-
brokerMessageChannel().subscribe(handler);
143+
brokerChannel().subscribe(handler);
144144
return handler;
145145
}
146146
}
@@ -153,14 +153,14 @@ public AbstractBrokerMessageHandler stompBrokerRelayMessageHandler() {
153153
}
154154
else {
155155
webSocketRequestChannel().subscribe(handler);
156-
brokerMessageChannel().subscribe(handler);
156+
brokerChannel().subscribe(handler);
157157
return handler;
158158
}
159159
}
160160

161161
protected final MessageBrokerConfigurer getMessageBrokerConfigurer() {
162162
if (this.messageBrokerConfigurer == null) {
163-
MessageBrokerConfigurer configurer = new MessageBrokerConfigurer(webSocketReplyChannel());
163+
MessageBrokerConfigurer configurer = new MessageBrokerConfigurer(webSocketResponseChannel());
164164
configureMessageBroker(configurer);
165165
this.messageBrokerConfigurer = configurer;
166166
}
@@ -175,19 +175,19 @@ public UserDestinationMessageHandler userDestinationMessageHandler() {
175175
UserDestinationMessageHandler handler = new UserDestinationMessageHandler(
176176
brokerMessagingTemplate(), userQueueSuffixResolver());
177177
webSocketRequestChannel().subscribe(handler);
178-
brokerMessageChannel().subscribe(handler);
178+
brokerChannel().subscribe(handler);
179179
return handler;
180180
}
181181

182182
@Bean
183183
public SimpMessageSendingOperations brokerMessagingTemplate() {
184-
SimpMessagingTemplate template = new SimpMessagingTemplate(webSocketRequestChannel());
184+
SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel());
185185
template.setMessageConverter(brokerMessageConverter());
186186
return template;
187187
}
188188

189189
@Bean
190-
public SubscribableChannel brokerMessageChannel() {
190+
public SubscribableChannel brokerChannel() {
191191
return new ExecutorSubscribableChannel(); // synchronous
192192
}
193193

spring-messaging/src/main/java/org/springframework/messaging/simp/handler/AbstractBrokerMessageHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public abstract class AbstractBrokerMessageHandler
4747

4848
private AtomicBoolean brokerAvailable = new AtomicBoolean(false);
4949

50+
private boolean autoStartup = true;
51+
5052
private Object lifecycleMonitor = new Object();
5153

5254
private volatile boolean running = false;
@@ -71,9 +73,13 @@ public ApplicationEventPublisher getApplicationEventPublisher() {
7173
return this.eventPublisher;
7274
}
7375

76+
public void setAutoStartup(boolean autoStartup) {
77+
this.autoStartup = autoStartup;
78+
}
79+
7480
@Override
7581
public boolean isAutoStartup() {
76-
return true;
82+
return this.autoStartup;
7783
}
7884

7985
@Override

spring-messaging/src/main/java/org/springframework/messaging/simp/handler/AnnotationMethodMessageHandler.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class AnnotationMethodMessageHandler implements MessageHandler, Applicati
7878

7979
private final SimpMessageSendingOperations brokerTemplate;
8080

81-
private final SimpMessageSendingOperations webSocketReplyTemplate;
81+
private final SimpMessageSendingOperations webSocketResponseTemplate;
8282

8383
private Collection<String> destinationPrefixes;
8484

@@ -106,15 +106,15 @@ public class AnnotationMethodMessageHandler implements MessageHandler, Applicati
106106

107107
/**
108108
* @param brokerTemplate a messaging template to sending messages to the broker
109-
* @param webSocketReplyChannel the channel for messages to WebSocket clients
109+
* @param webSocketResponseChannel the channel for messages to WebSocket clients
110110
*/
111111
public AnnotationMethodMessageHandler(SimpMessageSendingOperations brokerTemplate,
112-
MessageChannel webSocketReplyChannel) {
112+
MessageChannel webSocketResponseChannel) {
113113

114114
Assert.notNull(brokerTemplate, "brokerTemplate is required");
115-
Assert.notNull(webSocketReplyChannel, "webSocketReplyChannel is required");
115+
Assert.notNull(webSocketResponseChannel, "webSocketReplyChannel is required");
116116
this.brokerTemplate = brokerTemplate;
117-
this.webSocketReplyTemplate = new SimpMessagingTemplate(webSocketReplyChannel);
117+
this.webSocketResponseTemplate = new SimpMessagingTemplate(webSocketResponseChannel);
118118
}
119119

120120

@@ -129,7 +129,7 @@ public Collection<String> getDestinationPrefixes() {
129129
public void setMessageConverter(MessageConverter<?> converter) {
130130
this.messageConverter = converter;
131131
if (converter != null) {
132-
((AbstractMessageSendingTemplate<?>) this.webSocketReplyTemplate).setMessageConverter(converter);
132+
((AbstractMessageSendingTemplate<?>) this.webSocketResponseTemplate).setMessageConverter(converter);
133133
}
134134
}
135135

@@ -181,7 +181,7 @@ public void afterPropertiesSet() {
181181

182182
// Annotation-based return value types
183183
this.returnValueHandlers.addHandler(new ReplyToMethodReturnValueHandler(this.brokerTemplate));
184-
this.returnValueHandlers.addHandler(new SubscriptionMethodReturnValueHandler(this.webSocketReplyTemplate));
184+
this.returnValueHandlers.addHandler(new SubscriptionMethodReturnValueHandler(this.webSocketResponseTemplate));
185185

186186
// custom return value types
187187
this.returnValueHandlers.addHandlers(this.customReturnValueHandlers);
@@ -221,14 +221,14 @@ private <A extends Annotation> void initHandlerMethods(Object handler, Class<?>
221221
final Class<A> annotationType, MappingInfoCreator<A> mappingInfoCreator,
222222
Map<MappingInfo, HandlerMethod> handlerMethods) {
223223

224-
Set<Method> messageMethods = HandlerMethodSelector.selectMethods(handlerType, new MethodFilter() {
224+
Set<Method> methods = HandlerMethodSelector.selectMethods(handlerType, new MethodFilter() {
225225
@Override
226226
public boolean matches(Method method) {
227227
return AnnotationUtils.findAnnotation(method, annotationType) != null;
228228
}
229229
});
230230

231-
for (Method method : messageMethods) {
231+
for (Method method : methods) {
232232
A annotation = AnnotationUtils.findAnnotation(method, annotationType);
233233
HandlerMethod hm = createHandlerMethod(handler, method);
234234
handlerMethods.put(mappingInfoCreator.create(annotation), hm);

spring-messaging/src/main/java/org/springframework/messaging/support/channel/AbstractMessageChannel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,10 @@ public final boolean send(Message<?> message, long timeout) {
127127

128128
protected abstract boolean sendInternal(Message<?> message, long timeout);
129129

130+
131+
@Override
132+
public String toString() {
133+
return "MessageChannel [name=" + this.beanName + "]";
134+
}
135+
130136
}

0 commit comments

Comments
 (0)