java – 使用ActiveMQ,Camel和Spring实现Request-Reply模式
发布时间:2020-12-14 06:00:53 所属栏目:Java 来源:网络整理
导读:我正在尝试实现以下功能: 然后逐行读取CSV文件: 根据该行包含的值构建请求 将请求发送到消息队列 其他组件需要获取消息,处理请求并将响应发送到另一个消息队列(生产者已知,因此生产者可以获取响应). 我相信request-reply pattern符合要求. 我安装了ActiveM
我正在尝试实现以下功能:
然后逐行读取CSV文件: >根据该行包含的值构建请求 我相信request-reply pattern符合要求. 配置组件,队列和测试连接(工作)后,我试图弄清楚实际上如何实现请求 – 回复?我没找到任何好的examples 我有一个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"); } } 骆驼的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> 问题: >如何读取文件逐行构造并根据行内容发布消息? 编辑 我得到了下面的代码工作. 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")); } }); } } 解决方法
我只是有类似的东西,所以我改变它,它在这里.请注意,第二条路径不需要明确地知道请求/回复消息,只有生产者需要知道.如果有对目的地集的回复(由驼峰自动处理),第二条路线将回复.
我不知道有什么好的例子,但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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |