2013-10-30 69 views
0

我有一個運行在嵌入式碼頭中的球衣應用程序。我想將它轉換爲使用氣氛球衣以利用彗星的功能,並且我有點卡住了。任何幫助不勝感激。 我的球衣代碼使用了ResourceConfig配置的球衣ServletContainer指定資源實例使用帶有嵌入式碼頭和對象注入的氛圍球衣

ResourceConfig.registerInstances(resourceInstance); 
new org.glassfish.jersey.servlet.ServletContainer(ResourceConfig); 

我不能工作如何獲得相同的設置與環境中工作。我可以看到有一個AtmosphereServlet。是否有可能爲其提供資源實例?

這是球衣/碼頭是如何建立的那一刻...

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.servlet.ServletContextHandler; 
import org.eclipse.jetty.servlet.ServletHolder; 
import org.glassfish.jersey.server.ResourceConfig; 

public class HelloWorldJerseyJetty { 

    // http://localhost:8080/resources/resource 

    public static void main(String[] args) throws Exception { 

     Server server = new Server(8080); 

     ResourceConfig config = new ResourceConfig(); 
     config.registerInstances(new Resource("Hello World")); 
     org.glassfish.jersey.servlet.ServletContainer jerseyServlet = new org.glassfish.jersey.servlet.ServletContainer(config); 

     ServletContextHandler handler = new ServletContextHandler(); 
     handler.addServlet(new ServletHolder(jerseyServlet), "/resources/*"); 

     server.setHandler(handler); 
     server.start(); 
     server.join(); 

    } 

    @Path("resource") 
    public static class Resource { 

     private final String message; 

     public Resource(String message) { 
      this.message = message; 
     } 

     @GET 
     @Produces(MediaType.TEXT_PLAIN) 
     public String get() { 
      return message; 
     } 
    } 
} 

如何從球衣切換到AtmosphereServlet以及如何配置它使用一個實例?

 ResourceConfig config = new ResourceConfig(); 
     config.registerInstances(new Resource("Hello World")); 
     AtmosphereServlet atmosphereServlet = new AtmosphereServlet(); 
     atmosphereServlet.??? 

     ServletContextHandler handler = new ServletContextHandler(); 
     handler.addServlet(new ServletHolder(atmosphereServlet), "/resources/*"); 
+0

這是你的答案的解決方案? –

+0

嘿,你找到一個解決方案?可以分享嗎? – Arash

回答

0

我配置了這樣的AtmosphereServlet,它的工作原理。 也許它可能是你的問題爲時已晚,但它也可以幫助別人:)

Server server = new Server(8080); 

    //static files handler   
    ResourceHandler resource_handler = new ResourceHandler(); 
    resource_handler.setDirectoriesListed(true); 
    resource_handler.setWelcomeFiles(new String[] { "index.html" }); 
    resource_handler.setResourceBase("./src/main/webapp/"); 

    ContextHandler context1 = new ContextHandler(); 
    context1.setHandler(resource_handler); 

    //Atmosphere 
    AtmosphereServlet atmosphereServlet = new AtmosphereServlet(); 
    ServletHolder servletHolder = new ServletHolder(atmosphereServlet); 
    servletHolder.setInitParameter("com.sun.jersey.config.property.packages","org.atmosphere.samples.chat.jersey"); 
    servletHolder.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json"); 
    servletHolder.setAsyncSupported(true); 
    servletHolder.setInitParameter("org.atmosphere.useWebSocket","true"); 

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
    context.addServlet(servletHolder, "/resources/*"); 

    HandlerList handlers = new HandlerList(); 
    handlers.addHandler(context1); 
    handlers.addHandler(context);   

    server.setHandler(handlers);