即使无法访问 MQTT 服务器,如何启动骆驼 [英] How to start camel even if the MQTT server is not reachable

查看:52
本文介绍了即使无法访问 MQTT 服务器,如何启动骆驼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 Apache Camel 应用程序更具弹性并且即使在 MQTT 代理无法访问时也能启动.我们在互联网连接可能不稳定的物联网设备上使用 Camel,我希望我们的应用程序即使没有互联网访问也能启动.

I would like to make my Apache Camel application to be more resilient and start even when the MQTT broker is not reachable. We are using Camel on IoT devices with a possibly unstable internet connection and I want our application to start even without internet access.

示例路线如下所示:

from("timer:heartbeat?period=5000")
    .routeId("send heartbeat")
    .setBody(simple("Hello World!"))
    .to("paho:myhostnome/heartbeat?brokerUrl={{broker.url}}")

只要 MQTT 服务器可用,这就可以正常工作.但是,当服务器无法访问时,上下文会在预热 PahoEndpoint 时失败.

This works fine as long as the MQTT server is available. However when the server is not reachable, the context fails while warming up the PahoEndpoint.

Caused by: org.apache.camel.FailedToCreateRouteException: Failed to create route send heartbeat: Route(send heartbeat)[[From[timer:heartbeat?period={{heartbe... because of Unable to connect to server
at org.apache.camel.impl.RouteService.warmUp(RouteService.java:147) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.doWarmUpRoutes(DefaultCamelContext.java:3758) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3665) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3451) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3305) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:202) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3089) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3085) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3108) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3085) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3022) ~[camel-core-2.19.3.jar:2.19.3]
at org.apache.camel.spring.boot.RoutesCollector.maybeStart(RoutesCollector.java:242) ~[camel-spring-boot-2.19.3.jar:2.19.3]
at org.apache.camel.spring.boot.RoutesCollector.onApplicationEvent(RoutesCollector.java:217) ~[camel-spring-boot-2.19.3.jar:2.19.3]
... 13 common frames omitted
Caused by: org.eclipse.paho.client.mqttv3.MqttException: Unable to connect to server
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79) ~[org.eclipse.paho.client.mqttv3-1.1.0.jar:na]
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650) ~[org.eclipse.paho.client.mqttv3-1.1.0.jar:na]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_121]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70) ~[org.eclipse.paho.client.mqttv3-1.1.0.jar:na]

我的第一个想法是在所有涉及 Paho 的路线上禁用自动启动并手动启动它们.这不起作用,因为即使路线本身没有启动,PahoEndpoint 也已启动.

My first idea was to disable auto startup on all routes involving Paho and start them manually. This does not work as the PahoEndpoint is started even when the route itself is not started.

我现在正在寻找一种替代方法来处理这个问题.

I'm now looking for an alternative approach to handle this problem.

推荐答案

Apache Camel 2.20 之后的版本提供了一个新功能,让启动过程使用 superviser controller 来运行,它接管了 code>CamelContext 本身.这允许设置更高级的配置以让控制器处理错误,并尝试重试启动失败的路由等.

Apache Camel 2.20 onwards comes with a new functionality to let the startup procedure run using a superviser controller that takes over starting up from CamelContext itself. This allows to setup more advanced configuration to let the controller handle errors, and attempt to retry starting the failed routes etc.

有一个通过 Spring Boot 配置的示例,但您也可以从 Java API 配置它:https://github.com/apache/camel/tree/master/examples/camel-example-spring-boot-supervising-route-controller

There is an example where its configured via Spring Boot, but you can configure it from Java API as well: https://github.com/apache/camel/tree/master/examples/camel-example-spring-boot-supervising-route-controller

在即将发布的版本中,我们将改进这一新功能.对于旧版本的 Camel,通常是组件本身可能提供或不提供任何类型的重试机制,您需要进行配置,而不是快速失败.

In the upcoming releases we will improve on this new functionality. For older versions of Camel then its often the component itself that may or may not offer any kind of retry mechanism, you need to configure, instead of failing fast.

这篇关于即使无法访问 MQTT 服务器,如何启动骆驼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆