如何通知"内容消费"对码头9.02。 ContentListener? [英] How to notify "content consumed" for Jetty 9.02. ContentListener?

查看:310
本文介绍了如何通知"内容消费"对码头9.02。 ContentListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个码头的HttpClient发送使用码头v9.0.2异步请求。我有一个成功的缓冲式响应Response.ContentListener和通话时已经完成了被称为Response.CompeleteListener。

I have a Jetty HttpClient sending Async Requests using Jetty v9.0.2. I have a Response.ContentListener that successfully buffers-up response and a Response.CompeleteListener that is called when the call has completed.


  • 码头9.2 JavaDoc中有一个<一个href=\"http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/client/api/Response.AsyncContentListener.html\"相对=nofollow> Request.AsyncContentListener 其中有一个参数来告诉码头内容已消耗:

    • <一个href=\"http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/client/api/Response.AsyncContentListener.html#onContent%28org.eclipse.jetty.client.api.Response,%20java.nio.ByteBuffer,%20org.eclipse.jetty.util.Callback%29\"相对=nofollow>回调 - 当内容被消耗调用回调

    • Jetty 9.2 JavaDocs has a Request.AsyncContentListener which has a parameter to tell Jetty the content has been consumed:
      • callback - the callback to call when the content is consumed.

      这监听器类型是不是在码头v9.0.2响应API:

      This listener type is NOT in the Jetty v9.0.2 Response api:


      • 进口org.eclipse.jetty.client.api的结果

      • import org.eclipse.jetty.client.api.Result;

      有两个问题真的:


      1. 是否Response.ContentListener需要通知码头客户端的内容被消耗?

        • 如果这样:如何与码头v9.0.2管理


      • 是否有使用异步与码头v9.0.2潜在gottchas?

      这似乎是做工精细;我需要确保我们没有资源泄漏或潜在泄漏等。当然,我们需要的程序是非常干净的,因此它可以全天候运行。的在此先感谢的的欢迎帮助。

      It seems to be working fine; I need to ensure we have no resource leaks or potential leaks, etc. And of course we need the program to be nice and clean so it can run 24x7. Thanks in advance for your welcome help.

      请注意:

      • Request.AsyncContentListener - Stable Jetty doc (jetty 9.2, I believe)

      推荐答案

      简短的回答这个问题是调用的 HttpClient.stop 异步调用已完成处理()之后。一个解决方案的方法是在本教程中的例子所示:

      The short answer to this question is to call HttpClient.stop() after the async call has completed processing. A solution approach is outlined in this tutorial example:

      • Jetty's HTTP Client

      主要的方面是,停止()可能不被调用,直到(全部)的听众已经完成,并处理完成。在上面,一个 CountdownLatch <例子/ STRONG> 用来停止()调用同步。这是不是真的在码头的例子,例如解释道:

      The main aspect is that stop() may not be called until (all) listeners have finished-up and processing has completed. In the example above, a CountdownLatch is used to synchronize the stop() call. This isn't really explained in the Jetty examples, e.g.:

      • The new Jetty 9 HTTP client
      • Chapter 28. HTTP Client - Asynchronous APIs
      • Class HttpClient

      有些异步接口使用这样的方法调用的消费内容的,等等。然而,随着码头9停止()调用做清理。

      Some async interfaces use calls like "consume content", etc. However with Jetty 9 the stop() call does the clean-up.

      我们的用例是直线前进足够了。在听者的onComplete()方法,将调用到 setCompleted ()的递减倒计时锁定为Httpclient.send()方法进行修改。后面的send()调用,我们有一个叫做同步方法,完成()上等待的呼吁setCompleted()。

      Our use-case was straight forward enough. In the onComplete() method in the listener, a call is made to a setCompleted() method that decrements a count down latch for the Httpclient.send(). Following the send() call, we have a synchronization method called, finished() that wait on the call for setCompleted().

      public  boolean finish(){
      
          boolean result      = false;
          int     retryCount  =  0;
      
          try
          {
              if( !this.latch.await( 5, TimeUnit.SECONDS ) )
              {
                  Util.warn(LOG,"Timed-out -- msg #", String.format("%0,4d", this.msgId) );
              }
      
              this.httpClient.stop();
              result = this.httpClient.isStopped();
          }
          catch (InterruptedException ex )
          {
              log.warn("Interrupted -- msg #", String.format("%0,4d", this.msgId)" );
          }
          catch (Exception ex)
          {
              log.error(LOG,"Stop-exception -- msg #", String.format("%0,4d", this.msgId) );
          }
      
          if( !result )
          {
              log.warn(" * HttpClient NOT stopped -- msg #", String.format("%0,4d", this.msgId) );
          }
      
          return  result;
      }
      

      停止通话,似乎很乐意与一个电话,只要setCompleted()被调用在我们的onComplete()方法的末尾。我不清楚,如果你不能肯定为的onComplete内部定时的。否则,这一切似乎令人满意。我真的相信API文件应包括清理code 的对于这种opertations - 数据通讯科,数据库等希望这帖子会节省时间,为别人

      The stop call seems to be happy with a call as long as the setCompleted() is called at the end of our onComplete() method. I'm not clear if you can't be sure of the internal timing for onComplete. Otherwise it all seems satisfactory. I really believe API documentation should include "clean-up code" for opertations of this kind - data comms, database, etc. Hopefully this posting will save time for others.

      这篇关于如何通知&QUOT;内容消费&QUOT;对码头9.02。 ContentListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆