创建线程并将消息从 Camel 路由到微服务并返回

Create thread and route message from Camel to microservice and back

我正在将 Camel 与 JMS 和 Spring 引导一起使用,并想为下一个场景构建路由:

用户 1(MQTT 客户端)向 ActiveMQ Artemis 发送消息(主题)。 骆驼(通过使用 from)捕获该消息并在日志的帮助下将其打印出来。

我想做的是 - 为捕获的消息创建一个新线程(异步)。将该消息从 Camel 发送到微服务(python 程序),微服务应该接收消息并插入一些额外的字符串,然后将更改后的消息发送回 Camel 和 ActiveMQ。 最后,来自 ActiveMQ 的更改消息将发送给用户 2。

你能给我一些指示或路线示例,说明如何做这样的事情吗?

因此,关键点是为每条消息创建新线程并创建往返于该微服务的路由。

路线可能如下所示

from("jms:queue:inputQueue")
    .log("${body}")
    .to("http://oldhost")
    .to("jms:queue:outputQueue")

一些注意事项:

  • 您可以使用 .to() 调用下游 HTTP 端点。当前消息正文用作请求正文。服务的响应覆盖消息正文。
  • 我不知道你为什么要为同步调用创建一个新线程。您可以通过与多个消费者并行使用来自 Artemis 的多条消息来利用并行处理。像这样,每条消息都在自己的线程中处理。如果你的动力是韧性,Camel
  • 中也有一个Circuit Breaker EIP
  • 如果您使用 Camel 2.x,请使用 HTTP4 Component ("http4://") to use the newer HTTP client lib. In Camel 3.x the old one was dropped and the new one is simply called HTTP component