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();
        }
    }
服務器

相關文章
相關標籤/搜索