我正在Jetty Http端点上收到请求。请求正文包含请求正文中的一些网址。我必须向这些网址发出GET
请求。然后聚合每个GET
请求的结果并将其返回给调用者。
申请机构: -
{
"data" : [
{"name" : "Hello", "url" : "http://server1"}
{"name" : "Hello2", "url" : "http://server2"}
]
}
我可以想到这样做的一种方式如下: -
from("jetty:http://localhost:8888/hello").process(new Processor() {
public void process(Exchange exchange) throws Exception {
// 1. Make the GET request in parallel using ThreadPoolExecutor
// 2. Wait for all calls to finish. Collate the response
// 3. Write it to exchange.getOut().setBody
}
})
如果可以通过使用camel Dynamic Routes,Splitter& amp;的Java DSL实现这一目标,有人可以告诉我。聚合器使Processor
仍然相对较小?
我使用的是camel 2.16.3。
答案 0 :(得分:2)
步骤如下:
听起来你的问题的核心是动态URI。这就是代码片段的外观:
from(...)... etc.
.setHeader(Exchange.HTTP_URI, simple("${body}"))
.setHeader(Exchange.HTTP_METHOD,
constant(org.apache.camel.component.http4.HttpMethods.GET))
.to("http4://google.com")
对于小型工作演示,see this class。
public class HttpDynamicClient extends RouteBuilder {
@Override
public void configure() throws Exception {
from("direct:testMultiple")
.split(body())
.to("direct:httpClient");
from("direct:httpClient")
.log("starting httpClient route")
.setHeader(Exchange.HTTP_URI, simple("${body}"))
.setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.GET))
.to("http4://google.com")
.process(new BodyToStringConverter())
.log(LoggingLevel.INFO, "Output was ${body}");
}
private static class BodyToStringConverter implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getOut().setBody(exchange.getIn().getBody(String.class));
}
}
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
try {
Logger logger = Logger.getLogger(HttpDynamicClient.class);
context.addRoutes(new HttpDynamicClient());
ProducerTemplate template = context.createProducerTemplate();
context.start();
Thread.sleep(1000);
template.sendBody("direct:httpClient", "http://jsonplaceholder.typicode.com/posts/1");
Thread.sleep(2000);
template.sendBody("direct:testMultiple", new String [] {"http://jsonplaceholder.typicode.com/posts/1" , "http://jsonplaceholder.typicode.com/posts/1"});
} finally {
context.stop();
}
}
}
答案 1 :(得分:2)
@Darius X的答案非常符合你的需要。要使后端请求并行执行并在字符串列表中聚合响应主体,您需要设置聚合策略并在拆分定义上设置并行处理标志。
@Override
public void configure() throws Exception {
from("direct:testMultiple")
.split(body(), new FlexibleAggregationStrategy<String>()
.pick(Builder.body())
.castAs(String.class)
.accumulateInCollection(ArrayList.class))
.parallelProcessing()
// .executorService(<instance of java.util.concurrent.ExecutorService>) // optional: use custom thread pool for parallel processing
.to("direct:httpClient");
from("direct:httpClient")
.log("starting httpClient route")
.setHeader(Exchange.HTTP_URI, simple("${body}"))
.setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http4.HttpMethods.GET))
.to("http4://google.com")
.convertBodyTo(String.class)
.log(LoggingLevel.INFO, "Output was ${body}");
}
direct:testMultiple
返回的交换消息将包含您的结果数组作为正文。