using Apache's AsyncHttpClient in a storm bolt

与世无争的帅哥 提交于 2019-12-04 22:17:29

I got this to work with no issues. the key things to note are:

  1. declare the client on the bolt class scope

    public class MyRichBolt extends BaseRichBolt {
        private CloseableHttpAsyncClient httpclient; 
    
  2. Instantiate and stat the client in the bolt's prepare method

    @Override
    public final void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        try {
            // start the http client
            httpclient = HttpAsyncClients.createDefault();
            httpclient.start();
            // other initialization code ... 
        } catch (Throwable exception) {
        // handle errors
        }
    }
    
  3. make the calls in the bolt's execute method

    @Override
    public final void execute(Tuple tuple) {
        // format the request url
        String url = ... 
        sendAsyncGetRequest(url);
    }
    
    
    private void sendAsyncGetRequest(String url){
        logger.debug("Async call to URL...");
        HttpGet request = new HttpGet(url);
        HttpAsyncRequestProducer producer = HttpAsyncMethods.create(request);
        AsyncCharConsumer<HttpResponse> consumer = new AsyncCharConsumer<HttpResponse>() {
    
            HttpResponse response;
    
            @Override
            protected void onResponseReceived(final HttpResponse response) {
            this.response = response;
            }
    
            @Override
            protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException {
                // Do something useful
            }
    
            @Override
            protected void releaseResources() {
            }
    
            @Override
            protected HttpResponse buildResult(final HttpContext context) {
                return this.response;
            }
        };
    
        httpclient.execute(producer, consumer, new FutureCallback<HttpResponse>() {
    
            @Override
            public void completed(HttpResponse response) {
                // do something useful with the response
                logger.debug(response.toString());
            }
    
            @Override
            public void failed(Exception ex) {
                logger.warn("!!! Async http request failed!", ex);
            }
    
            @Override
            public void cancelled() {
                logger.warn("Async http request canceled!");
            }
        });
    }
    

Are you shutting down the client (client.close();) in your main flow before the callback can execute?

The error is saying that the IO path has already been closed. In general, instances of async clients should be re-used for repeated requests and destroyed only when "ALL" requests have been made, e.g. at application shutdown.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!