On Mon, Oct 19, 2009 at 3:55 PM, linuca <[email protected]> wrote:
>
> Hi,
>
> This is my first attempt at coding a request reply pattern with Camel
> (please be gentle :blush:). This is what I want to do: I have a Master and a
> Slave class. The Master class sends an sql query to the Slave class. The
> latter executes the query, fills the result in a List and then returns that
> List to the Master. These classes will be in different hosts in the future.
> This is the code of what I have so far:
>
> (does anyone know how to make the <code> tags work?)
>
> The Master class:
>
> <code>
>
> CamelContext context = new DefaultCamelContext();
> ConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory("tcp://localhost:61616");
> context.addComponent("activemq",
> ActiveMQComponent.jmsComponentAutoAcknowledge(connectionFactory));
> ProducerTemplate template = context.createProducerTemplate();
> context.start();
> template.requestBody("activemq:queue:test.queue", "select * from clients");
> Thread.sleep(1000);
> context.stop();
>
> </code>
>
> The Slave class:
>
> <code>
>
> CamelContext context = new DefaultCamelContext();
> ConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory("tcp://localhost:61616");
> context.addComponent("activemq",
> ActiveMQComponent.jmsComponentAutoAcknowledge(connectionFactory));
> context.addRoutes(new RouteBuilder() {
> public void configure() {
> from("activemq:queue:test.queue").process(new
> Processor() {
> public void process(Exchange e) {
> try {
> DefaultMessage msg = (DefaultMessage)
> e.getIn();
> String clientsQuery = (String)
> msg.getBody();
> List result =
> Db.getDbResultList(clientsQuery);
> e.getOut(true).setBody(result);
> } catch(Exception ex) {
> logger.debug("slave process exception", ex);
> }
> }
> });
> }
> });
>
> context.start();
>
> </code>
>
> And that's all I've got.
> I realize that the query is received and executed successfully. But I don't
> know if the List is returned and I don't know how to read it from the
> master.
>
> Thanks in advance.
> --
> View this message in context:
> http://www.nabble.com/request-reply-with-sql-query-tp25958657p25958657.html
> Sent from the Camel - Users (activemq) mailing list archive at Nabble.com.
>
>
Hi
Welcome on the Camel ride.
You actually does it right. What you just need is to grab the result
where you send from the Slave to the Master.
So this
> template.requestBody("activemq:queue:test.queue", "select * from clients");
Should be
Object reply = template.requestBody("activemq:queue:test.queue",
"select * from clients");
Then you can check that the reply is that List.
And you can take advantage of Camels type system to avoid the ugly
type casts and verbose coding
> DefaultMessage msg = (DefaultMessage)
> e.getIn();
> String clientsQuery = (String)
Could become
String clientQuery = exchange.getIn().getBody(String.class);
--
Claus Ibsen
Apache Camel Committer
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus