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/*");
4

1 回答 1

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);
于 2014-07-03T15:35:19.597 回答