Question

I have problems getting tomcat work with cometD and Longpolling.

I see in firebug that all requests are in ms area although the poll should stay for at least 1 second. The Server does not respond in ms time.

Here is what i got in firebug:

8ms 
  dojo.j...ssed.js (Zeile 11610)
  POST http://localhost:8080/chatty/cometd/connect

  200 OK
                4ms 
            dojo.j...ssed.js (Zeile 11610)
        POST http://localhost:8080/chatty/cometd/connect

     200 OK
    2ms 
  dojo.j...ssed.js (Zeile 11610)
    POST http://localhost:8080/chatty/cometd/connect

My web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <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_3_0.xsd"
     version="3.0">

<servlet>
    <servlet-name>cometd</servlet-name>
    <servlet-class>org.cometd.server.CometdServlet</servlet-class>
    <init-param>
        <param-name>transports</param-name>
        <param-value>org.cometd.websocket.server.JSONTransport</param-value>
    </init-param>
    <init-param>
        <param-name>allowedTransports</param-name>
        <param-value>org.cometd.server.transport.LongPollingTransport</param-value>
    </init-param>
    <init-param>
        <param-name>services</param-name>
        <param-value>com.xxxxx.StockPriceService</param-value>
    </init-param>
    <init-param>
        <param-name>timeout</param-name>
        <param-value>60000</param-value>
    </init-param>
    <init-param>
        <param-name>logLevel</param-name>
        <param-value>3</param-value>
    </init-param>
    <init-param>
        <param-name>interval</param-name>
        <param-value>0</param-value>
    </init-param>
    <init-param>
        <param-name>long-polling.multiSessionInterval</param-name>
        <param-value>2000</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
    <servlet-name>cometd</servlet-name>
    <url-pattern>/cometd/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>initializer</servlet-name>
    <servlet-class>com.xxxxx.Initializer</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/cometd/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>continuation</filter-name>
    <filter-class>org.eclipse.jetty.continuation.ContinuationFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>continuation</filter-name>
    <url-pattern>/cometd/*</url-pattern>
</filter-mapping>

my pom: install maven-war-plugin org.cometd.javascript cometd-javascript-dojo org.mortbay.jetty jetty-maven-plugin 10 /

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.cometd.java</groupId>
        <artifactId>bayeux-api</artifactId>
        <version>${cometd-version}</version>
    </dependency>
    <dependency>
        <groupId>org.cometd.javascript</groupId>
        <artifactId>cometd-javascript-dojo</artifactId>
        <version>${cometd-version}</version>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>org.cometd.java</groupId>
        <artifactId>cometd-java-server</artifactId>
        <version>${cometd-version}</version>
    </dependency>
    <dependency>
        <groupId>org.cometd.java</groupId>
        <artifactId>cometd-java-annotations</artifactId>
        <version>${cometd-version}</version>
    </dependency>
    <dependency>
        <groupId>org.cometd.java</groupId>
        <artifactId>cometd-java-client</artifactId>
        <version>${cometd-version}</version>
    </dependency>
    <dependency>
        <groupId>org.cometd.java</groupId>
        <artifactId>cometd-websocket-jetty</artifactId>
        <version>${cometd-version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.cometd.java</groupId>
                <artifactId>cometd-java-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-util</artifactId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-jmx</artifactId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-continuation</artifactId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-websocket</artifactId>
        <version>${jetty-version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlets</artifactId>
        <version>${jetty-version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j-version}</version>
    </dependency>
</dependencies>

I do miss something for the longpoll configuration?

Was it helpful?

Solution 2

Filters in the web.xml must be marked as true For example the org.eclipse.jetty.continuation.ContinuationFilter

OTHER TIPS

Your web.xml is really wrong.

First of all, you don't need to specify the transports <init-param> if you just want to use the long-polling transport. This parameter is for additional transports like websocket. This is explained here.

Parameter allowedTransports specifies transport names, not full qualified class names. This is explained here.

The services parameter is only read by AnnotationCometdServlet, as explained here (see section 6.4.2.3.4).

Look at the primer to setup your initial web application, and follow the tutorials.

I dont know if you solved your problem. I had the same problem using Faye Browser client (JS). In my case started to work when I added a new Transport class in my project and I included it in my web.xml.

See below:

package org.foo.bar.http.transports;

import java.util.Enumeration;

import java.io.IOException;
import java.text.ParseException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cometd.bayeux.server.ServerMessage;
import org.cometd.server.BayeuxServerImpl;
import org.cometd.server.ServerSessionImpl;
import org.cometd.server.transport.LongPollingTransport;


public class CrossOriginLongPollingTransport extends LongPollingTransport
{
    public final static String PREFIX = "long-polling-cross-origin.json";
    public final static String NAME = "cross-origin-long-polling";
    public final static String MIME_TYPE_OPTION = "mimeType";

    private boolean _jsonDebug = false;
    private String _mimeType = "application/json;charset=UTF-8";

    public CrossOriginLongPollingTransport(BayeuxServerImpl bayeux)
    {
        super(bayeux, NAME);
        setOptionPrefix(PREFIX);
    }

    @Override
    protected boolean isAlwaysFlushingAfterHandle()
    {
        return false;
    }

    @Override
    protected void init()
    {
        super.init();
        _jsonDebug = getOption(JSON_DEBUG_OPTION, _jsonDebug);
        _mimeType = getOption(MIME_TYPE_OPTION, _mimeType);
    }

    @Override
    public boolean accept(HttpServletRequest request)
    {
        return "POST".equals(request.getMethod());
    }

    @Override
    protected ServerMessage.Mutable[] parseMessages(HttpServletRequest request) throws IOException, ParseException
    {
        String charset = request.getCharacterEncoding();
        if (charset == null)
            request.setCharacterEncoding("UTF-8");
        String contentType = request.getContentType();
        if (contentType == null || contentType.startsWith("application/json"))
            return parseMessages(request.getReader(), _jsonDebug);
        else if (contentType.startsWith("application/x-www-form-urlencoded")) {
            return parseMessages(request.getParameterValues(MESSAGE_PARAM));
        } else
            throw new IOException("Invalid Content-Type " + contentType);
    }

    @Override
    protected ServletOutputStream beginWrite(HttpServletRequest request, HttpServletResponse response, ServerSessionImpl session) throws IOException
    {
        response.setContentType(_mimeType);
        ServletOutputStream output = response.getOutputStream();
        output.write('[');
        return output;
    }

    @Override
    protected void endWrite(ServletOutputStream output, ServerSessionImpl session) throws IOException
    {
        output.write(']');
        output.close();
    }
}

after, include in your web.xml:

    <init-param>
        <param-name>transports</param-name>
        <param-value>org.cometd.websocket.server.WebSocketTransport,org.foo.bar.http.transports.CrossOriginLongPollingTransport</param-value>
    </init-param>
    <init-param>
        <param-name>allowedTransports</param-name>
        <param-value>cross-origin-long-polling,long-polling,callback-polling,websocket</param-value>
    </init-param>

It worked for me, I hope it could you help too.

Best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top