Deploy Request-Reply Template with ActiveMQ, Camel, and Spring

I am trying to implement the following functions:

Read the CSV file one at a time, then for each line:

  • A request to build based on the values ​​contained in a string
  • Send a request to the message queue
  • Other components need to pick up a message, process the request and send a response to another message queue (known to the manufacturer so that the producer can get a response).

I believe that the request-response template matches the invoice. I installed ActiveMQ, downloaded the camel and tried to use my jms project.

After setting up the component queue and test connection (worked), I tried to figure out how to actually implement the request-response? I could not find 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-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> 

Questions:

  • How can I read the line by line of a file and send a message based on the contents of the line?
  • How to set up a route and how to set up a message header to receive a response in a temporary queue that will be deleted after a response is answered
  • What recommendations for a quick launch of the above can you recommend?

EDIT

I got the code below. Now let's say that in the Processor I create an answer. How can I send it back? How can I use the answer?

 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")); } }); } } 
+6
source share
1 answer

I just had something similar, so I redid it, and here it is. Please note that the 2nd route should not be explicitly aware of the response message, only the manufacturer should know this. The 2nd route will answer if there is an answer to the given set (which is automatically processed by the camel).

I do not know a single good example, but this page of the document is really limited to 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> 
+9
source

Source: https://habr.com/ru/post/943742/


All Articles