2013-11-22 3 views
0

cometd를 dropwizard에 대한 서블릿으로 사용하려하지만 BayeuxServer가 내 서비스에 주입되지 않은 것 같습니다.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); 

그리고 내 서비스를 (내 코드가 실패 곳입니다) :

을 나는 (그래서 나는 나 자신을 PARAMS 정의하고있어 내가 web.xml를 사용하지 않는주의) 그래서 나의 두 서블릿을 추가 해요
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 내 자신의 인스턴스를 생성 시도

그러나 BayeuxServerImpl.freeze();

사람에 별도의 NPE로 연결이 제대로 dropwizard와 cometd를 사용하는 방법을 알아?

+0

'Class.getCanonicalName()'을 사용하지 마십시오. 내부 클래스의 클래스 로딩을 중단시킬 것이라고 생각합니다. 대신 Class.getName()을 사용하십시오. – sbordet

답변

3

BayeuxServer 인스턴스를 삽입하려면 CometD에 주입 할 서비스 인스턴스가 있어야합니다.이 경우에는 MyService 클래스 인스턴스가 있어야합니다.

불행하게도, 당신이 생성자가 여전히 실행되고 있기 때문에 아직 주입되지 않은 BayeuxServer 필드를 사용하려고 initChannel() 방법을 요구하고있다 (나는 당신이 그것을 OrderService를 호출, 위의 이름을 잘못 생각) 생성자에서.

이 솔루션은 @PostConstruct 주석이 다른 방법으로 채널 초기화를 연기하는 것입니다

사용되는 CometD API는 내가 나이가 CometD 버전에있는 경우 사용하는 것이 좋습니다 CometD 2.7.0에서입니다
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(); 
    } 
} 

.

+0

감사합니다.'@ PostConstruct'를 사용했지만 여전히 NPE를 얻고 있습니다. 내가 체크 할 때,'bayeuxServer'는 여전히 null이긴하지만 나중에 호출됩니다. –

+0

제가 놓친 한 가지 사실은'@ Service'로 서비스에 주석을다는 것이 아니 었습니다. '@ PostConstruct'없이 다시 시도해 보았지만 여전히 실패 했으므로 어쨌든 필요했습니다. 고마워요! –

관련 문제