尝试实现 websockets 时出现无法启动 bean 'subProtocolWebSocketHandler' 异常 [英] Getting a Failed to start bean 'subProtocolWebSocketHandler' exception while trying to implement websockets

查看:146
本文介绍了尝试实现 websockets 时出现无法启动 bean 'subProtocolWebSocketHandler' 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在 此处 中学习带有 Spring 4 教程的 websocket.我让它在客户端调用时服务器响应的地方工作.

I am going through the websockets with Spring 4 tutorial here. I have it working where the server responds when client calls.

但是,我的用例是让服务器推送消息而无需客户端调用.所以我在网上查找了如何做到这一点,并遇到了一个很少 帖子 那个 说我需要使用一种叫做 的东西SimpMessagingTemplate.所以我更改了代码以使用 SimpMessagingTemplate.

However, my use case is having the server push the messages without client having to call. So I looked up online on ways to do that and came across a few posts that said that I needed to use something called SimpMessagingTemplate. So I changed the code to use the SimpMessagingTemplate.

现在我在服务器控制台中得到这个异常:

Now I get this exception in the server console:

04-Jun-2016 22:49:24.093 SEVERE [ContainerBackgroundProcessor[StandardEngine[Catalina]]] org.springframework.web.context.ContextLoader.initWebApplicationContext Context initialization failed
 org.springframework.context.ApplicationContextException: Failed to start bean 'subProtocolWebSocketHandler'; nested exception is java.lang.IllegalArgumentException: No handlers
        at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:176)
        at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:51)
        at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:346)
        at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:149)
        at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:112)
        at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:852)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4809)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
        at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3827)
        at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:291)
        at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5608)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
        at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: No handlers
        at org.springframework.util.Assert.isTrue(Assert.java:68)
        at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.start(SubProtocolWebSocketHandler.java:251)
        at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173)
        ... 20 more

我找到了这篇文章这篇文章 有同样的问题,但他们都没有发布真正的解决方案.

I found this post and this post with the same issue but none of them have a real solution posted.

我的 websocket 控制器:

My websocket controller:

@Controller
public class WebSockController {

    private SimpMessagingTemplate template;

    @Autowired
    public WebSockController(SimpMessagingTemplate template) {
        this.template = template;
    }

    @MessageMapping("/hello")    
    public void greeting(GreetingsFromClient message) throws Exception {
        String[] messages = {"message1","message2","message3","message4","message5"};
        for(int i=0 ; i<messages.length ; i++){
            Thread.sleep(3000); // simulated delay
            this.template.convertAndSend("/topic/greetings", new GreetingsFromServer("Hello, " + messages[i] + "!"));
        }        
    }
}

我的 websocket 配置:

My websocket config:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig  implements WebSocketMessageBrokerConfigurer  {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configureClientOutboundChannel(ChannelRegistration arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean configureMessageConverters(List<MessageConverter> arg0) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration arg0) {
        // TODO Auto-generated method stub

    }
}

此外,我必须将 org.springframework.web.socket.config 添加到组件扫描中,因为如果没有这个,我会得到一个自动连接异常 (ref).

Also I had to add org.springframework.web.socket.config to component scan because without this I would get an auto-wiring exception (ref).

我使用的是 Spring 4.2.5 和 Tomcat 8.

I am using Spring 4.2.5 and Tomcat 8.

感谢任何帮助.如果您需要更多信息,请告诉我.谢谢.

Any help is appreciated. Please let me know if you need more info. Thanks.

推荐答案

在解决这个问题几个小时后,我通过将 websocket 配置添加到 rootconfig 中解决了这个问题.

After struggling with this problem for a few hours, I resolved this by adding the websocket config to the rootconfig.

@Configuration
@ComponentScan({ "xxxxxx.xxxxxxxxx.controllers" })
@Import({SecurityConfig.class, WebSocketConfig.class, HibernateConfig.class})
public class RootConfig {

}

我的应用程序初始化器,它使用根配置.

My application initializer that uses the root config.

public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

这篇关于尝试实现 websockets 时出现无法启动 bean 'subProtocolWebSocketHandler' 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆