28.5. Samples
JMS 也在许多示例中用于其他组件。但是,我们提供了几个示例来开始。
28.5.1. 从 JMS 接收 复制链接链接已复制到粘贴板!
在以下示例中,我们将配置一个路由,该路由接收 JMS 信息并将消息路由到 POJO:
from("jms:queue:foo"). to("bean:myBusinessLogic");
from("jms:queue:foo").
to("bean:myBusinessLogic");
您可以使用任何 EIP 模式,以便路由基于上下文。例如,这里的如何为大消耗器过滤订购主题:
from("jms:topic:OrdersTopic"). filter().method("myBean", "isGoldCustomer"). to("jms:queue:BigSpendersQueue");
from("jms:topic:OrdersTopic").
filter().method("myBean", "isGoldCustomer").
to("jms:queue:BigSpendersQueue");
28.5.2. 发送到 JMS 复制链接链接已复制到粘贴板!
在以下示例中,我们轮询文件文件夹,并将文件内容发送到 JMS 主题。当我们希望将文件的内容作为 TextMessage
而不是 BytesMessage
一样,我们需要将正文转换为 字符串
:
from("file://orders"). convertBodyTo(String.class). to("jms:topic:OrdersTopic");
from("file://orders").
convertBodyTo(String.class).
to("jms:topic:OrdersTopic");
28.5.3. 使用注解 复制链接链接已复制到粘贴板!
Camel 还具有注释,因此您可以使用 POJO Consuming 和 POJO Producing。
28.5.4. Spring DSL 示例 复制链接链接已复制到粘贴板!
前面的示例使用 Java DSL。Camel 还支持 Spring XML DSL。以下是使用 Spring DSL 的大消耗的示例:
28.5.5. 其他示例 复制链接链接已复制到粘贴板!
JMS 出现在其他组件和 EIP 模式的许多示例中,以及此 Camel 文档。因此,可以浏览文档。
28.5.6. 使用 JMS 作为 Dead Letter Queue Store Exchange 复制链接链接已复制到粘贴板!
通常,当使用 JMS 作为传输时,它仅传输正文和标头作为有效负载。如果要将 JMS 与 Dead Letter Channel 搭配使用,使用 JMS 队列作为 Dead Letter Queue,则原因的例外通常不会存储在 JMS 消息中。但是,您可以在 JMS 死信队列中使用 transferExchange
选项来指示 Camel 将整个 Exchange 存储在队列中,作为包含 org.apache.camel.support.DefaultExchangeHolder
的 javax.jms.ObjectMessage
。这可让您从 Dead Letter Queue 中使用,并使用密钥 Exchange.EXCEPTION_CAUGHT
从 Exchange 属性中检索原因异常。以下演示展示了这一点:
// setup error handler to use JMS as queue and store the entire Exchange errorHandler(deadLetterChannel("jms:queue:dead?transferExchange=true"));
// setup error handler to use JMS as queue and store the entire Exchange
errorHandler(deadLetterChannel("jms:queue:dead?transferExchange=true"));
然后,您可以从 JMS 队列中消耗并分析问题:
28.5.7. 使用 JMS 作为只存储错误的 Dead Letter Channel 复制链接链接已复制到粘贴板!
您可以使用 JMS 存储原因错误消息或存储自定义正文,您可以自行初始化。以下示例使用 Message Translator EIP 在移到 JMS 死信队列前对失败的交换进行转换:
// we sent it to a seda dead queue first errorHandler(deadLetterChannel("seda:dead")); // and on the seda dead queue we can do the custom transformation before its sent to the JMS queue from("seda:dead").transform(exceptionMessage()).to("jms:queue:dead");
// we sent it to a seda dead queue first
errorHandler(deadLetterChannel("seda:dead"));
// and on the seda dead queue we can do the custom transformation before its sent to the JMS queue
from("seda:dead").transform(exceptionMessage()).to("jms:queue:dead");
在这里,我们只会将原始原因错误消息存储在转换中。但是,您可以使用任何表达式来发送您需要的任何表达式。例如,您可以在 Bean 上调用方法或使用自定义处理器。