Java – use ActiveMQ, camel and spring to implement the request reply mode
I'm trying to implement the following features:
Then read the CSV file line by line:
>Build the request according to the value contained in this line > send the request to the message queue > other components need to get the message, process the request and send the response to another message queue (the producer knows, so the producer can get the response)
I believe the request reply pattern meets the requirements I installed ActiveMQ, downloaded camel and tried to use their JMS project
After configuring components, queues and test connections (work), I try to figure out how to actually implement request reply? I didn't find any good examples
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's 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>
Question:
>How to read a file, construct it line by line, and publish a message according to the line content? > How to configure routes and headers to get responses in a temporary queue that will be deleted after getting responses? > What quick start guides can you recommend?
edit
I got the following code work Now let's say I create a response in the processor How can I send it back? How do I respond?
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")); } }); } }
Solution
I just have something similar, so I change it, it's here Please note that the second path does not need to know the request / reply message explicitly, only the producer needs to know If there is a reply to the destination set (automatically processed by the hump), the second route will reply
I don't know any good examples, but this doc page is very comprehensive, with only 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>