Skip to content

Commit 0b5c099

Browse files
committed
Polishing
1 parent 4f9a18f commit 0b5c099

File tree

8 files changed

+64
-88
lines changed

8 files changed

+64
-88
lines changed

spring-messaging/src/main/java/org/springframework/messaging/support/ChannelInterceptor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828
* @author Mark Fisher
2929
* @author Rossen Stoyanchev
3030
* @since 4.0
31+
* @see Message
32+
* @see MessageChannel
3133
*/
3234
public interface ChannelInterceptor {
3335

@@ -56,8 +58,8 @@ default void postSend(Message<?> message, MessageChannel channel, boolean sent)
5658
* completed and returned a Message, i.e. it did not return {@code null}.
5759
* @since 4.1
5860
*/
59-
default void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent,
60-
@Nullable Exception ex) {
61+
default void afterSendCompletion(
62+
Message<?> message, MessageChannel channel, boolean sent, @Nullable Exception ex) {
6163
}
6264

6365
/**

spring-messaging/src/main/java/org/springframework/messaging/support/ExecutorChannelInterceptor.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,10 +26,13 @@
2626
* asynchronous sending of a {@link org.springframework.messaging.Message} to
2727
* a specific subscriber through an {@link java.util.concurrent.Executor}.
2828
* Supported on {@link org.springframework.messaging.MessageChannel}
29-
* implementations that can be configured with an Executor.
29+
* implementations that can be configured with an {@code Executor}.
3030
*
3131
* @author Rossen Stoyanchev
3232
* @since 4.1
33+
* @see Message
34+
* @see MessageChannel
35+
* @see MessageHandler
3336
*/
3437
public interface ExecutorChannelInterceptor extends ChannelInterceptor {
3538

@@ -44,7 +47,9 @@ public interface ExecutorChannelInterceptor extends ChannelInterceptor {
4447
* @return the input message, or a new instance, or {@code null}
4548
*/
4649
@Nullable
47-
Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler);
50+
default Message<?> beforeHandle(Message<?> message, MessageChannel channel, MessageHandler handler) {
51+
return message;
52+
}
4853

4954
/**
5055
* Invoked inside the {@link Runnable} submitted to the Executor after calling
@@ -57,6 +62,8 @@ public interface ExecutorChannelInterceptor extends ChannelInterceptor {
5762
* @param handler the target handler that handled the message
5863
* @param ex any exception that may been raised by the handler
5964
*/
60-
void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler, @Nullable Exception ex);
65+
default void afterMessageHandled(
66+
Message<?> message, MessageChannel channel, MessageHandler handler, @Nullable Exception ex) {
67+
}
6168

6269
}

spring-messaging/src/test/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandlerTests.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,23 +38,11 @@
3838
import org.springframework.messaging.support.MessageBuilder;
3939
import org.springframework.scheduling.TaskScheduler;
4040

41-
import static org.junit.Assert.assertArrayEquals;
42-
import static org.junit.Assert.assertEquals;
43-
import static org.junit.Assert.assertNotNull;
44-
import static org.junit.Assert.assertNull;
45-
import static org.junit.Assert.assertSame;
46-
import static org.junit.Assert.assertTrue;
47-
import static org.mockito.Mockito.any;
48-
import static org.mockito.Mockito.atLeast;
49-
import static org.mockito.Mockito.eq;
50-
import static org.mockito.Mockito.mock;
51-
import static org.mockito.Mockito.times;
52-
import static org.mockito.Mockito.verify;
53-
import static org.mockito.Mockito.verifyNoMoreInteractions;
54-
import static org.mockito.Mockito.when;
41+
import static org.junit.Assert.*;
42+
import static org.mockito.Mockito.*;
5543

5644
/**
57-
* Unit tests for SimpleBrokerMessageHandler.
45+
* Unit tests for {@link SimpleBrokerMessageHandler}.
5846
*
5947
* @author Rossen Stoyanchev
6048
* @since 4.0
@@ -89,8 +77,7 @@ public void setup() {
8977

9078

9179
@Test
92-
public void subcribePublish() {
93-
80+
public void subscribePublish() {
9481
this.messageHandler.start();
9582

9683
this.messageHandler.handleMessage(createSubscriptionMessage("sess1", "sub1", "/foo"));
@@ -114,8 +101,7 @@ public void subcribePublish() {
114101
}
115102

116103
@Test
117-
public void subcribeDisconnectPublish() {
118-
104+
public void subscribeDisconnectPublish() {
119105
String sess1 = "sess1";
120106
String sess2 = "sess2";
121107

@@ -153,7 +139,6 @@ public void subcribeDisconnectPublish() {
153139

154140
@Test
155141
public void connect() {
156-
157142
this.messageHandler.start();
158143

159144
String id = "sess1";
@@ -173,26 +158,23 @@ public void connect() {
173158
}
174159

175160
@Test
176-
public void heartbeatValueWithAndWithoutTaskScheduler() throws Exception {
177-
161+
public void heartbeatValueWithAndWithoutTaskScheduler() {
178162
assertNull(this.messageHandler.getHeartbeatValue());
179-
180163
this.messageHandler.setTaskScheduler(this.taskScheduler);
181164

182165
assertNotNull(this.messageHandler.getHeartbeatValue());
183166
assertArrayEquals(new long[] {10000, 10000}, this.messageHandler.getHeartbeatValue());
184167
}
185168

186169
@Test(expected = IllegalArgumentException.class)
187-
public void startWithHeartbeatValueWithoutTaskScheduler() throws Exception {
170+
public void startWithHeartbeatValueWithoutTaskScheduler() {
188171
this.messageHandler.setHeartbeatValue(new long[] {10000, 10000});
189172
this.messageHandler.start();
190173
}
191174

192175
@SuppressWarnings("unchecked")
193176
@Test
194-
public void startAndStopWithHeartbeatValue() throws Exception {
195-
177+
public void startAndStopWithHeartbeatValue() {
196178
ScheduledFuture future = mock(ScheduledFuture.class);
197179
when(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), eq(15000L))).thenReturn(future);
198180

@@ -211,8 +193,7 @@ public void startAndStopWithHeartbeatValue() throws Exception {
211193

212194
@SuppressWarnings("unchecked")
213195
@Test
214-
public void startWithOneZeroHeartbeatValue() throws Exception {
215-
196+
public void startWithOneZeroHeartbeatValue() {
216197
this.messageHandler.setTaskScheduler(this.taskScheduler);
217198
this.messageHandler.setHeartbeatValue(new long[] {0, 10000});
218199
this.messageHandler.start();
@@ -222,7 +203,6 @@ public void startWithOneZeroHeartbeatValue() throws Exception {
222203

223204
@Test
224205
public void readInactivity() throws Exception {
225-
226206
this.messageHandler.setHeartbeatValue(new long[] {0, 1});
227207
this.messageHandler.setTaskScheduler(this.taskScheduler);
228208
this.messageHandler.start();
@@ -254,7 +234,6 @@ public void readInactivity() throws Exception {
254234

255235
@Test
256236
public void writeInactivity() throws Exception {
257-
258237
this.messageHandler.setHeartbeatValue(new long[] {1, 0});
259238
this.messageHandler.setTaskScheduler(this.taskScheduler);
260239
this.messageHandler.start();
@@ -286,7 +265,6 @@ public void writeInactivity() throws Exception {
286265

287266
@Test
288267
public void readWriteIntervalCalculation() throws Exception {
289-
290268
this.messageHandler.setHeartbeatValue(new long[] {1, 1});
291269
this.messageHandler.setTaskScheduler(this.taskScheduler);
292270
this.messageHandler.start();
@@ -311,9 +289,10 @@ public void readWriteIntervalCalculation() throws Exception {
311289
messages.get(0).getHeaders().get(SimpMessageHeaderAccessor.MESSAGE_TYPE_HEADER));
312290
}
313291

314-
private Message<String> createSubscriptionMessage(String sessionId, String subcriptionId, String destination) {
292+
293+
private Message<String> createSubscriptionMessage(String sessionId, String subscriptionId, String destination) {
315294
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
316-
headers.setSubscriptionId(subcriptionId);
295+
headers.setSubscriptionId(subscriptionId);
317296
headers.setDestination(destination);
318297
headers.setSessionId(sessionId);
319298
return MessageBuilder.createMessage("", headers.getMessageHeaders());
@@ -333,11 +312,11 @@ private Message<String> createMessage(String destination, String payload) {
333312
return MessageBuilder.createMessage(payload, headers.getMessageHeaders());
334313
}
335314

336-
private boolean messageCaptured(String sessionId, String subcriptionId, String destination) {
315+
private boolean messageCaptured(String sessionId, String subscriptionId, String destination) {
337316
for (Message<?> message : this.messageCaptor.getAllValues()) {
338317
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
339318
if (sessionId.equals(headers.getSessionId())) {
340-
if (subcriptionId.equals(headers.getSubscriptionId())) {
319+
if (subscriptionId.equals(headers.getSubscriptionId())) {
341320
if (destination.equals(headers.getDestination())) {
342321
return true;
343322
}

spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,10 @@ public void clientInboundChannelCustomized() {
117117

118118
AbstractSubscribableChannel channel = context.getBean(
119119
"clientInboundChannel", AbstractSubscribableChannel.class);
120-
121120
assertEquals(3, channel.getInterceptors().size());
122121

123122
CustomThreadPoolTaskExecutor taskExecutor = context.getBean(
124123
"clientInboundChannelExecutor", CustomThreadPoolTaskExecutor.class);
125-
126124
assertEquals(11, taskExecutor.getCorePoolSize());
127125
assertEquals(12, taskExecutor.getMaxPoolSize());
128126
assertEquals(13, taskExecutor.getKeepAliveSeconds());
@@ -480,7 +478,6 @@ private void testDotSeparator(ApplicationContext context, boolean expectLeadingS
480478
TestChannel outChannel = context.getBean("clientOutboundChannel", TestChannel.class);
481479
MessageChannel brokerChannel = context.getBean("brokerChannel", MessageChannel.class);
482480

483-
484481
// 1. Subscribe to user destination
485482

486483
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
@@ -506,7 +503,6 @@ private void testDotSeparator(ApplicationContext context, boolean expectLeadingS
506503
assertEquals(expectLeadingSlash ? "/queue.q1-usersess1" : "queue.q1-usersess1", headers.getDestination());
507504
assertEquals("123", new String((byte[]) outputMessage.getPayload()));
508505

509-
510506
// 3. Send message via broker channel
511507

512508
SimpMessagingTemplate template = new SimpMessagingTemplate(brokerChannel);

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandlerIntegrationTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@
4949
import org.springframework.util.Assert;
5050
import org.springframework.util.SocketUtils;
5151

52-
import static org.junit.Assert.assertEquals;
53-
import static org.junit.Assert.assertNotNull;
54-
import static org.junit.Assert.assertTrue;
52+
import static org.junit.Assert.*;
5553

5654
/**
5755
* Integration tests for {@link StompBrokerRelayMessageHandler} running against ActiveMQ.
@@ -79,7 +77,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
7977

8078

8179
@Before
82-
public void setUp() throws Exception {
80+
public void setup() throws Exception {
8381
logger.debug("Setting up before '" + this.testName.getMethodName() + "'");
8482
this.port = SocketUtils.findAvailableTcpPort(61613);
8583
this.responseChannel = new ExecutorSubscribableChannel();
@@ -114,7 +112,7 @@ private void createAndStartRelay() throws InterruptedException {
114112
}
115113

116114
@After
117-
public void tearDown() throws Exception {
115+
public void stop() throws Exception {
118116
try {
119117
logger.debug("STOMP broker relay stats: " + this.relay.getStatsInfo());
120118
this.relay.stop();
@@ -168,7 +166,6 @@ public void publishSubscribe() throws Exception {
168166

169167
@Test(expected = MessageDeliveryException.class)
170168
public void messageDeliveryExceptionIfSystemSessionForwardFails() throws Exception {
171-
172169
logger.debug("Starting test messageDeliveryExceptionIfSystemSessionForwardFails()");
173170

174171
stopActiveMqBrokerAndAwait();
@@ -179,8 +176,8 @@ public void messageDeliveryExceptionIfSystemSessionForwardFails() throws Excepti
179176
}
180177

181178
@Test
182-
public void brokerBecomingUnvailableTriggersErrorFrame() throws Exception {
183-
logger.debug("Starting test brokerBecomingUnvailableTriggersErrorFrame()");
179+
public void brokerBecomingUnavailableTriggersErrorFrame() throws Exception {
180+
logger.debug("Starting test brokerBecomingUnavailableTriggersErrorFrame()");
184181

185182
String sess1 = "sess1";
186183
MessageExchange connect = MessageExchangeBuilder.connect(sess1).build();

spring-messaging/src/test/java/org/springframework/messaging/support/ExecutorSubscribableChannelTests.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,8 +65,9 @@ public void setup() {
6565
MockitoAnnotations.initMocks(this);
6666
}
6767

68+
6869
@Test
69-
public void messageMustNotBeNull() throws Exception {
70+
public void messageMustNotBeNull() {
7071
thrown.expect(IllegalArgumentException.class);
7172
thrown.expectMessage("Message must not be null");
7273
this.channel.send(null);
@@ -84,7 +85,7 @@ public void sendWithoutExecutor() {
8485
}
8586

8687
@Test
87-
public void sendWithExecutor() throws Exception {
88+
public void sendWithExecutor() {
8889
BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
8990
TaskExecutor executor = mock(TaskExecutor.class);
9091
ExecutorSubscribableChannel testChannel = new ExecutorSubscribableChannel(executor);
@@ -100,15 +101,15 @@ public void sendWithExecutor() throws Exception {
100101
}
101102

102103
@Test
103-
public void subscribeTwice() throws Exception {
104+
public void subscribeTwice() {
104105
assertThat(this.channel.subscribe(this.handler), equalTo(true));
105106
assertThat(this.channel.subscribe(this.handler), equalTo(false));
106107
this.channel.send(this.message);
107108
verify(this.handler, times(1)).handleMessage(this.message);
108109
}
109110

110111
@Test
111-
public void unsubscribeTwice() throws Exception {
112+
public void unsubscribeTwice() {
112113
this.channel.subscribe(this.handler);
113114
assertThat(this.channel.unsubscribe(this.handler), equalTo(true));
114115
assertThat(this.channel.unsubscribe(this.handler), equalTo(false));
@@ -117,7 +118,7 @@ public void unsubscribeTwice() throws Exception {
117118
}
118119

119120
@Test
120-
public void failurePropagates() throws Exception {
121+
public void failurePropagates() {
121122
RuntimeException ex = new RuntimeException();
122123
willThrow(ex).given(this.handler).handleMessage(this.message);
123124
MessageHandler secondHandler = mock(MessageHandler.class);
@@ -133,7 +134,7 @@ public void failurePropagates() throws Exception {
133134
}
134135

135136
@Test
136-
public void concurrentModification() throws Exception {
137+
public void concurrentModification() {
137138
this.channel.subscribe(message1 -> channel.unsubscribe(handler));
138139
this.channel.subscribe(this.handler);
139140
this.channel.send(this.message);
@@ -208,8 +209,8 @@ public Message<?> beforeHandle(Message<?> message, MessageChannel channel, Messa
208209
}
209210

210211
@Override
211-
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler,
212-
Exception ex) {
212+
public void afterMessageHandled(
213+
Message<?> message, MessageChannel channel, MessageHandler handler, Exception ex) {
213214

214215
this.afterHandledInvoked = true;
215216
}

0 commit comments

Comments
 (0)