WebSocketMessageBrokerConfigurationSupport.java 5.8 KB
Newer Older
1
/*
2
 * Copyright 2002-2014 the original author or authors.
3 4 5 6 7
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.web.socket.config.annotation;
18

R
Rossen Stoyanchev 已提交
19
import org.springframework.beans.factory.config.CustomScopeConfigurer;
20
import org.springframework.context.annotation.Bean;
21 22
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
R
Rossen Stoyanchev 已提交
23
import org.springframework.messaging.simp.SimpSessionScope;
24
import org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler;
25
import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration;
26
import org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler;
27 28 29
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.socket.WebSocketHandler;
30
import org.springframework.web.socket.config.WebSocketMessageBrokerStats;
31
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory;
32 33 34 35 36
import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler;

/**
 * Extends {@link AbstractMessageBrokerConfiguration} and adds configuration for
 * receiving and responding to STOMP messages from WebSocket clients.
37 38
 *
 * <p>Typically used in conjunction with
39 40 41 42
 * {@link EnableWebSocketMessageBroker @EnableWebSocketMessageBroker} but can
 * also be extended directly.
 *
 * @author Rossen Stoyanchev
43
 * @author Artem Bilan
44 45 46 47
 * @since 4.0
 */
public abstract class WebSocketMessageBrokerConfigurationSupport extends AbstractMessageBrokerConfiguration {

48 49 50
	private WebSocketTransportRegistration transportRegistration;


51 52
	@Bean
	public HandlerMapping stompWebSocketHandlerMapping() {
53 54 55
		WebSocketHandler handler = subProtocolWebSocketHandler();
		handler = decorateWebSocketHandler(handler);
		WebMvcStompEndpointRegistry registry = new WebMvcStompEndpointRegistry(handler,
R
Polish  
Rossen Stoyanchev 已提交
56
				getTransportRegistration(), userSessionRegistry(), messageBrokerSockJsTaskScheduler());
57
		registry.setApplicationContext(getApplicationContext());
58 59 60 61 62 63
		registerStompEndpoints(registry);
		return registry.getHandlerMapping();
	}

	@Bean
	public WebSocketHandler subProtocolWebSocketHandler() {
64
		return new SubProtocolWebSocketHandler(clientInboundChannel(), clientOutboundChannel());
65 66
	}

67 68 69 70 71 72 73
	protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
		for (WebSocketHandlerDecoratorFactory factory : getTransportRegistration().getDecoratorFactories()) {
			handler = factory.decorate(handler);
		}
		return handler;
	}

74 75 76 77 78 79 80 81 82 83 84
	protected final WebSocketTransportRegistration getTransportRegistration() {
		if (this.transportRegistration == null) {
			this.transportRegistration = new WebSocketTransportRegistration();
			configureWebSocketTransport(this.transportRegistration);
		}
		return this.transportRegistration;
	}

	protected void configureWebSocketTransport(WebSocketTransportRegistration registry) {
	}

R
Rossen Stoyanchev 已提交
85 86
	protected abstract void registerStompEndpoints(StompEndpointRegistry registry);

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	/**
	 * The default TaskScheduler to use if none is configured via
	 * {@link SockJsServiceRegistration#setTaskScheduler(org.springframework.scheduling.TaskScheduler)}, i.e.
	 * <pre class="code">
	 * &#064;Configuration
	 * &#064;EnableWebSocketMessageBroker
	 * public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
	 *
	 *   public void registerStompEndpoints(StompEndpointRegistry registry) {
	 *     registry.addEndpoint("/stomp").withSockJS().setTaskScheduler(myScheduler());
	 *   }
	 *
	 *   // ...
	 * }
	 * </pre>
	 */
	@Bean
	public ThreadPoolTaskScheduler messageBrokerSockJsTaskScheduler() {
105
		ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
106
		scheduler.setThreadNamePrefix("MessageBrokerSockJS-");
107
		scheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
R
Rossen Stoyanchev 已提交
108
		scheduler.setRemoveOnCancelPolicy(true);
109 110 111
		return scheduler;
	}

R
Rossen Stoyanchev 已提交
112 113 114
	@Bean
	public static CustomScopeConfigurer webSocketScopeConfigurer() {
		CustomScopeConfigurer configurer = new CustomScopeConfigurer();
115
		configurer.addScope("websocket", new SimpSessionScope());
R
Rossen Stoyanchev 已提交
116 117
		return configurer;
	}
118

119 120
	@Bean
	public WebSocketMessageBrokerStats webSocketMessageBrokerStats() {
121 122 123
		AbstractBrokerMessageHandler relayBean = stompBrokerRelayMessageHandler();
		StompBrokerRelayMessageHandler brokerRelay = (relayBean instanceof StompBrokerRelayMessageHandler ?
				(StompBrokerRelayMessageHandler) relayBean : null);
124 125 126 127 128 129 130 131 132 133 134 135 136

		// Ensure STOMP endpoints are registered
		stompWebSocketHandlerMapping();

		WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
		stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler());
		stats.setStompBrokerRelay(brokerRelay);
		stats.setInboundChannelExecutor(clientInboundChannelExecutor());
		stats.setOutboundChannelExecutor(clientOutboundChannelExecutor());
		stats.setSockJsTaskScheduler(messageBrokerSockJsTaskScheduler());
		return stats;
	}

137 138 139 140 141 142 143 144
	@Override
	protected MappingJackson2MessageConverter createJacksonConverter() {
		MappingJackson2MessageConverter messageConverter = super.createJacksonConverter();
		// Use Jackson builder in order to have JSR-310 and Joda-Time modules registered automatically
		messageConverter.setObjectMapper(Jackson2ObjectMapperBuilder.json().build());
		return messageConverter;
	}

145
}