0

I'm new to CometD and having problems publishing data on a channel. I'm getting the following error for not invoking a handshake() on my channel:

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)

but when i include sender.handshake() i get the following exception

    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)

I've included my Class below. Can someone tell me if I'm missing something:

@Service

public class CometDSender implements 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);

}

}

user676567
  • 1,119
  • 9
  • 20
  • 39

1 Answers1

2

You must not create BayeuxServerImpl instances unless you know what you're doing, and the same applies to LocalSession. Since you are using CometD services, those fields must be non-null and you must not create them yourself. They probably are null, and this is due to the fact that you have a misconfiguration most likely in your web.xml (which you did not post).

Please read the documentation about CometD services and follow the tutorials: they will guide you step by step in solving your problems with CometD services.

sbordet
  • 16,856
  • 1
  • 50
  • 45