zoukankan      html  css  js  c++  java
  • spring websocket tomcat was websphere9.0 Multiple Endpoints may not be deployed to the same path

    网上说给 去掉 @Configuration , 注释掉 ServerEndpointExporter ,但是  不同环境 不可能 代码乱搞,所以明显是不行的。

    下面贴代码:  主要在注册bean时候 ,做了个判断  @Conditional(value=WebSocketCondition.class)

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Conditional;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    import org.springframework.web.socket.server.standard.ServerEndpointExporter;
    import org.springframework.web.socket.server.standard.ServerEndpointRegistration;
    
    @Configuration
    //@Component
    public class EndpointConfig {
        
        @Bean
        public ServerEndpointExporter endpointExporter() {
            return new ServerEndpointExporter();
        }
        /**
         * 在websphere种,无法完成自动注册websocket。
         * 判断WebSocketCondition 是否注册,未注册则执行
         * @return
         */
        @Conditional(value=WebSocketCondition.class)
        @Bean
        public EchoAnnotatedEndpoint echoAnnotatedEndpoint() {
            return new EchoAnnotatedEndpoint(echoService());
        }
    
        @Bean
        public EchoService echoService() {
            return new DefaultEchoService();
        }
    }
    WebSocketCondition
    import javax.servlet.ServletContext;
    
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    import org.springframework.web.context.support.XmlWebApplicationContext;
    
    public class WebSocketCondition implements Condition{
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            XmlWebApplicationContext resourceLoader = (XmlWebApplicationContext) context.getResourceLoader();
            ServletContext servletContext = resourceLoader.getServletContext();
            String serverInfo = servletContext.getServerInfo();
            //tomcat 容器  ,这里可以根据实际业务,自己判断
            if(serverInfo != null && serverInfo.contains("Tomcat")) {
                return false;
            }
            return true;
        }
    
    }

     OK, 问题解决, 说白了,就是bean注册了两次而已

  • 相关阅读:
    Android捕捉错误try catch 的简单使用
    ubuntu下安装lua和tolua++
    mosh安装与使用
    三,温习redis持久化解析与配置
    二,温习redis(工具命令使用)
    一,温习Redis (详解从安装到配置)
    报错!-> CPU100%-但是找不到使用cpu的进程
    linux安全---防火墙(iptables)理论解析
    Mysql8.0版二进制安装(my.cnf文件灵活编写)
    ansible实现template管理nginx
  • 原文地址:https://www.cnblogs.com/bignew/p/13552752.html
Copyright © 2011-2022 走看看