我試圖使用cometd作爲servlet來dropwizard,但BayeuxServer似乎沒有在我的服務中注入。我將我的兩個Servlet作爲這樣(請注意,我沒有使用web.xml
所以我定義PARAMS自己):在dropwizard中使用cometd
cometdConfig.put("services", MyService.class.getCanonicalName());
System.out.print("OrderService name: " + MyService.class.getCanonicalName());
environment.addServlet(AnnotationCometdServlet.class, "/cometd/*").addInitParams(cometdConfig).setInitOrder(1);
environment.addServlet(new MyServiceServlet(), "/orders/*").setInitOrder(2);
我的服務(這是我的代碼失敗):
public class MyService
implements MyWatcher.Listener
{
@Inject
private BayeuxServer bayeuxServer;
@Session
private LocalSession sender;
private final String _channelName;
private ServerChannel _channel = null;
public OrderService() {
_channelName = "/cometd/";
initChannel();
}
private void initChannel() {
// I get an NPE here
bayeuxServer.createIfAbsent(_channelName, new ConfigurableServerChannel.Initializer() {
@Override
public void configureChannel(ConfigurableServerChannel channel) {
// ...
}
});
_channel = bayeuxServer.getChannel(_channelName);
}
}
我也試圖創造我自己的BayeuxServer的實例,但隨後導致單獨的NPE在BayeuxServerImpl.freeze();
任何人都知道如何正確使用的cometd與dropwizard?
不要使用'Class.getCanonicalName()',因爲我認爲它會破壞內部類的類加載。改爲使用'Class.getName()'。 – sbordet