play 脱离容器的践行者

web应用程序,通常采用流行的ssh等,这些多须要servlet容器,甚至ejb容器的支持。开发完后,系统实际须要ap服务器的支持。虽然也有免费的tomcat能够使用,但一旦系统升级,须要移植到其余ap服务器时,若是使用了ap服务器特有的扩张功能的时候,每每须要花费必定的时间和人力。

所以,若是可以使得web应用程序脱离容器运行,无疑可以提升系统扩展性。
play 正好给咱们提供了这样一种选择的机会。

public class Server {
    public Server(String[] args) {
        ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors.newCachedThreadPool())
    }
    public static void main(String[] args) throws Exception {
        Play.init(root, System.getProperty("play.id", ""));
        if (System.getProperty("precompile") == null)
            new Server(args);
    }

play能够做为一个独立的java程序运行,经过netty来对客户请求作出响应。java

当让,play也支持在ap容器中运行。只要一个包裹类ServletWrapper就简单的实现了。web

public class ServletWrapper extends HttpServlet implements ServletContextListener {bootstrap

    protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        Request request = null;
        try {
            Response response = new Response();
            response.out = new ByteArrayOutputStream();
            Response.current.set(response);
            request = parseRequest(httpServletRequest);
tomcat

            boolean raw = Play.pluginCollection.rawInvocation(request, response);
            if (raw) {
                copyResponse(Request.current(), Response.current(), httpServletRequest, httpServletResponse);
            } else {
                Invoker.invokeInThread(new ServletInvocation(request, response, httpServletRequest, httpServletResponse));
            }
        } finally {
            Request.current.remove();
            Response.current.remove();
            Scope.Session.current.remove();
            Scope.Params.current.remove();
            Scope.Flash.current.remove();
            Scope.RenderArgs.current.remove();
            Scope.RouteArgs.current.remove();
            CachedBoundActionMethodArgs.clear();
        }
    }
服务器

相关文章
相关标签/搜索