使用ActiveMQ,Camel和Spring实现请求回复模式 [英] Implement Request-Reply pattern using ActiveMQ, Camel and Spring

查看:438
本文介绍了使用ActiveMQ,Camel和Spring实现请求回复模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下功能:

I am trying to implement the following functionality:

每行都逐行读取CSV文件:

Read CSV file line-by-line then for every line:


  1. 根据行包含的值生成请求

  2. 将请求发送到消息队列

  3. 其他组件需要接收消息,处理请求并将响应发送到另一个消息队列(生产者已知,因此生产者可以接收响应)。

我相信请求回复模式符合账单。
我安装了ActiveMQ,下载了骆驼并试图使用他们的jms项目。

I believe that the request-reply pattern fits the bill. I installed ActiveMQ, downloaded camel and tried to use their jms project.

配置组件后,队列和测试连接(工作),我试图弄清楚如何实际请求回复?我找不到任何好的示例

After configuring the component, the queue and testing connection (worked), I tried to figure out how actually to implement request-reply? I failed to find any good examples

我有一个RouteBuilder

I have a RouteBuilder

RouteBuilder

public class MyRouteBuilder extends RouteBuilder {
    public static void main(String[] args) throws Exception {
        new Main().run(args);
    }

    public void configure() {
        from("file:src/data?noop=true")
        .to("activemq:RequestQ");

        from("activemq:RequestQ?exchangePattern=InOut&timeToLive=5000") 
        .inOut("activemq:RequestQ", "bean:myBean?method=someMethod"); 
    }
}

camel-context.xml

<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring
         http://camel.apache.org/schema/spring/camel-spring.xsd">

    <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <package>org.apache.camel.example.spring</package>
    </camelContext>

    <bean id="jmsConnectionFactory" 
        class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616" />
    </bean>

    <bean id="pooledConnectionFactory" 
        class="org.apache.activemq.pool.PooledConnectionFactory" 
        init-method="start" destroy-method="stop">
        <property name="maxConnections" value="8" />
        <property name="connectionFactory" ref="jmsConnectionFactory" />
    </bean>

    <bean id="jmsConfig" 
        class="org.apache.camel.component.jms.JmsConfiguration">
        <property name="connectionFactory" ref="pooledConnectionFactory"/>
        <property name="concurrentConsumers" value="10"/>
    </bean>

    <bean id="activemq" 
        class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="configuration" ref="jmsConfig"/>
    </bean>

    <bean id="myBean" class="org.apache.camel.example.spring.MyBean"/>
</beans>

问题:


  1. 如何读取文件逐行构造并根据行内容发布消息?

  2. 如何配置路由以及如何配置消息头以便在临时队列中获得响应,该响应将在收到响应后被删除?

  3. 您可以推荐上述哪些快速入门指南?

编辑

我的代码下面工作。
现在让我们说在处理器中创建响应。
如何发回?我如何消费回应?

I got the code below working. Now lets say that in the Processor I create the response. How can I send it back? How can I consume the response?

public class MyRouteBuilder extends RouteBuilder {

    public static void main(String[] args) throws Exception {
        new Main().run(args);
    }

    public void configure() {
        from("file:/Users/aviad/ws/integ/src/data?fileName=lines.txt&noop=true&idempotent=true")
        .split()
        .tokenize("\\n")
        .inOut("activemq:req");

        from("activemq:req")
        .process(new Processor() {
            public void process(Exchange exchange) throws Exception {
                System.out.println(exchange.getIn().getBody(String.class));
                System.out.println("jmscorrelationid=" + exchange.getIn().getHeader("jmscorrelationid"));
                System.out.println("jmsdestination=" + exchange.getIn().getHeader("jmsdestination"));
            }
        });
    }
}


推荐答案

只是有一些类似的东西,所以我改变它在这里是。请注意,第二条路由不需要明确地知道请求/回复消息,只有生产者需要知道。如果对目的地设置有回复(由骆驼自动处理),则第二条路线将会回复。

I just had something similar around, so I alterd it and here it is. Note that the 2nd route does not need to be explicitly aware of a request/reply message, only the producer needs to know that. The 2nd route will reply if there is a reply to destination set (which is handled automagically by camel).

我不知道任何好的例子,但这个文档页面是非常全面的小例子。

I don't know of any good example, but this doc page is really comprehensive with small examples.

 <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
        <route>
          <from uri="file://c:/apps/in"/>
          <split>
            <tokenize token="\n"/>
            <to uri="activemq:req" pattern="InOut"/>
            <to uri="stream:out"/><!-- print Hello to console -->
          </split>
        </route>
        <route>
          <from uri="activemq:req"/>
            <transform>
              <simple>Hello ${in.body}</simple>
            </transform>
        </route>
    </camelContext>

这篇关于使用ActiveMQ,Camel和Spring实现请求回复模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆