CometD发布数据的问题

时间:2013-02-12 15:57:28

标签: cometd bayeux

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

}

}

1 个答案:

答案 0 :(得分:2)

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

请阅读有关CometD services的文档并按照tutorials:它们将指导您逐步解决您使用CometD服务的问题。