博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
play 脱离容器的践行者
阅读量:7082 次
发布时间:2019-06-28

本文共 1693 字,大约阅读时间需要 5 分钟。

hot3.png

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来对客户请求做出响应。

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

public class ServletWrapper extends HttpServlet implements ServletContextListener {

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

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

转载于:https://my.oschina.net/u/616801/blog/194697

你可能感兴趣的文章
lvs+keepalived原理
查看>>
windows安装
查看>>
C实现2台主机间的passwordLess,基于配置文件批量建立主机之间的passwordless
查看>>
私有云桌面和公有云桌面的区别是什么
查看>>
Locale java
查看>>
【转】Android 环境变量 和 AVD 环境变量 配置
查看>>
使用Weka进行数据挖掘
查看>>
关于计算机信息系统集成项目经理资质申报的补充通知
查看>>
飞机大战小游戏
查看>>
中国物联网的随笔
查看>>
VMware虚拟机linux系统时间同步的解决办法
查看>>
我的友情链接
查看>>
Linux网络管理工具
查看>>
Linux磁盘管理7
查看>>
【学神】 1-3xmanager远程工具的使用和vim编辑器的使用
查看>>
IBM 把 Informix 数据库给“卖了”
查看>>
maven在windows和Linux下的安装
查看>>
如何解锁Oracle数据库中账号
查看>>
C# Socket编程笔记(自己看,转载)
查看>>
UML建模工具Visual Paradigm(VP-UML)使用教程:安装详解
查看>>