我将我的cometd从2迁移到3并遇到了一些问题:
握手永远不会成功,并且没有错误消息。
我尽我所能,但仍然徒劳无功。
这是我的代码:
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:
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>
以下是我在lib中添加的内容:
我的代码或配置有什么问题吗?
我在meta_channel中添加了一个监听器,我在握手时得到了这个:
{"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"}
答案 0 :(得分:0)
事实证明,glassfish4中存在一个错误 感谢@sbordet调查并了解发生了什么 有关详细信息,请visit here。
对于想要使用CometD3的人,建议使用Jetty容器 对于那些别无选择但只能使用玻璃鱼的人来说,Cometd2.X可以工作 我试过在glassfish4中使用cometd2.9.1,3.0.0和3.0.1,结果是 2.9.1适用于glassfish4而3.0.0和3.0.1不适用。