1 /*-
2 * #%L
3 * Spring Stomp Server
4 * ===============================================================
5 * Copyright (C) 2020 Brabenetz Harald, Austria
6 * ===============================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * #L%
19 */
20 package net.brabenetz.app.springstompserver.config;
21
22 import org.springframework.boot.context.properties.ConfigurationProperties;
23 import org.springframework.stereotype.Component;
24 import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
25 import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
26 import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
27
28 /**
29 * Additional Websocket Stomp Properties use in the {@link WebSocketConfig}.
30 */
31 @Component
32 @ConfigurationProperties("spring-stomp-server")
33 @SuppressWarnings("PMD.DataClass")
34 public class WebSocketConfigProperties {
35
36 /**
37 * The Simple Message Broker destination prefixes which are handled by the WebSocket Stomp-Endpoint.
38 */
39 private String[] destinationPrefixes = new String[] {"/topic", "/app", "/user"};
40
41 /**
42 * The Websocket Endpoint which are handled by the {@link StompEndpointRegistry}.
43 * <p>
44 * Both variants with and without sockJs are supported.
45 */
46 private String[] websocketEndpoints = new String[] {"/websocket"};
47
48 /**
49 * The WebSocketTransportRegistration messageSizeLimit. Optional, The default value is 64K (i.e. 64 * 1024).
50 *
51 * @see WebSocketTransportRegistration#setMessageSizeLimit(int)
52 */
53 private Integer messageSizeLimit;
54
55 /**
56 * The WebSocketTransportRegistration sendBufferSizeLimit. Optional, The default value is 512K (i.e. 512 * 1024).
57 *
58 * @see WebSocketTransportRegistration#setSendBufferSizeLimit(int)
59 */
60 private Integer sendBufferSizeLimit;
61
62 /**
63 * The WebSocketTransportRegistration sendTimeLimit. Optional, The default value is 10 seconds (i.e. 10 * 10000).
64 *
65 * @see WebSocketTransportRegistration#setSendTimeLimit(int)
66 */
67 private Integer sendTimeLimit;
68
69 /**
70 * The WebSocketTransportRegistration timeToFirstMessage. Optional, The default is set to 60,000 (1 minute).
71 *
72 * @see WebSocketTransportRegistration#setTimeToFirstMessage(int)
73 */
74 private Integer timeToFirstMessage;
75
76 /**
77 * The activation of SocketJs: Set to true if a SockJs-Supported Endpoint should be registered.
78 * <p>
79 * Default is false, because nowadays (2020) all Browsers support Websockets natively.
80 *
81 * @see <a href="https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/using-stomp-with-sockjs.html">StompJs: using-stomp-with-sockjs.html</a>
82 */
83 private boolean withSockJs;
84
85 /**
86 * The cache limit to apply for registrations with the broker.
87 * <p>
88 * This is currently only applied for the destination cache in the subscription registry. The default cache limit there is 1024.
89 * <p>
90 * For performance-testing read https://programmersought.com/article/58855147686/ <br>
91 * ... There is a fatal weakness in SimpleBrokerMessageHandler ... <br>
92 * ... If the cache size exceeds the cacheLimit value, the original old The data is cleaned up, and when it is read again ...
93 */
94 private int brokerRegistryCacheLimit = 1024;
95
96 /**
97 * The MessageChannel PoolSize used for incoming messages from WebSocket clients.<br>
98 * See {@link WebSocketMessageBrokerConfigurer#configureClientInboundChannel(org.springframework.messaging.simp.config.ChannelRegistration)}
99 * <p>
100 * By default the channel is backedby a thread pool of size 1.
101 */
102 private int channelInboundCorePoolSize = 1;
103
104 /**
105 * The MessageChannel PoolSize used for outbound messages to WebSocket clients.<br>
106 * See {@link WebSocketMessageBrokerConfigurer#configureClientOutboundChannel(org.springframework.messaging.simp.config.ChannelRegistration)}
107 * <p>
108 * By default the channel is backed by a thread pool of size 1.
109 */
110 private int channelOutboundCorePoolSize = 1;
111
112 /**
113 * The channel PoolSize used to send messages from the application to the message broker.
114 * <p>
115 * By default, messages from the application to the message broker are sent synchronously, which means application code sending a message will find out if
116 * the message cannot be sent through an exception. <br>
117 * However, this can be changed if the broker channel is configured here with task executor properties.
118 */
119 private int channelBrokerCorePoolSize = 1;
120
121 /**
122 * Gets the Simple Message Broker destination prefixes which are handled by the WebSocket Stomp-Endpoint.
123 *
124 * @return the Simple Message Broker destination prefixes which are handled by the WebSocket Stomp-Endpoint
125 */
126 public String[] getDestinationPrefixes() {
127 return destinationPrefixes;
128 }
129
130 /**
131 * Sets the Simple Message Broker destination prefixes which are handled by the WebSocket Stomp-Endpoint.
132 *
133 * @param destinationPrefixes the new Simple Message Broker destination prefixes which are handled by the WebSocket Stomp-Endpoint
134 */
135 public void setDestinationPrefixes(final String... destinationPrefixes) {
136 this.destinationPrefixes = destinationPrefixes;
137 }
138
139 /**
140 * Gets the Websocket Endpoint which are handled by the {@link StompEndpointRegistry}.
141 * <p>
142 * Both variants with and without sockJs are supported.
143 *
144 * @return the Websocket Endpoint which are handled by the {@link StompEndpointRegistry}
145 */
146 public String[] getWebsocketEndpoints() {
147 return websocketEndpoints;
148 }
149
150 /**
151 * Sets the Websocket Endpoint which are handled by the {@link StompEndpointRegistry}.
152 * <p>
153 * Both variants with and without sockJs are supported.
154 *
155 * @param websocketEndpoints the new Websocket Endpoint which are handled by the {@link StompEndpointRegistry}
156 */
157 public void setWebsocketEndpoints(final String... websocketEndpoints) {
158 this.websocketEndpoints = websocketEndpoints;
159 }
160
161 /**
162 * Gets the WebSocketTransportRegistration messageSizeLimit. Optional, The default value is 64K (i.e. 64 * 1024).
163 *
164 * @return the WebSocketTransportRegistration messageSizeLimit
165 */
166 public Integer getMessageSizeLimit() {
167 return messageSizeLimit;
168 }
169
170 /**
171 * Sets the WebSocketTransportRegistration messageSizeLimit. Optional, The default value is 64K (i.e. 64 * 1024).
172 *
173 * @param messageSizeLimit the new WebSocketTransportRegistration messageSizeLimit
174 */
175 public void setMessageSizeLimit(Integer messageSizeLimit) {
176 this.messageSizeLimit = messageSizeLimit;
177 }
178
179 /**
180 * Gets the WebSocketTransportRegistration sendBufferSizeLimit. Optional, The default value is 512K (i.e. 512 * 1024).
181 *
182 * @return the WebSocketTransportRegistration sendBufferSizeLimit
183 */
184 public Integer getSendBufferSizeLimit() {
185 return sendBufferSizeLimit;
186 }
187
188 /**
189 * Sets the WebSocketTransportRegistration sendBufferSizeLimit. Optional, The default value is 512K (i.e. 512 * 1024).
190 *
191 * @param sendBufferSizeLimit the new WebSocketTransportRegistration sendBufferSizeLimit
192 */
193 public void setSendBufferSizeLimit(Integer sendBufferSizeLimit) {
194 this.sendBufferSizeLimit = sendBufferSizeLimit;
195 }
196
197 /**
198 * Gets the WebSocketTransportRegistration sendTimeLimit. Optional, The default value is 10 seconds (i.e. 10 * 10000).
199 *
200 * @return the WebSocketTransportRegistration sendTimeLimit
201 */
202 public Integer getSendTimeLimit() {
203 return sendTimeLimit;
204 }
205
206 /**
207 * Sets the WebSocketTransportRegistration sendTimeLimit. Optional, The default value is 10 seconds (i.e. 10 * 10000).
208 *
209 * @param sendTimeLimit the new WebSocketTransportRegistration sendTimeLimit
210 */
211 public void setSendTimeLimit(Integer sendTimeLimit) {
212 this.sendTimeLimit = sendTimeLimit;
213 }
214
215 /**
216 * Gets the WebSocketTransportRegistration timeToFirstMessage. Optional, The default is set to 60,000 (1 minute).
217 *
218 * @return the WebSocketTransportRegistration timeToFirstMessage
219 */
220 public Integer getTimeToFirstMessage() {
221 return timeToFirstMessage;
222 }
223
224 /**
225 * Sets the WebSocketTransportRegistration timeToFirstMessage. Optional, The default is set to 60,000 (1 minute).
226 *
227 * @param timeToFirstMessage the new WebSocketTransportRegistration timeToFirstMessage
228 */
229 public void setTimeToFirstMessage(Integer timeToFirstMessage) {
230 this.timeToFirstMessage = timeToFirstMessage;
231 }
232
233 /**
234 * Checks if is activation of SocketJs: Set to true if a SockJs-Supported Endpoint should be registered.
235 * <p>
236 * Default is false, because nowadays (2020) all Browsers support Websockets natively.
237 *
238 * @return the activation of SocketJs: Set to true if a SockJs-Supported Endpoint should be registered
239 */
240 public boolean isWithSockJs() {
241 return withSockJs;
242 }
243
244 /**
245 * Sets the activation of SocketJs: Set to true if a SockJs-Supported Endpoint should be registered.
246 * <p>
247 * Default is false, because nowadays (2020) all Browsers support Websockets natively.
248 *
249 * @param withSockJs the new activation of SocketJs: Set to true if a SockJs-Supported Endpoint should be registered
250 */
251 public void setWithSockJs(boolean withSockJs) {
252 this.withSockJs = withSockJs;
253 }
254
255 /**
256 * Gets the cache limit to apply for registrations with the broker.
257 * <p>
258 * This is currently only applied for the destination cache in the subscription registry. The default cache limit there is 1024.
259 * <p>
260 * For performance-testing read https://programmersought.com/article/58855147686/ <br>
261 * .. There is a fatal weakness in SimpleBrokerMessageHandler .. <br>
262 * .. If the cache size exceeds the cacheLimit value, the original old The data is cleaned up, and when it is read again ..
263 *
264 * @return the cache limit to apply for registrations with the broker
265 */
266 public int getBrokerRegistryCacheLimit() {
267 return brokerRegistryCacheLimit;
268 }
269
270 /**
271 * Sets the cache limit to apply for registrations with the broker.
272 * <p>
273 * This is currently only applied for the destination cache in the subscription registry. The default cache limit there is 1024.
274 * <p>
275 * For performance-testing read https://programmersought.com/article/58855147686/ <br>
276 * .. There is a fatal weakness in SimpleBrokerMessageHandler .. <br>
277 * .. If the cache size exceeds the cacheLimit value, the original old The data is cleaned up, and when it is read again ..
278 *
279 * @param brokerRegistryCacheLimit the new cache limit to apply for registrations with the broker
280 */
281 public void setBrokerRegistryCacheLimit(int brokerRegistryCacheLimit) {
282 this.brokerRegistryCacheLimit = brokerRegistryCacheLimit;
283 }
284
285 /**
286 * Gets the MessageChannel PoolSize used for incoming messages from WebSocket clients.<br>
287 * See {@link WebSocketMessageBrokerConfigurer#configureClientInboundChannel(org.springframework.messaging.simp.config.ChannelRegistration)}
288 * <p>
289 * By default the channel is backedby a thread pool of size 1.
290 *
291 * @return the MessageChannel PoolSize used for incoming messages from WebSocket clients
292 */
293 public int getChannelInboundCorePoolSize() {
294 return channelInboundCorePoolSize;
295 }
296
297 /**
298 * Sets the MessageChannel PoolSize used for incoming messages from WebSocket clients.<br>
299 * See {@link WebSocketMessageBrokerConfigurer#configureClientInboundChannel(org.springframework.messaging.simp.config.ChannelRegistration)}
300 * <p>
301 * By default the channel is backedby a thread pool of size 1.
302 *
303 * @param channelInboundCorePoolSize the new MessageChannel PoolSize used for incoming messages from WebSocket clients
304 */
305 public void setChannelInboundCorePoolSize(int channelInboundCorePoolSize) {
306 this.channelInboundCorePoolSize = channelInboundCorePoolSize;
307 }
308
309 /**
310 * Gets the MessageChannel PoolSize used for outbound messages to WebSocket clients.<br>
311 * See {@link WebSocketMessageBrokerConfigurer#configureClientOutboundChannel(org.springframework.messaging.simp.config.ChannelRegistration)}
312 * <p>
313 * By default the channel is backed by a thread pool of size 1.
314 *
315 * @return the MessageChannel PoolSize used for outbound messages to WebSocket clients
316 */
317 public int getChannelOutboundCorePoolSize() {
318 return channelOutboundCorePoolSize;
319 }
320
321 /**
322 * Sets the MessageChannel PoolSize used for outbound messages to WebSocket clients.<br>
323 * See {@link WebSocketMessageBrokerConfigurer#configureClientOutboundChannel(org.springframework.messaging.simp.config.ChannelRegistration)}
324 * <p>
325 * By default the channel is backed by a thread pool of size 1.
326 *
327 * @param channelOutboundCorePoolSize the new MessageChannel PoolSize used for outbound messages to WebSocket clients
328 */
329 public void setChannelOutboundCorePoolSize(int channelOutboundCorePoolSize) {
330 this.channelOutboundCorePoolSize = channelOutboundCorePoolSize;
331 }
332
333 /**
334 * Gets the channel PoolSize used to send messages from the application to the message broker.
335 * <p>
336 * By default, messages from the application to the message broker are sent synchronously, which means application code sending a message will find out if
337 * the message cannot be sent through an exception. <br>
338 * However, this can be changed if the broker channel is configured here with task executor properties.
339 *
340 * @return the channel PoolSize used to send messages from the application to the message broker
341 */
342 public int getChannelBrokerCorePoolSize() {
343 return channelBrokerCorePoolSize;
344 }
345
346 /**
347 * Sets the channel PoolSize used to send messages from the application to the message broker.
348 * <p>
349 * By default, messages from the application to the message broker are sent synchronously, which means application code sending a message will find out if
350 * the message cannot be sent through an exception. <br>
351 * However, this can be changed if the broker channel is configured here with task executor properties.
352 *
353 * @param channelBrokerCorePoolSize the new channel PoolSize used to send messages from the application to the message broker
354 */
355 public void setChannelBrokerCorePoolSize(int channelBrokerCorePoolSize) {
356 this.channelBrokerCorePoolSize = channelBrokerCorePoolSize;
357 }
358
359 }