Skip to content

Commit 8ffbecc

Browse files
committed
WebSocketMessageBrokerStats implements SmartInitializingSingleton
Closes gh-26536
1 parent e6ab8a6 commit 8ffbecc

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
2828

29+
import org.springframework.beans.factory.SmartInitializingSingleton;
2930
import org.springframework.core.task.TaskExecutor;
3031
import org.springframework.lang.Nullable;
3132
import org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler;
@@ -53,7 +54,7 @@
5354
* @author Sam Brannen
5455
* @since 4.1
5556
*/
56-
public class WebSocketMessageBrokerStats {
57+
public class WebSocketMessageBrokerStats implements SmartInitializingSingleton {
5758

5859
private static final Log logger = LogFactory.getLog(WebSocketMessageBrokerStats.class);
5960

@@ -84,24 +85,6 @@ public class WebSocketMessageBrokerStats {
8485

8586
public void setSubProtocolWebSocketHandler(SubProtocolWebSocketHandler webSocketHandler) {
8687
this.webSocketHandler = webSocketHandler;
87-
this.stompSubProtocolHandler = initStompSubProtocolHandler();
88-
}
89-
90-
@Nullable
91-
private StompSubProtocolHandler initStompSubProtocolHandler() {
92-
if (this.webSocketHandler == null) {
93-
return null;
94-
}
95-
for (SubProtocolHandler handler : this.webSocketHandler.getProtocolHandlers()) {
96-
if (handler instanceof StompSubProtocolHandler stompHandler) {
97-
return stompHandler;
98-
}
99-
}
100-
SubProtocolHandler defaultHandler = this.webSocketHandler.getDefaultProtocolHandler();
101-
if (defaultHandler instanceof StompSubProtocolHandler stompHandler) {
102-
return stompHandler;
103-
}
104-
return null;
10588
}
10689

10790
public void setStompBrokerRelay(StompBrokerRelayMessageHandler stompBrokerRelay) {
@@ -118,17 +101,6 @@ public void setOutboundChannelExecutor(TaskExecutor outboundChannelExecutor) {
118101

119102
public void setSockJsTaskScheduler(TaskScheduler sockJsTaskScheduler) {
120103
this.sockJsTaskScheduler = sockJsTaskScheduler;
121-
this.loggingTask = initLoggingTask(TimeUnit.MINUTES.toMillis(1));
122-
}
123-
124-
@Nullable
125-
private ScheduledFuture<?> initLoggingTask(long initialDelay) {
126-
if (this.sockJsTaskScheduler != null && this.loggingPeriod > 0 && logger.isInfoEnabled()) {
127-
return this.sockJsTaskScheduler.scheduleWithFixedDelay(
128-
() -> logger.info(WebSocketMessageBrokerStats.this.toString()),
129-
Instant.now().plusMillis(initialDelay), Duration.ofMillis(this.loggingPeriod));
130-
}
131-
return null;
132104
}
133105

134106
/**
@@ -137,11 +109,11 @@ private ScheduledFuture<?> initLoggingTask(long initialDelay) {
137109
* <p>By default this property is set to 30 minutes (30 * 60 * 1000).
138110
*/
139111
public void setLoggingPeriod(long period) {
112+
this.loggingPeriod = period;
140113
if (this.loggingTask != null) {
141114
this.loggingTask.cancel(true);
115+
this.loggingTask = initLoggingTask(0);
142116
}
143-
this.loggingPeriod = period;
144-
this.loggingTask = initLoggingTask(0);
145117
}
146118

147119
/**
@@ -151,6 +123,41 @@ public long getLoggingPeriod() {
151123
return this.loggingPeriod;
152124
}
153125

126+
127+
@Override
128+
public void afterSingletonsInstantiated() {
129+
this.stompSubProtocolHandler = initStompSubProtocolHandler();
130+
this.loggingTask = initLoggingTask(TimeUnit.MINUTES.toMillis(1));
131+
}
132+
133+
@Nullable
134+
private StompSubProtocolHandler initStompSubProtocolHandler() {
135+
if (this.webSocketHandler == null) {
136+
return null;
137+
}
138+
for (SubProtocolHandler handler : this.webSocketHandler.getProtocolHandlers()) {
139+
if (handler instanceof StompSubProtocolHandler stompHandler) {
140+
return stompHandler;
141+
}
142+
}
143+
SubProtocolHandler defaultHandler = this.webSocketHandler.getDefaultProtocolHandler();
144+
if (defaultHandler instanceof StompSubProtocolHandler stompHandler) {
145+
return stompHandler;
146+
}
147+
return null;
148+
}
149+
150+
@Nullable
151+
private ScheduledFuture<?> initLoggingTask(long initialDelay) {
152+
if (this.sockJsTaskScheduler != null && this.loggingPeriod > 0 && logger.isInfoEnabled()) {
153+
return this.sockJsTaskScheduler.scheduleWithFixedDelay(
154+
() -> logger.info(WebSocketMessageBrokerStats.this.toString()),
155+
Instant.now().plusMillis(initialDelay), Duration.ofMillis(this.loggingPeriod));
156+
}
157+
return null;
158+
}
159+
160+
154161
/**
155162
* Get stats about WebSocket sessions.
156163
*/

0 commit comments

Comments
 (0)