1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.brabenetz.app.springstompserver.config;
21
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.context.annotation.Configuration;
24 import org.springframework.context.annotation.Lazy;
25 import org.springframework.messaging.simp.config.ChannelRegistration;
26 import org.springframework.messaging.simp.config.MessageBrokerRegistry;
27 import org.springframework.scheduling.TaskScheduler;
28 import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
29 import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
30 import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
31 import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;
32
33
34
35
36 @Configuration
37 @EnableWebSocketMessageBroker
38 public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
39
40 @Autowired
41 private WebSocketConfigProperties properties;
42
43 @Autowired
44 @Lazy
45 private TaskScheduler messageBrokerTaskScheduler;
46
47 @Override
48 public void configureMessageBroker(final MessageBrokerRegistry registry) {
49 registry.enableSimpleBroker(properties.getDestinationPrefixes())
50 .setTaskScheduler(messageBrokerTaskScheduler);
51
52 registry.configureBrokerChannel().taskExecutor().corePoolSize(properties.getChannelBrokerCorePoolSize());
53
54 registry.setCacheLimit(properties.getBrokerRegistryCacheLimit());
55 }
56
57 @Override
58 public void registerStompEndpoints(final StompEndpointRegistry registry) {
59 registry.addEndpoint(properties.getWebsocketEndpoints());
60
61 if (properties.isWithSockJs()) {
62 registry.addEndpoint(properties.getWebsocketEndpoints()).withSockJS();
63 }
64 }
65
66 @Override
67 public void configureWebSocketTransport(final WebSocketTransportRegistration registry) {
68 if (properties.getMessageSizeLimit() != null) {
69 registry.setMessageSizeLimit(properties.getMessageSizeLimit());
70 }
71 if (properties.getSendBufferSizeLimit() != null) {
72 registry.setSendBufferSizeLimit(properties.getSendBufferSizeLimit());
73 }
74 if (properties.getSendTimeLimit() != null) {
75 registry.setSendTimeLimit(properties.getSendTimeLimit());
76 }
77 if (properties.getTimeToFirstMessage() != null) {
78 registry.setTimeToFirstMessage(1000);
79 }
80 }
81
82 @Override
83 public void configureClientInboundChannel(ChannelRegistration registration) {
84
85 registration.taskExecutor().corePoolSize(properties.getChannelInboundCorePoolSize());
86 }
87
88 @Override
89 public void configureClientOutboundChannel(ChannelRegistration registration) {
90
91 registration.taskExecutor().corePoolSize(properties.getChannelOutboundCorePoolSize());
92 }
93
94 }