26
26
import org .apache .commons .logging .Log ;
27
27
import org .apache .commons .logging .LogFactory ;
28
28
29
+ import org .springframework .beans .factory .SmartInitializingSingleton ;
29
30
import org .springframework .core .task .TaskExecutor ;
30
31
import org .springframework .lang .Nullable ;
31
32
import org .springframework .messaging .simp .stomp .StompBrokerRelayMessageHandler ;
53
54
* @author Sam Brannen
54
55
* @since 4.1
55
56
*/
56
- public class WebSocketMessageBrokerStats {
57
+ public class WebSocketMessageBrokerStats implements SmartInitializingSingleton {
57
58
58
59
private static final Log logger = LogFactory .getLog (WebSocketMessageBrokerStats .class );
59
60
@@ -84,24 +85,6 @@ public class WebSocketMessageBrokerStats {
84
85
85
86
public void setSubProtocolWebSocketHandler (SubProtocolWebSocketHandler webSocketHandler ) {
86
87
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 ;
105
88
}
106
89
107
90
public void setStompBrokerRelay (StompBrokerRelayMessageHandler stompBrokerRelay ) {
@@ -118,17 +101,6 @@ public void setOutboundChannelExecutor(TaskExecutor outboundChannelExecutor) {
118
101
119
102
public void setSockJsTaskScheduler (TaskScheduler sockJsTaskScheduler ) {
120
103
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 ;
132
104
}
133
105
134
106
/**
@@ -137,11 +109,11 @@ private ScheduledFuture<?> initLoggingTask(long initialDelay) {
137
109
* <p>By default this property is set to 30 minutes (30 * 60 * 1000).
138
110
*/
139
111
public void setLoggingPeriod (long period ) {
112
+ this .loggingPeriod = period ;
140
113
if (this .loggingTask != null ) {
141
114
this .loggingTask .cancel (true );
115
+ this .loggingTask = initLoggingTask (0 );
142
116
}
143
- this .loggingPeriod = period ;
144
- this .loggingTask = initLoggingTask (0 );
145
117
}
146
118
147
119
/**
@@ -151,6 +123,41 @@ public long getLoggingPeriod() {
151
123
return this .loggingPeriod ;
152
124
}
153
125
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
+
154
161
/**
155
162
* Get stats about WebSocket sessions.
156
163
*/
0 commit comments