首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在main()中指定Spring Servlet with Atmosphere

如何在main()中指定Spring Servlet with Atmosphere
EN

Stack Overflow用户
提问于 2013-03-29 04:22:28
回答 1查看 1.2K关注 0票数 2

目前我正在将atmosphere实现到main()方法中,如下所示

代码语言:javascript
复制
public static void main(String[] args) throws IOException {
    final HttpServer server = HttpServer.createSimpleServer(".", 8181);

    WebappContext ctx = new WebappContext("Socket", "/");

    //allow spring to do all of it's stuff
    ctx.addListener("org.springframework.web.context.ContextLoaderListener");

    //enable web socket support
    final WebSocketAddOn addon = new WebSocketAddOn();
    for (NetworkListener listener : server.getListeners()) {
        listener.registerAddOn(addon);

        //if false, local files (html, etc.) can be modified without restarting the server
        //@todo experiment with this setting in production vs development
        listener.getFileCache().setEnabled(false);
    }

    //add jersey servlet support
    ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new ServletContainer());
    //jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "come.fettergroup.production.queue.resources");
    jerseyServletRegistration.setLoadOnStartup(1);
    jerseyServletRegistration.addMapping("/api/*");

    //add atmosphere servlet support
    ServletRegistration atmosphereServletRegistration = ctx.addServlet("AtmosphereServlet", new AtmosphereServlet());
    atmosphereServletRegistration.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json");
    atmosphereServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    atmosphereServletRegistration.setLoadOnStartup(1);

但是在上面的代码中,我如何获取这个XML文件并完成相同的事情呢?

代码语言:javascript
复制
<atmosphere-handlers>
    <atmosphere-handler context-root="/api" class-name="org.atmosphere.handler.ReflectorServletProcessor">
        <property name="servletClassName"
                  value="com.sun.jersey.spi.spring.container.servlet.SpringServlet" />
    </atmosphere-handler>
</atmosphere-handlers>

我已经研究过将处理程序分配给Atmosphere,但它需要一个AtmosphereFramework实例,但我无法获得该实例。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-29 04:33:12

您可以通过执行以下操作来添加AtmosphereHandler:

代码语言:javascript
复制
AtmosphereServlet s = new AtmosphereServlet();
AtmosphereFramework f = s.framework();

ReflectorServletProcessor r = new ReflectorServletProcessor();
r.setServletClassName("com.sun.jersey.spi.spring.container.servlet.SpringServlet");

f.addAtmosphereHandler("/api/*", r);

ServletRegistration atmosphereServletRegistration = ctx.addServlet("AtmosphereServlet", s);

感谢您填写问题BTW,将改进常见问题

-- Jeanfrancois

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15691442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档