2013-03-28 13 views
2

目前我實現大氣氣氛指定春天的Servlet成這樣如何使用在main()

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文件並完成同樣的事情我的main()方法,但在上面的代碼?

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

我看着到大氣分配處理程序,但它需要的AtmosphereFramework這我無法獲得一個實例。

回答

2

您可以通過執行添加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); 

感謝BTW填充問題,將改善FAQ

- Jeanfrancois

+0

真棒,雖然它看起來像'setServletClassName()'是我所需要的,而不是'setServletClass()',因爲它已被棄用 – Webnet

相關問題