0

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

}

}

4

1 回答 1

2

BayeuxServerImpl除非您知道自己在做什么,否则不得创建实例,这同样适用于LocalSession. 由于您使用的是 CometD 服务,因此这些字段必须是非空的,并且您不能自己创建它们。它们可能为空,这是因为您的配置很可能有错误web.xml(您没有发布)。

请阅读有关CometD 服务的文档并按照教程进行操作:它们将逐步指导您解决 CometD 服务的问题。

于 2013-02-12T20:58:43.253 回答