View Javadoc
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.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   * The Websocket Stomp Configuration.
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          // By default, messages from the application to the message broker are sentsynchronously.
52          registry.configureBrokerChannel().taskExecutor().corePoolSize(properties.getChannelBrokerCorePoolSize());
53          // The default cache limit there is 1024.
54          registry.setCacheLimit(properties.getBrokerRegistryCacheLimit());
55      }
56  
57      @Override
58      public void registerStompEndpoints(final StompEndpointRegistry registry) {
59          registry.addEndpoint(properties.getWebsocketEndpoints()); // normal WebSocket
60  
61          if (properties.isWithSockJs()) {
62              registry.addEndpoint(properties.getWebsocketEndpoints()).withSockJS(); // SockJs Legacy support.
63          }
64      }
65  
66      @Override
67      public void configureWebSocketTransport(final WebSocketTransportRegistration registry) {
68          if (properties.getMessageSizeLimit() != null) {
69              registry.setMessageSizeLimit(properties.getMessageSizeLimit()); // The default value is 64K (i.e. 64 * 1024).
70          }
71          if (properties.getSendBufferSizeLimit() != null) {
72              registry.setSendBufferSizeLimit(properties.getSendBufferSizeLimit()); // The default value is 512K (i.e. 512 * 1024).
73          }
74          if (properties.getSendTimeLimit() != null) {
75              registry.setSendTimeLimit(properties.getSendTimeLimit()); // The default value is 10 seconds (i.e. 10 * 10000).
76          }
77          if (properties.getTimeToFirstMessage() != null) {
78              registry.setTimeToFirstMessage(1000); // The default is set to 60,000 (1 minute).
79          }
80      }
81  
82      @Override
83      public void configureClientInboundChannel(ChannelRegistration registration) {
84          // By default the channel is backed by a thread pool of size 1.
85          registration.taskExecutor().corePoolSize(properties.getChannelInboundCorePoolSize());
86      }
87  
88      @Override
89      public void configureClientOutboundChannel(ChannelRegistration registration) {
90          // By default the channel is backed by a thread pool of size 1.
91          registration.taskExecutor().corePoolSize(properties.getChannelOutboundCorePoolSize());
92      }
93  
94  }