I want to use Cometd in my project and tried the Jetty 8 version of it on a Tomcat 7 server. It works but the message is being sent twice.
To check if it had something to do with the rest of the project I set up a stand-alone project with only this functionality and I still get the message twice.
I can not find out why that might be.
There are 5 files in the project:
- pom.xml
- web.xml
- index.html
- BayeuxInitializer.java
- FormDataService.java
Here is the code for each of the files:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Sertal Vision</display-name> <servlet> <servlet-name>cometd</servlet-name> <servlet-class>org.cometd.server.CometdServlet</servlet-class> <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> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>bayeux-init</servlet-name> <servlet-class>ch.sertal.server.BayeuxInitializer</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cometd</servlet-name> <url-pattern>/cometd/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>bayeux-init</servlet-name> <url-pattern>/bayeux/servlet/*</url-pattern> </servlet-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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script> <script type="text/javascript"> var subscription = undefined; dojo.addOnLoad( function () { dojo.require( "dojox.cometd" ); dojo.require("dojox.timing"); // Disconnect when the page unloads dojo.addOnUnload( function () { dojox.cometd.disconnect( true ); } ); var cometURL = "cometd"; dojox.cometd.init( cometURL ); var subscription = dojox.cometd.subscribe( "/sertal/formData", function ( message ) { dojo.create( "li", { innerHTML:message.data.count }, dojo.byId( "message-list" ) ); } ); i = 1; var t = new dojox.timing.Timer(); t.setInterval( 1000 ); t.onTick = function() { dojox.cometd.publish( '/sertal/formData', { count: i++ } ); }; t.start(); } ); </script> </head> <body> <div> <ul id="message-list"/> </div> </body> </html>
BayeuxInitializer.java
package ch.sertal.server; import org.cometd.bayeux.server.BayeuxServer; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public class BayeuxInitializer extends GenericServlet { private static final long serialVersionUID = -9089442901563633963L; @Override public void init() throws ServletException { BayeuxServer bayeux = ( BayeuxServer ) getServletContext().getAttribute( BayeuxServer.ATTRIBUTE ); // new HelloService( bayeux ); new FormDataService( bayeux, getServletContext() ); } @Override public void service( ServletRequest servletRequest, ServletResponse servletResponse ) throws ServletException, IOException { throw new ServletException( "sorry :-)" ); } }
FormDataService.java
package ch.sertal.server; import org.cometd.bayeux.Message; import org.cometd.bayeux.server.BayeuxServer; import org.cometd.bayeux.server.ServerSession; import org.cometd.server.AbstractService; import javax.servlet.ServletContext; import java.util.Map; /** * Created by IntelliJ IDEA. * User: micha.roon * Date: 1/16/12 * Time: 12:25 PM * To change this template use File | Settings | File Templates. */ public class FormDataService extends AbstractService { private ServletContext context = null; public FormDataService( BayeuxServer bayeux, ServletContext context ) { super( bayeux, "hello" ); this.context = context; addService( "/sertal/formData", "processFormData" ); System.out.println( "New FormDataService" ); } public void processFormData( final ServerSession remote, Message message ) { Map<String, Object> input = message.getDataAsMap(); remote.deliver( getServerSession(), "/sertal/formData", input, null ); } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>BayeuxTest</groupId> <artifactId>BayeuxTest</artifactId> <version>1.0</version> <build> <plugins> <!--compiler plugin--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <username>${tomcat.username}</username> <password>${tomcat.password}</password> <url>${tomcat.manager}</url> <path>${tomcat.context}</path> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!--for cometd support--> <dependency> <groupId>org.cometd.java</groupId> <artifactId>bayeux-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.cometd.javascript</groupId> <artifactId>cometd-javascript-jquery</artifactId> <version>2.3.1</version> <type>war</type> </dependency> <dependency> <groupId>org.cometd.java</groupId> <artifactId>cometd-java-server</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlets</artifactId> <version>8.0.4.v20111024</version> <exclusions> <exclusion> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
When you are using a normal channel (/sertal/formData) all published posts will be sent to all subscribed clients, in your case that's the browser itself and the Bayeux service. You will therefore receive two answers: one is your published post, the other one is the response from your service.
Solution: you don't want to
publish
your formData, you want itdeliver
ed:There are other solutions, e.g. using a service channel instead of the normal channel. See this CometD FAQ Entry for further explanations.
UPDATE:
I withheld another change necessary to make this work on my machine:
dojo.require
needs to be called outside of theaddOnLoad
handler.I have been using dojo v1.7.1.