我是CometD的新手,在通道上发布数据时遇到问题。我收到以下错误,因为我的频道上没有调用握手():
Sender : null
Sender : L:/abc/1?
Exception in thread "Thread-9" java.lang.IllegalStateException: Method handshake() not invoked for local session L:/abc/1?
at org.cometd.server.LocalSessionImpl.getId(LocalSessionImpl.java:161)
at org.cometd.server.ServerChannelImpl.publish(ServerChannelImpl.java:309)
at packagename.CometDSender.sendData(CometDSender.java:64)
at packagename.ProcessorImp.processData(ProcessorImp.java:18)
at packagename.TestSource.processNewData(TestSource.java:50)
at packagename.TestSource.run(TestSource.java:36)
at java.lang.Thread.run(Unknown Source)
但当我包括sender.handshake()
时,我得到以下异常
Sender : null
Sender : L:/abc/1?
Exception in thread "Thread-9" java.lang.NullPointerException
at org.cometd.server.BayeuxServerImpl.freeze(BayeuxServerImpl.java:868)
at org.cometd.server.BayeuxServerImpl.doPublish(BayeuxServerImpl.java:823)
at org.cometd.server.BayeuxServerImpl.handle(BayeuxServerImpl.java:649)
at org.cometd.server.LocalSessionImpl.doSend(LocalSessionImpl.java:214)
at org.cometd.server.LocalSessionImpl.handshake(LocalSessionImpl.java:125)
at org.cometd.server.LocalSessionImpl.handshake(LocalSessionImpl.java:110)
at packagename.CometDSender.sendData(CometDSender.java:62)
at packagename.ProcessorImp.processData(ProcessorImp.java:18)
at packagename.TestSource.processNewData(TestSource.java:50)
at packagename.TestSource.run(TestSource.java:36)
at java.lang.Thread.run(Unknown Source)
我把我的课包括在下面。有人能告诉我我是否遗漏了什么吗:
@Service
公共类CometDSender实现Sender{
@Inject
private BayeuxServer bayeuxServer;
@Session
private LocalSession sender;
private String channelName = null;
// setup CometD Channel
public CometDSender() {
this.channelName = "/abc/1";
System.out.println("bayeuxServer : " + (bayeuxServer == null ? "Is Null" : "Is Not Null"));
if (bayeuxServer == null) {
bayeuxServer = new BayeuxServerImpl();
}
bayeuxServer.createIfAbsent(channelName, new ConfigurableServerChannel.Initializer() {
public void configureChannel(ConfigurableServerChannel channelName) {
System.out.println("channel : " + (channelName == null ? "Is Null" : "Is Not Null"));
channelName.setPersistent(true);
channelName.setLazy(true);
}
});
}
public void sendData(Agent data) {
// Convert the Update business object to a CometD-friendly format
Map<String, Object> cometData = new HashMap<String, Object>(4);
cometData.put("ID", data.getID());
// Publish to all subscribers
ServerChannel channel = bayeuxServer.getChannel(channelName);
System.out.println("Sender : " + sender);
if (sender == null) {
sender = bayeuxServer.newLocalSession(channelName);
System.out.println("Sender : " + sender);
sender.handshake();
}
channel.publish(sender, cometData, null);
}
}
除非您知道自己在做什么,否则不得创建BayeuxServerImpl
实例,LocalSession
也是如此。由于您使用的是CometD服务,所以这些字段必须是非null的,并且您不能自己创建它们。它们可能为空,这是因为您的web.xml
中很可能存在错误配置(您没有发布)。
请阅读有关CometD服务的文档并遵循教程:它们将指导您一步一步地解决CometD的问题。