且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Apache Camel:如何并行发送两个 http 请求并等待响应?

更新时间:2022-03-15 06:23:43

尝试将您的路线拆分为许多更小的路线.然后您可以在那里执行必要的解组.

Try to split You route into many smaller routes. Then You can perform necessary unmarshalling there.

参见关于解组http响应的问题

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