001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.socket.config.annotation; 018 019import org.springframework.context.annotation.Bean; 020import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; 021import org.springframework.web.servlet.HandlerMapping; 022 023/** 024 * Configuration support for WebSocket request handling. 025 * 026 * @author Rossen Stoyanchev 027 * @since 4.0 028 */ 029public class WebSocketConfigurationSupport { 030 031 @Bean 032 public HandlerMapping webSocketHandlerMapping() { 033 ServletWebSocketHandlerRegistry registry = new ServletWebSocketHandlerRegistry(defaultSockJsTaskScheduler()); 034 registerWebSocketHandlers(registry); 035 return registry.getHandlerMapping(); 036 } 037 038 protected void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { 039 } 040 041 /** 042 * The default TaskScheduler to use if none is configured via 043 * {@link SockJsServiceRegistration#setTaskScheduler}, i.e. 044 * <pre class="code"> 045 * @Configuration 046 * @EnableWebSocket 047 * public class WebSocketConfig implements WebSocketConfigurer { 048 * 049 * public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { 050 * registry.addHandler(myWsHandler(), "/echo").withSockJS().setTaskScheduler(myScheduler()); 051 * } 052 * 053 * // ... 054 * } 055 * </pre> 056 */ 057 @Bean 058 public ThreadPoolTaskScheduler defaultSockJsTaskScheduler() { 059 ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); 060 scheduler.setThreadNamePrefix("SockJS-"); 061 scheduler.setPoolSize(Runtime.getRuntime().availableProcessors()); 062 scheduler.setRemoveOnCancelPolicy(true); 063 return scheduler; 064 } 065 066}