我需要编写一个Apache Camel路由
从Active-MQ JMS-Queue接收消息(包含文件位置)。 使用来自JMS-Queue的received-message中的location读取文件内容。 将该文件内容发送到另一个Active-MQ JMS-Queue。我可以编写两个单独的路由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");我知道在路线中不可能使用两个“从”。 但是我如何使用Apache Camel来设置这种逻辑呢?
伙计们请建议我解决方案,我也准备使用两个Camel路由来实现这个逻辑。
I need to write a single Apache Camel route to
Receive Message (which contain File-Location) from Active-MQ JMS-Queue. Using location in received-message from JMS-Queue read file content. Send that file content to another Active-MQ JMS-Queue.I can write two separate route 1) To get message from Active-MQ and 2) To read from a file in folder using static file-name and send to JMS-queue. But my requirement is to read the content only from those files, for which I am getting details from JMS queue. Means reading content from files is selective and based on condition.
Following is the sample Java DSL Route configuration I required for it.
from("activemq:queue:filelocationQueue") .from("file://<<File-Location from JMS-Queue>>?noop=true") .convertBodyTo(String.class) .to("activemq:queue:fileContent");I know it is not possible to use two "from" inside a route. But how can I put this kind of logic using Apache Camel?
Guys please suggest me the solution, I am also ready to use two Camel route to implement this logic.
最满意答案
您可以在处理器内部使用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接收的消息体包含要使用的文件的确切路径,例如file:/ home / user / input?noop = true& fileName = file.txt 。 请注意,只能使用一个文件的唯一方法是使用fileName URI选项。 否则,您将使用该文件夹中的所有文件。
You could use Camel's ConsumerTemplate inside Processor to get what you need like this:
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");Above assumes that the message body received from the filelocationQueue contains exact path to the file to be consumed, e.g. file:/home/user/input?noop=true&fileName=file.txt. Note that the only way you can consume only one file is to use fileName URI option. Otherwise you will be consuming all the files in that folder.
更多推荐
Camel,Active-MQ,JMS-Queue,使用,电脑培训,计算机培训,IT培训"/> <meta name="
发布评论