title: Implement Request-Reply pattern using ActiveMQ, Camel and Spring
body: <p>I am trying to implement the following functionality:</p>
  
  <p>Read CSV file line-by-line then for every line:</p>
  
  <ol>
  <li>Build request based on the values that the line contains</li>
  <li>Send the request to the message queue</li>
  <li>Other component needs to pick up the message, process the request and send the response to another message queue (known to producer, so the producer could pick up the response). </li>
  </ol>
  
  <p>I believe that the <a href="http://www.enterpriseintegrationpatterns.com/RequestReply.html" rel="noreferrer">request-reply pattern</a> fits the bill.
  I installed ActiveMQ, downloaded camel and tried to use their jms project.</p>
  
  <p>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 <a href="http://camel.apache.org/request-reply.html" rel="noreferrer">examples</a> </p>
  
  <p>I have a RouteBuilder </p>
  
  <p><strong>The RouteBuilder</strong></p>
  
  <pre><code>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&amp;timeToLive=5000") 
          .inOut("activemq:RequestQ", "bean:myBean?method=someMethod"); 
      }
  }
  </code></pre>
  
  <p><strong>camel-context.xml</strong></p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"&gt;
          &lt;package&gt;org.apache.camel.example.spring&lt;/package&gt;
      &lt;/camelContext&gt;
  
      &lt;bean id="jmsConnectionFactory" 
          class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
          &lt;property name="brokerURL" value="tcp://localhost:61616" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="pooledConnectionFactory" 
          class="org.apache.activemq.pool.PooledConnectionFactory" 
          init-method="start" destroy-method="stop"&gt;
          &lt;property name="maxConnections" value="8" /&gt;
          &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jmsConfig" 
          class="org.apache.camel.component.jms.JmsConfiguration"&gt;
          &lt;property name="connectionFactory" ref="pooledConnectionFactory"/&gt;
          &lt;property name="concurrentConsumers" value="10"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="activemq" 
          class="org.apache.activemq.camel.component.ActiveMQComponent"&gt;
          &lt;property name="configuration" ref="jmsConfig"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="myBean" class="org.apache.camel.example.spring.MyBean"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Questions:</strong></p>
  
  <ol>
  <li>How can I read a file line-by-line construct and post a message based on line content?</li>
  <li>How to configure the route and how to configure the message header in order to get response in temporary queue that will be deleted after the response was picked up?</li>
  <li>What quick-start guides for the above can you recommend?</li>
  </ol>
  
  <p><strong>EDIT</strong></p>
  
  <p>I got the code below working.
  Now lets say that in the Processor I create the response.
  <strong>How can I send it back? How can I consume the response?</strong></p>
  
  <pre><code>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&amp;noop=true&amp;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"));
              }
          });
      }
  }
  </code></pre>