【问题标题】:Cancelling a Camel message取消 Camel 消息
【发布时间】:2016-12-22 18:58:32
【问题描述】:

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

其中一位开发人员添加了一些代码,这些代码将从飞行存储库中删除特定消息,并将我们存储库的状态更改为已取消。我发现 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);
        }
    }
}

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

【问题讨论】:

    标签: java rest soap apache-camel


    【解决方案1】:

    尝试在取消时将 InterruptedException 设置为异常 交换也是如此,这应该由重新交付错误检测到 处理程序和爆发,例如

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

    【讨论】:

    • 我试过了。然而,当我外出度假时,其他开发人员之一完全删除了 Camel,并使用自定义代码编写了消息管理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2018-03-19
    • 1970-01-01
    • 2014-12-30
    • 2016-02-02
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多