I migrated my cometd from 2 to 3 and come across some problem:
Handshake never succeed, and there's no error messages.
I tried all I can do but still in vain.
Here's my Code:
ConfigurationServlet:
@Override
public void init() throws ServletException {
logger.info("start prepare echoService...");
// Grab the Bayeux object
BayeuxServer bayeuxServer = (BayeuxServer) getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
bayeuxServer.createChannelIfAbsent(NODE_CHANNEL, new ServerChannel.Initializer() { //1030829 change createIfAbsend to createChannelIfAbsent
@Override
public void configureChannel(ConfigurableServerChannel channel) {
channel.setPersistent(true);
}
});
EchoService echoService = new EchoService(bayeuxServer);
logger.info("...prepare echoService done");
}
EchoService:
public EchoService(BayeuxServer bayeuxServer) {
super(bayeuxServer, "echo");
//for what?
logger.info("=============enter echoService constructor=============");
addService("/echo", "processEcho");
addService(CometdProperties.COMETD_CHANNEL_WEB, "processReq");
logger.info("=============leaving echoService constructor=============");
isEchoServiceStarted = true;
}
public void processEcho(ServerSession remote, ServerMessage message) {
remote.deliver(getServerSession(), "/echo", message.getData());
}
public void processReq(ServerSession remote, ServerMessage message) {
//do something...
}
InitConnect in my client:
public boolean initConnect(String channel, ClientSessionChannel.MessageListener ml) {
HClogger.info("=============initConnect=============Listener:" + ml);
HttpClient httpClient = new HttpClient();
try {
httpClient.start();
} catch (Exception ex) {
HClogger.error(ex.toString());
if(connCnt<10){//Retry 9 times.
connCnt++;
return initConnect(channel,ml);
}
return false;
}
HClogger.info("*********************************try connect to server...");
Map<String, Object> options = new HashMap<>();
ClientTransport longPool = new LongPollingTransport(options, httpClient);
bayeuxClient = new BayeuxClient(CometdProperties.COMETD_DEFAULTURL, longPool);
bayeuxClient.handshake();
boolean handshaken = bayeuxClient.waitFor(10000, BayeuxClient.State.CONNECTED);
if (handshaken) {
HClogger.info("connected to server ok...");
bayeuxClient.getChannel(channel).subscribe(ml);
HClogger.info("subscribed to " + channel + " channel.");
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
HClogger.error("Error when sleep after subscribe.", ex);
}
HClogger.info("=============HandleClient subscribe done.=============");
return true;
} else {
HClogger.info("=============HandleClient handshake fail.=============");
if(connCnt<10){//Retry 9 times.
connCnt++;
return initConnect(channel,ml);
}
return false;
}
}
Web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>cometd</servlet-name>
<servlet-class>org.cometd.server.CometDServlet</servlet-class>
<init-param>
<param-name>timeout</param-name>
<param-value>300000</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet>
<servlet-name>ConfigurationServlet</servlet-name>
<servlet-class>org.astri.ims.ccs.webcommand.ConfigurationServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ConfigurationServlet</servlet-name>
<url-pattern>/ConfigurationServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>cometd</servlet-name>
<url-pattern>/cometd/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
And here's what I add in my lib:
Is there anything wrong in my code or configuration?
I added a listener to the meta_channel, and I got this while handshaking:
{"id":"3","failure":{"message":{"id":"3","supportedConnectionTypes":["long-polling"],"channel":"/meta/handshake","version":"1.0"},"exception":"java.util.concurrent.TimeoutException: Total timeout elapsed","connectionType":"long-polling"},"subscription":null,"successful":false,"channel":"/meta/handshake"}
It turns out there is a bug in glassfish4.
Thanks for @sbordet who investigate and find out what's going on.
For detail information, please visit here.
For those who want to use CometD3, Jetty is suggested container.
For those who have no choice but to use glassfish, Cometd2.X works.
I've tried with cometd2.9.1, 3.0.0 and 3.0.1 in glassfish4, the result is
2.9.1 works in glassfish4 while 3.0.0 and 3.0.1 not.