如何在RabbitMQ服务器暂时不可用时重试启动Apache Camel RabbitMQ消费者路由器

huangapple go评论89阅读模式
英文:

How to retry starting Apache Camel RabbitMQ consumer router if the RabbitMQ server is temporarily unavailable

问题

我有一个简单的Apache Camel路由

从 "spring-rabbitmq:myExchange?routingKey=foo&bridgeErrorHandler=true" 开始
.log("来自RabbitMQ: ${body}");

示例项目在 这里

如果RabbitMQ服务器在我启动路由时运行正常,一切都正常。问题是,如果路由启动时RabbitMQ不可用,路由启动将以 org.springframework.amqp.AmqpConnectException 终止。

是否有任何方法来处理异常并在一些延迟后重试启动路由?

我尝试添加 onException,但似乎只对生产者("to" 终点)起作用。

我还发现了Apache Camel的 RoutePolicy。它的 onInit 方法在路由崩溃之前触发,因此可以在一定程度上用于重试路由启动,但文档不是很详尽,我也没有找到任何示例。

英文:

I have a simple Apache Camel route

     from("spring-rabbitmq:myExchange?routingKey=foo&bridgeErrorHandler=true")
        .log("From RabbitMQ: ${body}");

The sample project is here.

If the RabbitMQ server is up and running when I start the route, everything works fine. The problem is if the RabbitMQ is not available on the route start the route startup terminates with org.springframework.amqp.AmqpConnectException.

Is there any way how to handle the exception and retry several times after some delay starting the route again?

I tried to add onException but that seems to work for producer ("to" endpoint) only.

I have also found Apace Camel RoutePolicy. It's method onInit is triggered before the route crashes so it might be used somehow to retry the route startup but the documentation is not very exhausting and I have not found any example either.

答案1

得分: 1

是的,查看监控路由控制器
https://camel.apache.org/manual/route-controller.html

此外,您可以查看RabbitMQ连接工厂(Spring RabbitMQ相关内容),因为它可能也内置了某种重试功能,您可以进行配置。

英文:

Yes see supervising route controller
https://camel.apache.org/manual/route-controller.html

Also you may look at the rabbitmq connection factory (spring rabbitmq stuff) as it may have some kind of retry built-in as well, you can configure.

答案2

得分: 0

使用监控控制器(感谢@ClausIbsen),我通过将以下内容添加到我的application.properties文件来解决了该问题:

# 设置监控控制器
camel.springboot.route-controller-supervise-enabled = true

# 您还可以配置更多选项
camel.springboot.routeControllerBackoffDelay = 5000
camel.springboot.routeControllerBackoffMaxAttempts = 3
camel.springboot.routeControllerInitialDelay = 1000
camel.springboot.routeControllerThreadPoolSize = 2

关键在于将在文档中所述的

camel.springboot.route-controller-enabled = true

替换为

camel.springboot.route-controller-supervise-enabled = true
英文:

With the usage of Supervising controler (thanks to @ClausIbsen) i solved the issue by adding following lines to my application.properties file

# Set Supervising controller
camel.springboot.route-controller-supervise-enabled = true

# and you can configure more options
camel.springboot.routeControllerBackoffDelay = 5000    
camel.springboot.routeControllerBackoffMaxAttempts = 3
camel.springboot.routeControllerInitialDelay = 1000
camel.springboot.routeControllerThreadPoolSize = 2

The trick was to replace

camel.springboot.route-controller-enabled = true

as stated in the documentation with

camel.springboot.route-controller-supervise-enabled = true

huangapple
  • 本文由 发表于 2023年8月10日 22:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定