在学习使用comet4j中,CometEngine engine = CometContext.getInstance().getEngine();
网上的例子 获取engine获取为空,搜了百度,没查到有人遇到这问题,于是自己反编译看源码:
发现 真正的engine是在init方法中进行初始化,它所需要的参数正好是ServletContextListener提供的ServletContextEvent,
直接在下面的方法里调用init,解决。
class FkkTestComet implements ServletContextListener{
private static final Logger logger = LoggerFactory.getLogger(FkkTestComet.class);
private static final String channel1 = "res1";
private static final String channel2 = "res2";
private static int number1 = 0;
private static int number2 = 100;
public void contextInitialized(ServletContextEvent sce) {
CometContext cc = CometContext.getInstance();
cc.registChannel(channel1);
cc.registChannel(channel2);
CometContext.getInstance().init(sce);
CometEngine engine = CometContext.getInstance().getEngine();
。。。。。。
}
}