我是 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);
}
}