在dropwizard中使用Cometd [英] Using cometd in dropwizard

查看:107
本文介绍了在dropwizard中使用Cometd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cometd作为dropwizard的servlet,但是BayeuxServer似乎没有注入我的服务中。我正在这样添加我的两个servlet(请注意,我没有使用 web.xml ,所以我自己定义了参数):

I'm trying to use cometd as a servlet to dropwizard but BayeuxServer doesn't seem to get injected in my service. I'm adding my two servlets as so (note, I'm not using web.xml so I'm defining the params myself):

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

我的服务(这是我的代码失败的地方):

And my service (this is where my code fails):

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;

I have also tried creating my own instance of the BayeuxServer but then that leads to a separate NPE in BayeuxServerImpl.freeze();

任何人都知道如何正确使用Cometd

Anyone know how to properly use cometd with dropwizard?

推荐答案

为了注入 BayeuxServer 实例,CometD必须具有要注入的服务实例,在这种情况下,必须是您的类 MyService 的实例。

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.

不幸的是,从构造函数中(我认为您在上面误命名为 OrderService ),您正在调用 initChannel()方法,它尝试使用由于构造函数仍在执行而尚未注入的 BayeuxServer 字段。

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.

解决方案是将您的频道初始化推迟到使用 @PostConstruct 注释的其他方法:

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();
    }
}

CometD API使用的是CometD 2.7.0,如果您使用的是CometD的旧版本,我建议使用它。

The CometD API used is from CometD 2.7.0, which I recommend to use if you are on older CometD versions.

这篇关于在dropwizard中使用Cometd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆