Apache Camel单一路由从多个源获取消息(Apache Camel single route to get message from multiple source)

编程入门 行业动态 更新时间:2024-10-22 07:33:20
Apache Camel单一路由从多个源获取消息(Apache Camel single route to get message from multiple source)

我需要编写一个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="

本文发布于:2023-08-01 19:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1363742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   路由   消息   Camel   Apache

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!