取消Camel消息

时间:2016-12-22 18:58:33

标签: java rest soap apache-camel

我支持使用Apache Camel作为SOAP和RESTful消息的路由中心运行的应用程序。我们实现了一些相当复杂的路由规则。有一个用户界面,管理员可以监控通过集线器发送的消息以及它们的状态。我有一个增强请求,允许用户请求正在处理的任何请求的删除。这通常是一个请求,无论出于何种原因发送失败,Camel已将其排队等待重新发送。

其中一位开发人员添加了一些代码,这些代码将从inflight存储库中删除特定消息,并将我们存储库中的状态更改为已取消..我发现Camel将尝试重新传递消息,直到达到最大值重试计数,消息以失败状态结束而不是取消。

现有的代码是:

@Override
@Asynchronous
public void cancelExchange(String exchangeId) {
    synchronized (this.camelContext) {
        // Get a list of all in flight exchanges with the passed in exchangeId
        final List<Exchange> exchanges = this.camelContext.getInflightRepository().browse().stream().filter(inflightExchange -> inflightExchange.getExchange().getExchangeId().equals(exchangeId))
                .map(InflightRepository.InflightExchange::getExchange).collect(Collectors.toList());

        // Remove any active exchanges from Camel
        for (final Exchange exchange : exchanges) {
            this.log.debug("Removing exchange {}", exchange.getExchangeId());

            this.camelContext.getInflightRepository().remove(exchange);

            // Set the ROUTE_STOP property so Camel will try to stop the Exchange on the next
            // retry
            exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
        }

        // Set the ExchangeMessage status to canceled
        final Optional<ExchangeMessage> optionalExchangeMessage = this.messageService.findOneByExchangeId(exchangeId);

        if (optionalExchangeMessage.isPresent()) {
            this.messageService.setProcessingStatus(optionalExchangeMessage.get(), ProcessingStatus.CANCELLED, null);
        }
    }
}

我不得不承认,我很惊讶看到该消息重新出现,因为它应该从队列中删除。我们哪里出错?

1 个答案:

答案 0 :(得分:0)

尝试将InterruptedException设置为已取消的异常 交换也应该通过重新传递错误来检测 处理程序和爆发,例如

exchange.setException(new InterruptedException("Cancel Exchange"));
exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);