Jetty7 Continuation 学习(一)

Jetty7发布了,Jetty7支持servlet 2.5,且对Jetty6做了很大的重构,使之更合理更高效。
Jetty的Http异步处理模式,包括Jetty HttpClient(异步的HttpClient),Jetty Continuation(异步的Http Request/Respoinse),都是很吸引人的技术,有很多很好的应用,比如在线聊天室,实时股票行情表,异步Ajax代理等等,都可以用Jetty的异步处理模式来实现。

趁Jetty7的到来,赶紧享受一下这道技术美味。

 

Jetty Continuation 实际上是一种异步Http技术,他能让Http连接挂起,直到超时或者异步事件发生时,Http连接可以恢复。Jetty Continuation 的技术应用起来不复杂,有几个关键的API,和两种设计模式:

 

API:
(1) 得到Continuation
Continuation continuation = ContinuationSupport.getContinuation(request);

 

(2) 挂起Http请求
void doGet(HttpServletRequest request, HttpServletResponse response)
{
    ...
    continuation.setTimeout(long);  // 可选:设置continuation 超时
    continuation.suspend();
    ...
}

 

(3) 恢复Http连接,一旦异步事件发生了,可以通过异步事件的回调函数来恢复Http连接
void myAsyncCallback(Object results)
{
    continuation.setAttribute("results", results);
    continuation.resume();
}

 

(4) 完成Http连接,通常用在异步事件回调函数里返回Http Response时:
void myAsyncCallback(Object results)
{
    writeResults(continuation.getServletResponse(), results); // 将异步事件结果result,通过Response返回客户端
    continuation.complete();
}

 

(5)注册异步事件处理器
myAsyncHandler.register(continuation);

 

(6)监听continuation事件
void doGet(HttpServletRequest request, HttpServletResponse response)
{
    ...
    Continuation continuation = ContinuationSupport.getContinuation(request);
   
continuation.addContinuationListener(new ContinuationListener()
    {
      public void onTimeout(Continuation continuation) { ... } // 超时事件
      public void onComplete(Continuation continuation) { ... } // 完成事件
    });
 
    continuation.suspend();
    ...
}

 

两种设计模式:Suspend/Resume模式,Suspend/Continue模式:
(1) Suspend/Resume模式:

void doGet(HttpServletRequest request, HttpServletResponse response)
{
     // 如果我们需要异步方式得到一个result,并放入request中
     Object results = request.getAttribute("results");
     if (results==null) // 如果异步处理尚未返回结果
      {
       final Continuation continuation = ContinuationSupport.getContinuation(request);
 
       // 判断是否超时
       if (continuation.isExpired())
       {
         // 返回超时Response
         sendMyTimeoutResponse(response);
         return;
       }
 
       // 挂起HTTP连接
       continuation.suspend(); 
 
       // 注册一个异步事件处理器
       myAsyncHandler.register(new MyHandler()
       {
          // 异步事件
          public void onMyEvent(Object result)
          {
            continuation.setAttribute("results", results); // 传送results
            continuation.resume(); // 恢复连接
          }
       });
       return; // or continuation.undispatch();
     }
 
     // 连接恢复后返回结果
     sendMyResultResponse(response,results);
}

 

(2)Suspend/Continue模式:

void doGet(HttpServletRequest request, HttpServletResponse response)
{
     final Continuation continuation = ContinuationSupport.getContinuation(request);
 
       // 判断是否超时
       if (continuation.isExpired())
       {
         // 返回超时Response
         sendMyTimeoutResponse(response);
         return;
       }
 
       // 挂起HTTP连接
       continuation.suspend(response); // response被包装
 
       // 注册一个异步事件处理器
       myAsyncHandler.register(new MyHandler()
       {
          // 异步事件
          public void onMyEvent(Object result)
          {
            sendMyResultResponse(continuation.getServletResponse(), results); // 通过response返回results
            continuation.complete(); // 完成
          }
       });
     }
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 CentOS 7 上安装 Jetty,请按照以下步骤进行操作: 1. 更新系统软件包: ``` sudo yum update ``` 2. 安装 Java 运行时环境(JRE): ``` sudo yum install java-1.8.0-openjdk ``` 3. 下载 Jetty 安装包: ``` wget https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distribution/9.4.43.v20210629/jetty-distribution-9.4.43.v20210629.tar.gz ``` 4. 解压缩 Jetty 安装包: ``` tar -zxvf jetty-distribution-9.4.43.v20210629.tar.gz ``` 5. 将 Jetty 移动到指定目录: ``` sudo mv jetty-distribution-9.4.43.v20210629 /opt/jetty ``` 6. 设置 Jetty 环境变量: ``` export JETTY_HOME=/opt/jetty export PATH=$PATH:$JETTY_HOME/bin ``` 7. 创建 Jetty 用户和组: ``` sudo groupadd jetty sudo useradd -g jetty -d $JETTY_HOME -s /bin/false jetty sudo chown -R jetty:jetty $JETTY_HOME ``` 8. 创建一个 Jetty 服务文件: ``` sudo vi /etc/systemd/system/jetty.service ``` 将以下内容添加到文件中: ``` [Unit] Description=Jetty service After=network.target [Service] User=jetty ExecStart=/opt/jetty/bin/jetty.sh start ExecStop=/opt/jetty/bin/jetty.sh stop ExecReload=/opt/jetty/bin/jetty.sh check [Install] WantedBy=default.target ``` 9. 启动 Jetty 服务: ``` sudo systemctl start jetty ``` 10. 设置 Jetty 服务开机自启: ``` sudo systemctl enable jetty ``` 现在,Jetty 已经成功安装在 CentOS 7 上,并作为一个服务运行。您可以通过访问 `http://localhost:8080` 来验证安装是否成功。请注意,您可能需要根据您的需求进行一些其他配置,例如修改 Jetty 的端口或添加您自己的 Web 应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值