Apache Camel 单一路由从多个源获取消息
Apache Camel single route to get message from multiple source
我需要写一个 Apache Camel 路由到
- 从 Active-MQ JMS-Queue 接收消息(包含文件位置)。
- 使用来自 JMS 队列的接收消息中的位置读取文件内容。
- 将该文件内容发送到另一个 Active-MQ JMS 队列。
我可以编写两个单独的路由 1) 从 Active-MQ 获取消息和
2)使用静态文件名从文件夹中的文件读取并发送到 JMS 队列。
但我的要求是只从那些文件中读取内容,为此我从 JMS 队列中获取详细信息。意味着从文件中读取内容是有选择性的并基于条件。
以下是示例 Java 我需要的 DSL 路由配置。
from("activemq:queue:filelocationQueue")
.from("file://<<File-Location from JMS-Queue>>?noop=true")
.convertBodyTo(String.class)
.to("activemq:queue:fileContent");
我知道不可能在一条路线中使用两个 "from"。但是我怎样才能使用 Apache Camel 来实现这种逻辑呢?
大佬们请教我解决方案,我也准备用两条Camel路由来实现这个逻辑。
您可以在 Processor 中使用 Camel 的 ConsumerTemplate 来获取您需要的内容,如下所示:
from("activemq:queue:filelocationQueue")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// "file://<<File-Location from JMS-Queue>>?noop=true"
String fileLocation = exchange.getIn().getBody(String.class);
ConsumerTemplate template = getContext().createConsumerTemplate();
// This is like your second "from". Use 2 second timeout (2000 ms).
Exchange fileExchange = template.receive(fileLocation,2000);
exchange.getOut().setBody(fileExchange.getIn().getBody());
template.doneUoW(fileExchange);
}
})
.convertBodyTo(String.class, "UTF-8")
.to("activemq:queue:fileContent");
以上假定从 filelocationQueue 接收到的消息正文包含要使用的文件的确切路径,例如文件:/home/user/input?noop=true&文件名=file.txt。请注意,您可以使用 only 一个文件的唯一方法是使用 fileName URI 选项。否则您将使用该文件夹中的所有文件。
您可以使用 Content Enricher EIP 及其 pollEnrich 方法:
from("direct:start")
.pollEnrich("file:inbox?fileName=data.txt")
.to("direct:result");
pollEnrich 的 URI 是使用当前 Exchange 中的值动态计算的。
我需要写一个 Apache Camel 路由到
- 从 Active-MQ JMS-Queue 接收消息(包含文件位置)。
- 使用来自 JMS 队列的接收消息中的位置读取文件内容。
- 将该文件内容发送到另一个 Active-MQ JMS 队列。
我可以编写两个单独的路由 1) 从 Active-MQ 获取消息和 2)使用静态文件名从文件夹中的文件读取并发送到 JMS 队列。 但我的要求是只从那些文件中读取内容,为此我从 JMS 队列中获取详细信息。意味着从文件中读取内容是有选择性的并基于条件。
以下是示例 Java 我需要的 DSL 路由配置。
from("activemq:queue:filelocationQueue")
.from("file://<<File-Location from JMS-Queue>>?noop=true")
.convertBodyTo(String.class)
.to("activemq:queue:fileContent");
我知道不可能在一条路线中使用两个 "from"。但是我怎样才能使用 Apache Camel 来实现这种逻辑呢?
大佬们请教我解决方案,我也准备用两条Camel路由来实现这个逻辑。
您可以在 Processor 中使用 Camel 的 ConsumerTemplate 来获取您需要的内容,如下所示:
from("activemq:queue:filelocationQueue")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// "file://<<File-Location from JMS-Queue>>?noop=true"
String fileLocation = exchange.getIn().getBody(String.class);
ConsumerTemplate template = getContext().createConsumerTemplate();
// This is like your second "from". Use 2 second timeout (2000 ms).
Exchange fileExchange = template.receive(fileLocation,2000);
exchange.getOut().setBody(fileExchange.getIn().getBody());
template.doneUoW(fileExchange);
}
})
.convertBodyTo(String.class, "UTF-8")
.to("activemq:queue:fileContent");
以上假定从 filelocationQueue 接收到的消息正文包含要使用的文件的确切路径,例如文件:/home/user/input?noop=true&文件名=file.txt。请注意,您可以使用 only 一个文件的唯一方法是使用 fileName URI 选项。否则您将使用该文件夹中的所有文件。
您可以使用 Content Enricher EIP 及其 pollEnrich 方法:
from("direct:start")
.pollEnrich("file:inbox?fileName=data.txt")
.to("direct:result");
pollEnrich 的 URI 是使用当前 Exchange 中的值动态计算的。