Skip to content

Commit 85675fb

Browse files
committed
Consistent SmartLifecycle implementations
Issue: SPR-14233 (cherry picked from commit f83cbff)
1 parent 85faeef commit 85675fb

File tree

11 files changed

+116
-114
lines changed

11 files changed

+116
-114
lines changed

spring-jms/src/main/java/org/springframework/jms/listener/AbstractJmsListeningContainer.java

Lines changed: 2 additions & 2 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-2016 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.
@@ -315,7 +315,7 @@ public void stop() throws JmsException {
315315

316316
@Override
317317
public void stop(Runnable callback) {
318-
this.stop();
318+
stop();
319319
callback.run();
320320
}
321321

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -272,13 +272,6 @@ public int getPhase() {
272272
return Integer.MAX_VALUE;
273273
}
274274

275-
@Override
276-
public final boolean isRunning() {
277-
synchronized (this.lifecycleMonitor) {
278-
return this.running;
279-
}
280-
}
281-
282275
@Override
283276
public final void start() {
284277
synchronized (this.lifecycleMonitor) {
@@ -303,6 +296,13 @@ public final void stop(Runnable callback) {
303296
}
304297
}
305298

299+
@Override
300+
public final boolean isRunning() {
301+
synchronized (this.lifecycleMonitor) {
302+
return this.running;
303+
}
304+
}
305+
306306

307307
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
308308
ConfigurableBeanFactory beanFactory = (getApplicationContext() instanceof ConfigurableApplicationContext ?

spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.messaging.simp.user;
1818

19-
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.*;
20-
2119
import java.util.Arrays;
2220
import java.util.List;
2321

@@ -154,13 +152,6 @@ public boolean isAutoStartup() {
154152
return true;
155153
}
156154

157-
@Override
158-
public final boolean isRunning() {
159-
synchronized (this.lifecycleMonitor) {
160-
return this.running;
161-
}
162-
}
163-
164155
@Override
165156
public final void start() {
166157
synchronized (this.lifecycleMonitor) {
@@ -187,6 +178,13 @@ public final void stop(Runnable callback) {
187178
}
188179
}
189180

181+
@Override
182+
public final boolean isRunning() {
183+
synchronized (this.lifecycleMonitor) {
184+
return this.running;
185+
}
186+
}
187+
190188

191189
@Override
192190
public void handleMessage(Message<?> message) throws MessagingException {
@@ -211,7 +209,7 @@ public void handleMessage(Message<?> message) throws MessagingException {
211209
}
212210
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
213211
initHeaders(accessor);
214-
accessor.setNativeHeader(ORIGINAL_DESTINATION, result.getSubscribeDestination());
212+
accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, result.getSubscribeDestination());
215213
accessor.setLeaveMutable(true);
216214
message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
217215
if (logger.isTraceEnabled()) {
@@ -242,18 +240,15 @@ private static class BroadcastHandler {
242240

243241
private static final List<String> NO_COPY_LIST = Arrays.asList("subscription", "message-id");
244242

245-
246243
private final MessageSendingOperations<String> messagingTemplate;
247244

248245
private final String broadcastDestination;
249246

250-
251247
public BroadcastHandler(MessageSendingOperations<String> template, String destination) {
252248
this.messagingTemplate = template;
253249
this.broadcastDestination = destination;
254250
}
255251

256-
257252
public String getBroadcastDestination() {
258253
return this.broadcastDestination;
259254
}
@@ -263,12 +258,13 @@ public Message<?> preHandle(Message<?> message) throws MessagingException {
263258
if (!getBroadcastDestination().equals(destination)) {
264259
return message;
265260
}
266-
SimpMessageHeaderAccessor accessor = getAccessor(message, SimpMessageHeaderAccessor.class);
261+
SimpMessageHeaderAccessor accessor =
262+
SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
267263
if (accessor.getSessionId() == null) {
268264
// Our own broadcast
269265
return null;
270266
}
271-
destination = accessor.getFirstNativeHeader(ORIGINAL_DESTINATION);
267+
destination = accessor.getFirstNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION);
272268
if (logger.isTraceEnabled()) {
273269
logger.trace("Checking unresolved user destination: " + destination);
274270
}
@@ -286,13 +282,14 @@ public Message<?> preHandle(Message<?> message) throws MessagingException {
286282

287283
public void handleUnresolved(Message<?> message) {
288284
MessageHeaders headers = message.getHeaders();
289-
if (SimpMessageHeaderAccessor.getFirstNativeHeader(ORIGINAL_DESTINATION, headers) != null) {
285+
if (SimpMessageHeaderAccessor.getFirstNativeHeader(
286+
SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, headers) != null) {
290287
// Re-broadcast
291288
return;
292289
}
293290
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(message);
294291
String destination = accessor.getDestination();
295-
accessor.setNativeHeader(ORIGINAL_DESTINATION, destination);
292+
accessor.setNativeHeader(SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, destination);
296293
accessor.setLeaveMutable(true);
297294
message = MessageBuilder.createMessage(message.getPayload(), accessor.getMessageHeaders());
298295
if (logger.isTraceEnabled()) {

spring-tx/src/main/java/org/springframework/jca/endpoint/GenericMessageEndpointManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -308,7 +308,7 @@ public void stop() {
308308
@Override
309309
public void stop(Runnable callback) {
310310
synchronized (this.lifecycleMonitor) {
311-
this.stop();
311+
stop();
312312
callback.run();
313313
}
314314
}

spring-websocket/src/main/java/org/springframework/web/socket/client/ConnectionManagerSupport.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -56,6 +56,10 @@ public ConnectionManagerSupport(String uriTemplate, Object... uriVariables) {
5656
}
5757

5858

59+
protected URI getUri() {
60+
return this.uri;
61+
}
62+
5963
/**
6064
* Set whether to auto-connect to the remote endpoint after this connection manager
6165
* has been initialized and the Spring context has been refreshed.
@@ -95,22 +99,9 @@ public int getPhase() {
9599
return this.phase;
96100
}
97101

98-
protected URI getUri() {
99-
return this.uri;
100-
}
101102

102103
/**
103-
* Return whether this ConnectionManager has been started.
104-
*/
105-
@Override
106-
public boolean isRunning() {
107-
synchronized (this.lifecycleMonitor) {
108-
return this.running;
109-
}
110-
}
111-
112-
/**
113-
* Start the websocket connection. If already connected, the method has no impact.
104+
* Start the WebSocket connection. If already connected, the method has no impact.
114105
*/
115106
@Override
116107
public final void start() {
@@ -122,29 +113,27 @@ public final void start() {
122113
}
123114

124115
protected void startInternal() {
125-
synchronized (lifecycleMonitor) {
116+
synchronized (this.lifecycleMonitor) {
126117
if (logger.isInfoEnabled()) {
127-
logger.info("Starting " + this.getClass().getSimpleName());
118+
logger.info("Starting " + getClass().getSimpleName());
128119
}
129120
this.running = true;
130121
openConnection();
131122
}
132123
}
133124

134-
protected abstract void openConnection();
135-
136125
@Override
137126
public final void stop() {
138127
synchronized (this.lifecycleMonitor) {
139128
if (isRunning()) {
140129
if (logger.isInfoEnabled()) {
141-
logger.info("Stopping " + this.getClass().getSimpleName());
130+
logger.info("Stopping " + getClass().getSimpleName());
142131
}
143132
try {
144133
stopInternal();
145134
}
146-
catch (Throwable e) {
147-
logger.error("Failed to stop WebSocket connection", e);
135+
catch (Throwable ex) {
136+
logger.error("Failed to stop WebSocket connection", ex);
148137
}
149138
finally {
150139
this.running = false;
@@ -153,22 +142,35 @@ public final void stop() {
153142
}
154143
}
155144

145+
@Override
146+
public final void stop(Runnable callback) {
147+
synchronized (this.lifecycleMonitor) {
148+
stop();
149+
callback.run();
150+
}
151+
}
152+
156153
protected void stopInternal() throws Exception {
157154
if (isConnected()) {
158155
closeConnection();
159156
}
160157
}
161158

162-
protected abstract boolean isConnected();
163-
164-
protected abstract void closeConnection() throws Exception;
165-
159+
/**
160+
* Return whether this ConnectionManager has been started.
161+
*/
166162
@Override
167-
public final void stop(Runnable callback) {
163+
public boolean isRunning() {
168164
synchronized (this.lifecycleMonitor) {
169-
this.stop();
170-
callback.run();
165+
return this.running;
171166
}
172167
}
173168

169+
170+
protected abstract void openConnection();
171+
172+
protected abstract void closeConnection() throws Exception;
173+
174+
protected abstract boolean isConnected();
175+
174176
}

spring-websocket/src/main/java/org/springframework/web/socket/client/standard/AnnotatedEndpointConnectionManager.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -47,10 +47,10 @@ public class AnnotatedEndpointConnectionManager extends ConnectionManagerSupport
4747

4848
private WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
4949

50-
private Session session;
51-
5250
private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor("AnnotatedEndpointConnectionManager-");
5351

52+
private volatile Session session;
53+
5454

5555
public AnnotatedEndpointConnectionManager(Object endpoint, String uriTemplate, Object... uriVariables) {
5656
super(uriTemplate, uriVariables);
@@ -96,19 +96,22 @@ public TaskExecutor getTaskExecutor() {
9696
return this.taskExecutor;
9797
}
9898

99+
99100
@Override
100101
protected void openConnection() {
101102
this.taskExecutor.execute(new Runnable() {
102103
@Override
103104
public void run() {
104105
try {
105-
logger.info("Connecting to WebSocket at " + getUri());
106+
if (logger.isInfoEnabled()) {
107+
logger.info("Connecting to WebSocket at " + getUri());
108+
}
106109
Object endpointToUse = (endpoint != null) ? endpoint : endpointProvider.getHandler();
107110
session = webSocketContainer.connectToServer(endpointToUse, getUri());
108-
logger.info("Successfully connected");
111+
logger.info("Successfully connected to WebSocket");
109112
}
110113
catch (Throwable ex) {
111-
logger.error("Failed to connect", ex);
114+
logger.error("Failed to connect to WebSocket", ex);
112115
}
113116
}
114117
});
@@ -128,7 +131,7 @@ protected void closeConnection() throws Exception {
128131

129132
@Override
130133
protected boolean isConnected() {
131-
return ((this.session != null) && this.session.isOpen());
134+
return (this.session != null && this.session.isOpen());
132135
}
133136

134137
}

0 commit comments

Comments
 (0)