Apache camel Apache骆驼超时同步路由
我试图使用ApacheCamel构建一个带有超时的同步路由,但在框架中找不到任何解决它的方法。 所以我决定用make it for me构建一个流程Apache camel Apache骆驼超时同步路由,apache-camel,spring-camel,Apache Camel,Spring Camel,我试图使用ApacheCamel构建一个带有超时的同步路由,但在框架中找不到任何解决它的方法。 所以我决定用make it for me构建一个流程 public class TimeOutProcessor implements Processor { private String route; private Integer timeout; public TimeOutProcessor(String route, Integer timeout) { this.route =
public class TimeOutProcessor implements Processor {
private String route;
private Integer timeout;
public TimeOutProcessor(String route, Integer timeout) {
this.route = route;
this.timeout = timeout;
}
@Override
public void process(Exchange exchange) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Exchange> future = executor.submit(new Callable<Exchange>() {
public Exchange call() {
// Check for field rating
ProducerTemplate producerTemplate = exchange.getFromEndpoint().getCamelContext().createProducerTemplate();
return producerTemplate.send(route, exchange);
}
});
try {
exchange.getIn().setBody(future.get(
timeout,
TimeUnit.SECONDS));
} catch (TimeoutException e) {
throw new TimeoutException("a timeout problem occurred");
}
executor.shutdownNow();
}
我想知道我的方法是否是推荐的方法,如果不是,构建带超时的同步路由的最佳方法是什么?我想感谢回答我的人 这是我的最终代码:
public class TimeOutProcessor implements Processor {
private String route;
private Integer timeout;
public TimeOutProcessor(String route, Integer timeout) {
this.route = route;
this.timeout = timeout;
}
@Override
public void process(Exchange exchange) throws Exception {
Future<Exchange> future = null;
ProducerTemplate producerTemplate = exchange.getFromEndpoint().getCamelContext().createProducerTemplate();
try {
future = producerTemplate.asyncSend(route, exchange);
exchange.getIn().setBody(future.get(
timeout,
TimeUnit.SECONDS));
producerTemplate.stop();
future.cancel(true);
} catch (TimeoutException e) {
producerTemplate.stop();
future.cancel(true);
throw new TimeoutException("a timeout problem occurred");
}
}
}
公共类TimeOutProcessor实现处理器{
专用字符串路由;
私有整数超时;
公共时间输出处理器(字符串路由,整数超时){
this.route=路线;
this.timeout=超时;
}
@凌驾
公共作废进程(Exchange)引发异常{
Future=null;
ProducerTemplate ProducerTemplate=exchange.getFromEndpoint().getCamelContext().createProducerTemplate();
试一试{
future=producerTemplate.asyncSend(路由、交换);
exchange.getIn().setBody(future.get(
超时,
时间单位(秒);
producerTemplate.stop();
future.cancel(true);
}捕获(超时异常e){
producerTemplate.stop();
future.cancel(true);
抛出新的TimeoutException(“出现超时问题”);
}
}
}
您的具体用例是什么?我认为没有理由对Camel中的直接路由应用超时。你为什么需要这样做?因为我的路由需要与数据库通信,而且这个路由必须是同步的,因为这个路由需要在特定的时间内执行。ProducerTemplate上有一些异步API,你可以用来发送,然后返回一个未来的
,你可以在超时后得到它。但是,我需要一个同步路由。是否有方法在同步中转换异步路由以仅用于超时?什么意思是可以使用带有ProducerTemplate
的bean,该bean使用异步方法调用直接路由,如asyncSend
(有关更多信息,请参阅)。这将立即为您提供一个Java的未来
。在此Future
上,您可以请求超时的结果(请参阅get
方法with timeout in)。您有一个同步路由,可以通过超时调用它。
public class TimeOutProcessor implements Processor {
private String route;
private Integer timeout;
public TimeOutProcessor(String route, Integer timeout) {
this.route = route;
this.timeout = timeout;
}
@Override
public void process(Exchange exchange) throws Exception {
Future<Exchange> future = null;
ProducerTemplate producerTemplate = exchange.getFromEndpoint().getCamelContext().createProducerTemplate();
try {
future = producerTemplate.asyncSend(route, exchange);
exchange.getIn().setBody(future.get(
timeout,
TimeUnit.SECONDS));
producerTemplate.stop();
future.cancel(true);
} catch (TimeoutException e) {
producerTemplate.stop();
future.cancel(true);
throw new TimeoutException("a timeout problem occurred");
}
}
}