java - 使用 ActiveMQ、Camel 和 Spring 实现请求-回复模式

标签 java design-patterns jms activemq apache-camel

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

逐行读取 CSV 文件,然后读取每一行:

  1. 根据该行包含的值构建请求
  2. 将请求发送到消息队列
  3. 其他组件需要获取消息、处理请求并将响应发送到另一个消息队列(生产者已知,因此生产者可以获取响应)。

我相信 request-reply pattern符合要求。 我安装了 ActiveMQ,下载了 camel 并尝试使用他们的 jms 项目。

在配置组件、队列和测试连接(有效)之后,我试图弄清楚如何实际实现请求-回复?我没有找到任何好的examples

我有一个路由生成器

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 上下文.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. 您可以推荐哪些关于上述内容的快速入门指南?

编辑

我得到了下面的代码。 现在假设我在处理器中创建了响应。 如何寄回?如何使用响应?

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"));
            }
        });
    }
}

最佳答案

我身边有一些类似的东西,所以我改变了它,它就在这里。请注意,第二条路线不需要明确知道请求/回复消息,只有生产者需要知道这一点。如果有对目标集的回复(由 Camel 自动处理),第二条路线将回复。

我不知道有什么好的例子,但是 this doc page非常全面,有小例子。

 <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>

关于java - 使用 ActiveMQ、Camel 和 Spring 实现请求-回复模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16243433/

相关文章:

delphi - 是否有用于Delphi/Free Pascal的免费JMS客户端?

Java - 如何制作 JButton "Deselected"

design-patterns - Windows 服务的六边形架构/端口和适配器架构。正确的路?

php - 哪个类应该取决于哪个类基于重要性级别

java - JMS 确认异步消息

java - 用于加密消息的 Qpid 和 JNDI

java - 按字母顺序排序相当慢

java - 将参数传递给 java 的 List 类型的构造函数方法

java - 固定线程池和大量任务的线程问题

javascript - Pub/Sub 中的发布者应该是同步还是异步?