@Before public void setup() { this.serverContainer = mock(ServerContainer.class); this.servletContext = new MockServletContext(); this.servletContext.setAttribute("javax.websocket.server.ServerContainer", this.serverContainer); this.webAppContext = new AnnotationConfigWebApplicationContext(); this.webAppContext.register(Config.class); this.webAppContext.setServletContext(this.servletContext); this.webAppContext.refresh(); this.exporter = new ServerEndpointExporter(); }
@Override public void afterSingletonsInstantiated() { registerEndpoints(); }
/** * Actually register the endpoints. Called by {@link #afterSingletonsInstantiated()}. */ protected void registerEndpoints() { Set<Class<?>> endpointClasses = new LinkedHashSet<>(); if (this.annotatedEndpointClasses != null) { endpointClasses.addAll(this.annotatedEndpointClasses); } ApplicationContext context = getApplicationContext(); if (context != null) { String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class); for (String beanName : endpointBeanNames) { endpointClasses.add(context.getType(beanName)); } } for (Class<?> endpointClass : endpointClasses) { registerEndpoint(endpointClass); } if (context != null) { Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class); for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) { registerEndpoint(endpointConfig); } } }
@Override public void afterPropertiesSet() { Assert.state(getServerContainer() != null, "javax.websocket.server.ServerContainer not available"); }
private void registerEndpoint(ServerEndpointConfig endpointConfig) { ServerContainer serverContainer = getServerContainer(); Assert.state(serverContainer != null, "No ServerContainer set"); try { if (logger.isDebugEnabled()) { logger.debug("Registering ServerEndpointConfig: " + endpointConfig); } serverContainer.addEndpoint(endpointConfig); } catch (DeploymentException ex) { throw new IllegalStateException("Failed to register ServerEndpointConfig: " + endpointConfig, ex); } }
/** * 用于扫描和注册所有携带ServerEndPoint注解的实例。 * <p> * PS:若部署到外部容器 则无需提供此类。 */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
@Override public void afterSingletonsInstantiated() { registerEndpoints(); }
/** * Actually register the endpoints. Called by {@link #afterSingletonsInstantiated()}. */ protected void registerEndpoints() { Set<Class<?>> endpointClasses = new LinkedHashSet<>(); if (this.annotatedEndpointClasses != null) { endpointClasses.addAll(this.annotatedEndpointClasses); } ApplicationContext context = getApplicationContext(); if (context != null) { String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class); for (String beanName : endpointBeanNames) { endpointClasses.add(context.getType(beanName)); } } for (Class<?> endpointClass : endpointClasses) { registerEndpoint(endpointClass); } if (context != null) { Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class); for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) { registerEndpoint(endpointConfig); } } }
private void registerEndpoint(Class<?> endpointClass) { ServerContainer serverContainer = getServerContainer(); Assert.state(serverContainer != null, "No ServerContainer set. Most likely the server's own WebSocket ServletContainerInitializer " + "has not run yet. Was the Spring ApplicationContext refreshed through a " + "org.springframework.web.context.ContextLoaderListener, " + "i.e. after the ServletContext has been fully initialized?"); try { if (logger.isDebugEnabled()) { logger.debug("Registering @ServerEndpoint class: " + endpointClass); } serverContainer.addEndpoint(endpointClass); } catch (DeploymentException ex) { throw new IllegalStateException("Failed to register @ServerEndpoint class: " + endpointClass, ex); } }
@Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }
@Override public void afterSingletonsInstantiated() { registerEndpoints(); }
/** * Actually register the endpoints. Called by {@link #afterSingletonsInstantiated()}. */ protected void registerEndpoints() { Set<Class<?>> endpointClasses = new LinkedHashSet<>(); if (this.annotatedEndpointClasses != null) { endpointClasses.addAll(this.annotatedEndpointClasses); } ApplicationContext context = getApplicationContext(); if (context != null) { String[] endpointBeanNames = context.getBeanNamesForAnnotation(ServerEndpoint.class); for (String beanName : endpointBeanNames) { endpointClasses.add(context.getType(beanName)); } } for (Class<?> endpointClass : endpointClasses) { registerEndpoint(endpointClass); } if (context != null) { Map<String, ServerEndpointConfig> endpointConfigMap = context.getBeansOfType(ServerEndpointConfig.class); for (ServerEndpointConfig endpointConfig : endpointConfigMap.values()) { registerEndpoint(endpointConfig); } } }
@Override public void afterPropertiesSet() { Assert.state(getServerContainer() != null, "javax.websocket.server.ServerContainer not available"); }
@Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }
@Override protected void registerEndpoints() { super.registerEndpoints(); if (!JSR356WebsocketInitializer.isAtmosphereAvailable()) { return; } if (getServerContainer() == null) { // ServerContainer (i.e. the websocket server provided by Jetty, // Tomcat etc) can be null at this point when running in a real // server (as opposed to embedded). At least Jetty uses a // ServletContainerInitializer to initialize its websocket support // and that one might or might not have been run before this code. // Need to bail out and let JSR356WebsocketInitializer handle it // through its listener when the websocket support is definitely // available. // // This feels like a Spring Boot bug. return; } new JSR356WebsocketInitializer().init(getServletContext()); }
@Override public void afterPropertiesSet() { Assert.state(getServerContainer() != null, "javax.websocket.server.ServerContainer not available"); }
@Bean public ServerEndpointExporter serverEndpointExporter(){ return new ServerEndpointExporter(); } }
@Override protected void registerEndpoints() { super.registerEndpoints(); if (!JSR356WebsocketInitializer.isAtmosphereAvailable()) { return; } if (getServerContainer() == null) { // ServerContainer (i.e. the websocket server provided by Jetty, // Tomcat etc) can be null at this point when running in a real // server (as opposed to embedded). At least Jetty uses a // ServletContainerInitializer to initialize its websocket support // and that one might or might not have been run before this code. // Need to bail out and let JSR356WebsocketInitializer handle it // through its listener when the websocket support is definitely // available. // // This feels like a Spring Boot bug. return; } new JSR356WebsocketInitializer().init(getServletContext()); }
private void registerEndpoint(ServerEndpointConfig endpointConfig) { ServerContainer serverContainer = getServerContainer(); Assert.state(serverContainer != null, "No ServerContainer set"); try { if (logger.isInfoEnabled()) { logger.info("Registering ServerEndpointConfig: " + endpointConfig); } serverContainer.addEndpoint(endpointConfig); } catch (DeploymentException ex) { throw new IllegalStateException("Failed to register ServerEndpointConfig: " + endpointConfig, ex); } }
/** * 会自动注册使用了@ServerEndpoint注解声明的Websocket endpoint * 要注意,如果使用独立的servlet容器, * 而不是直接使用springboot的内置容器, * 就不要注入ServerEndpointExporter,因为它将由容器自己提供和管理。 */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }