title: Cometd giving multiple endpoints may not be deployed to same path [/cometd]
body: <p>I am working on a Spring-MVC application in which I am trying to integrate Chat functionality with Cometd. I am getting the error that multiple endpoints cannot be deployed in the same path.</p>
  
  <p>Error log:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cometDInitializer.Processor': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.cometd.bayeux.server.BayeuxServer com.journaldev.spring.chat.CometDInitializer$Processor.bayeuxServer; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bayeuxServer' defined in class path resource [com/journaldev/spring/chat/CometDInitializer.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: javax.websocket.DeploymentException: Multiple Endpoints may not be deployed to the same path [/cometd]
      org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
      org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
  </code></pre>
  
  <p>If someone wants the complete error log, <a href="http://pastebin.com/UVyT03Pa" rel="nofollow noreferrer">Here it is on pastebin</a></p>
  
  <p>CometdInitializer.java:</p>
  
  <pre><code>@Component
  public class CometDInitializer implements ServletContextAware
  {
      private ServletContext servletContext;
  
      @Bean(initMethod = "start", destroyMethod = "stop")
      public BayeuxServer bayeuxServer()
      {
          BayeuxServerImpl bean = new BayeuxServerImpl();
          bean.setTransports(new WebSocketTransport(bean), new JSONTransport(bean), new JSONPTransport(bean));
          servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bean);
          bean.setOption(ServletContext.class.getName(), servletContext);
          bean.setOption("ws.cometdURLMapping", "/cometd/*");
          return bean;
      }
  
      public void setServletContext(ServletContext servletContext)
      {
          this.servletContext = servletContext;
      }
  
      @Component
      public static class Processor implements DestructionAwareBeanPostProcessor
      {
          @Inject
          private BayeuxServer bayeuxServer;
          private ServerAnnotationProcessor processor;
  
          @PostConstruct
          private void init()
          {
              this.processor = new ServerAnnotationProcessor(bayeuxServer);
          }
  
          @PreDestroy
          private void destroy()
          {
          }
  
          public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
          {
              processor.processDependencies(bean);
              processor.processConfigurations(bean);
              processor.processCallbacks(bean);
              return bean;
          }
  
          public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
          {
              return bean;
          }
  
          public void postProcessBeforeDestruction(Object bean, String name) throws BeansException
          {
              processor.deprocessCallbacks(bean);
          }
      }
  }
  </code></pre>
  
  <p>HelloService.java:</p>
  
  <pre><code>@Named
  @Singleton
  @Service("helloService")
  public class HelloService
  {
      @Inject
      private BayeuxServer bayeux;
      @Session
      private ServerSession serverSession;
  
      @PostConstruct
      public void init()
      {
      }
  
      @Listener("/service/hello")
      public void processHello(ServerSession remote, ServerMessage message)
      {
          System.out.println("We recieved a helo msg");
          Map&lt;String, Object&gt; input = message.getDataAsMap();
          String name = (String)input.get("name");
  
          Map&lt;String, Object&gt; output = new HashMap&lt;&gt;();
          output.put("greeting", "Hello, " + name);
          remote.deliver(serverSession, "/hello", output);
      }
  }
  </code></pre>
  
  <p>Application.js:</p>
  
  <pre><code>(function($)
  {
      var cometd = $.cometd;
  
      $(document).ready(function()
      {
          function _connectionEstablished()
          {
              $('#body').append('&lt;div&gt;CometD Connection Established&lt;/div&gt;');
          }
  
          function _connectionBroken()
          {
              $('#body').append('&lt;div&gt;CometD Connection Broken&lt;/div&gt;');
          }
  
          function _connectionClosed()
          {
              $('#body').append('&lt;div&gt;CometD Connection Closed&lt;/div&gt;');
          }
  
          // Function that manages the connection status with the Bayeux server
          var _connected = false;
          function _metaConnect(message)
          {
              if (cometd.isDisconnected())
              {
                  _connected = false;
                  _connectionClosed();
                  return;
              }
  
              var wasConnected = _connected;
              _connected = message.successful === true;
              if (!wasConnected &amp;&amp; _connected)
              {
                  _connectionEstablished();
              }
              else if (wasConnected &amp;&amp; !_connected)
              {
                  _connectionBroken();
              }
          }
  
          // Function invoked when first contacting the server and
          // when the server has lost the state of this client
          function _metaHandshake(handshake)
          {
              if (handshake.successful === true)
              {
                  cometd.batch(function()
                  {
                      cometd.subscribe('/hello', function(message)
                      {
                          $('#body').append('&lt;div&gt;Server Says: ' + message.data.greeting + '&lt;/div&gt;');
                      });
                      // Publish on a service channel since the message is for the server only
                      cometd.publish('/service/hello', { name: 'World' });
                  });
              }
          }
  
          // Disconnect when the page unloads
          $(window).unload(function()
          {
              cometd.disconnect(true);
          });
  
          var cometURL = location.protocol + "//" + location.host + config.contextPath + "/cometd";
          cometd.configure({
              url: cometURL,
              logLevel: 'debug'
          });
  
          cometd.addListener('/meta/handshake', _metaHandshake);
          cometd.addListener('/meta/connect', _metaConnect);
  
          cometd.handshake();
      });
  })(jQuery);
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;servlet-mapping&gt;
          &lt;servlet-name&gt;cometd&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/cometd/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;cometd&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.cometd.server.CometDServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;transports&lt;/param-name&gt;
              &lt;param-value&gt;org.cometd.websocket.server.WebSocketTransport&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;ws.cometdURLMapping&lt;/param-name&gt;
              &lt;param-value&gt;/cometd/*&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;init-param&gt;
              &lt;param-name&gt;logLevel&lt;/param-name&gt;
              &lt;param-value&gt;3&lt;/param-value&gt;
          &lt;/init-param&gt;
  
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  </code></pre>
  
  <p><em>Edit</em></p>
  
  <p>So finally, this was version mis-match from the code from mvn:archtype from cometd. The file below works:</p>
  
  <pre><code>@Component
  public class BayeuxInitializer implements DestructionAwareBeanPostProcessor, ServletContextAware
  {
      private BayeuxServer bayeuxServer;
      private ServerAnnotationProcessor processor;
  
      @Inject
      private void setBayeuxServer(BayeuxServer bayeuxServer)
      {
          this.bayeuxServer = bayeuxServer;
      }
  
      @PostConstruct
      private void init()
      {
          this.processor = new ServerAnnotationProcessor(bayeuxServer);
      }
  
      @PreDestroy
      private void destroy()
      {
      }
  
      public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException
      {
          processor.processDependencies(bean);
          processor.processConfigurations(bean);
          processor.processCallbacks(bean);
          return bean;
      }
  
      public Object postProcessAfterInitialization(Object bean, String name) throws BeansException
      {
          return bean;
      }
  
      public void postProcessBeforeDestruction(Object bean, String name) throws BeansException
      {
          processor.deprocessCallbacks(bean);
      }
  
      @Bean(initMethod = "start", destroyMethod = "stop")
      public BayeuxServer bayeuxServer()
      {
          BayeuxServerImpl bean = new BayeuxServerImpl();
      //    bean.setTransports(new WebSocketTransport(bean), new JSONTransport(bean), new JSONPTransport(bean));
          return bean;
      }
  
      public void setServletContext(ServletContext servletContext)
      {
          servletContext.setAttribute(BayeuxServer.ATTRIBUTE, bayeuxServer);
      }
  }
  </code></pre>