Dojo, Comet and Jetty
There’s not a great deal of up to date documentation of how to get dojo and cometd working with the Bayeux protocol. This quick blog post should get you up and running, and is based off the “Primer” sample code that you can get here (but you don’t need it for this): http://cometd.org/documentation/howtos/primer
The following code will show you how to set everything up and then publish a message from the webpage. This will kick off a thread in Jetty that publishes an ever-increasing number every 5 seconds that will be shown on screen.
Prereqs:
Jetty 8.x server runtime setup and installed in eclipse (well documented elsewhere)
Dojo 1.7
Create a Dynamic Web Project in eclipse, pointing it at Jetty.
Add to your build path:
bayeux-api-2.3.1.jar
cometd-java-common-2.3.1.jar
cometd-java-server-2.3.1.jar
Create a GenericServlet, calling it test.BayeuxInitialiser, paste in this:
/*
* Copyright (c) 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.ServerChannel;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.DefaultSecurityPolicy;
public class BayeuxInitialiser extends GenericServlet
{
private static final long serialVersionUID = -9089442901563633963L;
public void init() throws ServletException
{
BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
bayeux.setSecurityPolicy(new DefaultSecurityPolicy(){
@Override
public boolean canCreate(BayeuxServer server,
ServerSession session, String channelId,
ServerMessage message) {
return true;
}
@Override
public boolean canHandshake(BayeuxServer server,
ServerSession session, ServerMessage message) {
return true;
}
@Override
public boolean canPublish(BayeuxServer server,
ServerSession session, ServerChannel channel,
ServerMessage message) {
return true;
}
@Override
public boolean canSubscribe(BayeuxServer server,
ServerSession session, ServerChannel channel,
ServerMessage message) {
return true;
}
});
new HelloService(bayeux);
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
throw new ServletException();
}
}
Create a test.HelloService class, and paste in this:
/*
* Copyright (c) 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
import java.util.Map;
import java.util.HashMap;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.AbstractService;
public class HelloService extends AbstractService
{
public HelloService(BayeuxServer bayeux)
{
super(bayeux, "hello");
addService("/service/hello", "processHello");
System.out.println("New HelloService");
}
public void processHello(final ServerSession remote, Message message)
{
Map
Now, in your WebContent directory:
Unzip dojo into js/dojo (so you will have js/dojo/dojo/* js/dojo/dijit/* etc.)
Create index.jsp:
<!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">
dojo.require("dojox.cometd");
dojo.addOnLoad(function() {
// Disconnect when the page unloads
dojo.addOnUnload(function() {
dojox.cometd.disconnect(true);
});
var cometURL = "cometd";
dojox.cometd.init(cometURL);
});
function subscribe() {
dojox.cometd.subscribe("/*", console, "log");
}
function publish() {
dojox.cometd.publish('/service/hello', { name: 'World' })
}
</script>
</head>
<body>
<input type="button" onclick="subscribe()" value="1. Subscribe" />
<input type="button" onclick="publish();" value="2. Publish" />
</body>
</html>
Replace web.xml with this:
<?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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometdServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>initializer</servlet-name>
<servlet-class>test.BayeuxInitialiser</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>
</web-app>
Now point your browser to index.jsp, click the subscribe button, and click the publish button. You should see messages appear on the screen and in your console.
Hope this helps someone!