java - 如何在 main() 中指定带有 Atmosphere 的 Spring Servlet

标签 java spring sockets atmosphere

目前我正在像这样的 main() 方法中实现 Atmosphere

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 文件并在上面的代码中完成同样的事情?

<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 实例。

最佳答案

您可以通过以下方式添加 AtmosphereHandler:

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

感谢您填写问题,顺便说一句,我们将改进常见问题解答

--让弗朗索瓦

关于java - 如何在 main() 中指定带有 Atmosphere 的 Spring Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691442/

相关文章:

java - Spring Mvc : An exception occurred processing JSP page

java - 如何在Linux中给出路径以从Java文件中读取数据

java - Eclipse 中的 PKIX 路径构建失败

spring - 监控 Spring 批处理作业的进度

java - 警告 : No mapping for GET/StudentCurd/(HTTP Status 404 – Not Found) I have tried all the solutions but not able to sole this, 所以再次询问:

c - 如何查找已连接套接字使用的网络接口(interface)

Java网络IO服务器设置

linux - 以 QT 方式使用 linux 套接字?

java - 如果我在套接字上设置超时,即使我将其包装在另一种类型中,它的输入流仍然会抛出超时异常

java - Spring Kafka 客户端 SSL 设置