In order to inject the BayeuxServer
instance, CometD must have the instance of the service to inject to, in this case an instance of your class MyService
.
Unfortunately, from the constructor (which I think you misnamed above, calling it OrderService
) you are calling the initChannel()
method, which tries to use the BayeuxServer
field which is not yet injected because the constructor is still executing.
The solution is to defer your channel initialization to a different method annotated with @PostConstruct
:
public class MyService
{
@Inject
private BayeuxServer bayeuxServer;
@Session
private LocalSession sender;
private final String _channelName;
private ServerChannel _channel;
public MyService()
{
_channelName = "/cometd/";
}
@PostConstruct
private void initChannel()
{
_channel = bayeuxServer.createChannelIfAbsent(_channelName).getReference();
}
}
The CometD API used is from CometD 2.7.0, which I recommend to use if you are on older CometD versions.