title: CometD server throwing exception and not pushing all the messages at a time
body: <p>I am getting the following exception in Tomcat 7.0 when I start the CometD. If I try to push the messages it is taking more time sometimes 6000 ms to push the message and it pushes the message two at a time not all at a time. I tried to push 10 to 200 but the situation doesn't changed. I already included jetty-websocket-7.6.10.v20130312.jar and jetty-server-7.6.13.v20130916.jar in CometD server application but still I am getting this exception.</p>
  
  <p><strong>Exception:</strong></p>
  
  <pre><code>23 Oct, 2013 10:12:45 AM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [cometd] in context with path [/Samp] threw   exception
  java.lang.NullPointerException
  at org.eclipse.jetty.websocket.WebSocketFactory.upgrade(WebSocketFactory.java:238)
  at org.eclipse.jetty.websocket.WebSocketFactory.acceptWebSocket(WebSocketFactory.java:396)
  at org.cometd.websocket.server.WebSocketTransport.handle(WebSocketTransport.java:157)
  at org.cometd.server.CometdServlet.service(CometdServlet.java:166)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:247)
  at org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:210)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:662)
  
  23 Oct, 2013 10:12:46 AM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [cometd] in context with path [/Samp] threw exception
  java.lang.IllegalStateException: Not supported.
  at org.apache.catalina.connector.Request.startAsync(Request.java:1664)
  at org.apache.catalina.connector.Request.startAsync(Request.java:1657)
  at org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1022)
  at org.eclipse.jetty.continuation.Servlet3Continuation.suspend(Servlet3Continuation.java:189)
  at org.cometd.server.transport.LongPollingTransport.handle(LongPollingTransport.java:289)
  at org.cometd.server.CometdServlet.service(CometdServlet.java:166)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.eclipse.jetty.servlets.CrossOriginFilter.handle(CrossOriginFilter.java:247)
  at org.eclipse.jetty.servlets.CrossOriginFilter.doFilter(CrossOriginFilter.java:210)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:662) 
  </code></pre>
  
  <p><strong>service class:</strong></p>
  
  <pre><code>@Service
  public class HelloService
  {
  @Inject
  private BayeuxServer bayeux;
  @Session
  private ServerSession serverSession;
  
  private static int msgCount = 0;
  
  private  static Logger log;
  
  @Listener("/service/java")
  public void processMsgFromJava(ServerSession remote, ServerMessage.Mutable message)
  {
      msgCount++;
  
      String msgId = "EVENT-"+msgCount;
      Date dt = Calendar.getInstance().getTime();
  
      Map&lt;String, Object&gt; input = message.getDataAsMap();
      String eventId = (String)input.get("eventID");
  
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
  
      String channelName = "/java/test";
      // Initialize the channel, making it persistent and lazy
      bayeux.createIfAbsent(channelName, new ConfigurableServerChannel.Initializer(){
          public void configureChannel(ConfigurableServerChannel channel){
              channel.setPersistent(false);
              channel.setLazy(false);
          }
      });
  
      // Publish to all subscribers
      ServerChannel channel = bayeux.getChannel(channelName);
      input.put("ms", System.currentTimeMillis());
      channel.publish(serverSession, input, null);
      log.info("Msg published 111111111111........"+msgId);
      System.out.println(channel.isPersistent());
   }
     }
  </code></pre>
  
  <p><strong>Client class:</strong></p>
  
  <pre><code>private ClientTransport transport= null;
  private BayeuxClient client = null;
  
  private ClientSessionChannel clientSession = null;
  public static int msgCount = 0;
  
  public boolean pushToComet(String cometDURL, String subscriberChannel, Map&lt;String,Object&gt; msgToPublish) throws Exception{
      boolean isSuccess = true;
  
      try {
  
          Map&lt;String, Object&gt; options = new HashMap&lt;String, Object&gt;();
  
          /* Prepare the WebSocket transport*/
          WebSocketClientFactory wsFactory = new WebSocketClientFactory();
          wsFactory.start();
          ClientTransport wsTransport = new WebSocketTransport(null, wsFactory, null);
          wsTransport.setOption("maxNetworkDelay", 0);
          wsTransport.setOption("connectTimeout", 10000);
          wsTransport.setOption("idleTimeout", 10000);
          wsTransport.setOption("maxMessageSize", 8192);
  
          transport = LongPollingTransport.create(options);
      //  transport.setDebugEnabled(true);
          transport.setOption("maxNetworkDelay", 0);
  
          client = new BayeuxClient(cometDURL,wsTransport, transport);
          //client.setDebugEnabled(true);
  
          client.handshake();
  
          boolean handshaken = client.waitFor(2000, BayeuxClient.State.CONNECTED);
          if(handshaken){
              msgCount++;
              clientSession = client.getChannel(subscriberChannel);
              clientSession.publish(msgToPublish);
          }else{
              isSuccess=false;
              System.out.println("CommetD not connected????????");
          }
      } catch (Exception e) {
          isSuccess=false;
          e.printStackTrace();
      }finally{
  //          close();
      }
      return isSuccess;
  }
  
  private void close(){
          clientSession.release();
          client.disconnect();
          client.waitFor(1000, BayeuxClient.State.DISCONNECTED);
          transport.terminate();
  
  }
  
  public void closeConn(){
      client.disconnect();
      client.waitFor(1000, BayeuxClient.State.DISCONNECTED);
      transport.terminate();
  }
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5"&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;cometd&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.cometd.annotation.AnnotationCometdServlet&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;services&lt;/param-name&gt;
          &lt;param-value&gt;galaxy_comet.HelloService&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;maxLazyTimeout&lt;/param-name&gt;
          &lt;param-value&gt;-1&lt;/param-value&gt;
      &lt;/init-param&gt;
       &lt;init-param&gt;
          &lt;param-name&gt;timeout&lt;/param-name&gt;
          &lt;param-value&gt;2000&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;interval&lt;/param-name&gt;
          &lt;param-value&gt;5000&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;maxInterval&lt;/param-name&gt;
          &lt;param-value&gt;10000&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;maxQueue&lt;/param-name&gt;
          &lt;param-value&gt;-1&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;metaConnectDeliverOnly&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
     &lt;!--  &lt;init-param&gt;
          &lt;param-name&gt;multiFrameInterval&lt;/param-name&gt;
          &lt;param-value&gt;200&lt;/param-value&gt;
      &lt;/init-param&gt; --&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  
  &lt;/servlet&gt;
  &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;filter&gt;
      &lt;filter-name&gt;cross-origin&lt;/filter-name&gt;
      &lt;filter-class&gt;org.eclipse.jetty.servlets.CrossOriginFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;cross-origin&lt;/filter-name&gt;
      &lt;url-pattern&gt;/cometd/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Javascript in jsp</strong></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 _metaHandshake(handshake)
      {
          if (handshake.successful === true)
          {
              cometd.batch(function()
              {
                  cometd.subscribe('/java/test', function(message)
                  {
                      var d = new Date();
                      $('#body').append('&lt;div&gt;Server Says: ' + message.data.eventID + ':'+ (d.getTime()-message.data.ms) + '&lt;/div&gt;');
                  });
              });
          }
      }
  
      // Disconnect when the page unloads
      $(window).unload(function()
      {
          cometd.disconnect(true);
      });
  
      var cometURL = "http://localhost:8080/cometd1/cometd";
      cometd.configure({
          url: cometURL,
          logLevel: 'debug'
      });
  
      cometd.addListener('/meta/handshake', _metaHandshake);
      cometd.addListener('/meta/connect', _metaConnect);
  
      cometd.handshake();
  });
  })(jQuery);
  </code></pre>
  
  <p>Always the console message is printing as false  for channel is persistent().</p>
  
  <p>I am getting NullPointerException if I call close() in finally of client class.</p>
  
  <p>I am not able to get the results as mentioned in the benchmarks. I am not sure where I am going wrong. I even made the channel.setLazy(false) But still it is taking time. And also if more than one client is subscribed to the same channel then the message push time at these clients is different (for e.g. in first client if it is taking 2 seconds and in the second client it is taking 4 seconds to publish and so on). Means it is not broadcasting the messages at a time. Why is this so. I tried without websocket transportation. That time also message is getting delayed. Now when I am trying to use both websocket and longpolling I am getting this problem. Any help and guidance in this regard will help me lot.</p>