2016-05-23 8 views
1

Apache Camelでは、ルートを定義していますが、2つ以上のhttpリクエストを並行して送信し、AsyncHttpClientを使用したJavaのような処理を行うための 'future'を待つ方法はありますか?Apache Camel:2つのhttpリクエストを並行して送信し、応答を待つ方法

AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient(); 
Future<Response> f = asyncHttpClient.prepareGet("http://www.example.com/").execute(); 
Response r = f.get(); 

コンテキストの場合、次のルートはGET contactsのhttp呼び出しを呼び出し、応答を同期的に返します。

from("direct:getContact") 
.to("http://host:port/contacts/1453") 
+0

を参照してください:http://camel.apache.org/async.html –

+0

おかげでマシュー、 私は非同期APIを試しました。 ProducerTemplateを使用して、いくつかの困難に直面しています。私はそれを正しくやっているかどうか見てみてください。 http://stackoverflow.com/questions/37409460/apache-camel-producertemplate-not-unmarshalling-the-response – ndsurendra

答えて

1

複数の小さなルートに分割してください。次に、必要なアンマーシャリングを実行することができます。

私はあなたがキャメル非同期ライブラリに関する情報を探していると信じてquestion about unmarshalling http response

from("direct:getContact") 
    .process(new Processor() { 
     @Override 
     public void process(Exchange exchange) throws Exception { 
      CamelContext context = exchange.getContext(); 
      ProducerTemplate producerTemplate = context.createProducerTemplate(); 

      // Asynchronous call to internal route 
      Future<Contact> contact = 
       producerTemplate.asyncRequestBody("direct:invokeSomeRestApi", null, Contact.class); 

      // Do rest of the work 
      exchange.getOut().setBody(contact.get()); 
     } 
    }); 

// Unmarshalling REST response 
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(); 
jacksonDataFormat.setUnmarshalType(Contact.class); 

// Internal route definition 
from("direct:invokeSomeRestApi") 
    .to("http://localhost:8080/api/contact/2345") 
    .unmarshal(jacksonDataFormat); 
+0

ありがとうRafal!見つけた – ndsurendra

関連する問題