You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Matvey <ma...@inbox.ru> on 2009/01/20 07:56:49 UTC

Newbie: Mapping JMS Camel / Spring to POJO, ActiveMQ Queues

I need to configure Camel DSL / routes in such a way that on of my old
classes gets objects from JMS queue and sends objects to another JMS queue.


I can use Camel API to do that, but it seems like in such a case I almost do
not use Camel:

CamelContext context = new DefaultCamelContext();
        context.addComponent("activemq",
ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
        context.start();
        Endpoint endpoint =
context.getEndpoint("activemq:test.queue?concurrentConsumers=2");
 	PollingConsumer consumer = endpoint.createPollingConsumer();


It is still not clear for me how should I do that in a "Camel way", using
Spring, annotations and mapping.

 Also one more important question: we have 1000 messages in a queue, POJO
class gets a message, as it mapped for this queue (analog of onMessage()
method), I need some time to process this message (sure there are multiple
threads), and sometimes I can not process this message immediately, so I
have 2 options: 1. to wait (either with Thread.sleep() or waiting for an
event) or 2. put this message back to queue. The question is - may I just
wait (case 1) or it's necessary to throw an exception  and return this
message back to queue?
-- 
View this message in context: http://www.nabble.com/Newbie%3A-Mapping-JMS-Camel---Spring-to-POJO%2C-ActiveMQ-Queues-tp21558031s22882p21558031.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Newbie: Mapping JMS Camel / Spring to POJO, ActiveMQ Queues

Posted by Ashwin Karpe <as...@progress.com>.
Hi Matvey,

The approach of throwing an exception and returning the message to the queue
in order to process another message that can be processed quicker is not a
good solution. It is prrocessing expense incurred for no real value.

I would rather create 2 queues or 1 virtual queue/topic in activemq and
direct payloads of lower size to one queue and the larger payloads to
another. Thene you can separate your consumers to process the messages more
optimally based on your specific requirements.

Note that the Camel route need not change and this would be a simple
configuration setting in activemq.xml

You may want to check this out.
    http://activemq.apache.org/virtual-destinations.html

Cheers,

Ashwin...

Matvey wrote:
> 
> I need to configure Camel DSL / routes in such a way that on of my old
> classes gets objects from JMS queue and sends objects to another JMS
> queue.
> 
> 
> I can use Camel API to do that, but it seems like in such a case I almost
> do not use Camel:
> 
> CamelContext context = new DefaultCamelContext();
>         context.addComponent("activemq",
> ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
>         context.start();
>         Endpoint endpoint =
> context.getEndpoint("activemq:test.queue?concurrentConsumers=2");
>  	PollingConsumer consumer = endpoint.createPollingConsumer();
> 
> 
> It is still not clear for me how should I do that in a "Camel way", using
> Spring, annotations and mapping.
> 
>  Also one more important question: we have 1000 messages in a queue, POJO
> class gets a message, as it mapped for this queue (analog of onMessage()
> method), I need some time to process this message (sure there are multiple
> threads), and sometimes I can not process this message immediately, so I
> have 2 options: 1. to wait (either with Thread.sleep() or waiting for an
> event) or 2. put this message back to queue. The question is - may I just
> wait (case 1) or it's necessary to throw an exception  and return this
> message back to queue?
> 


-----
--- 
Ashwin Karpe, Principal Consultant, PS - Opensource Center of Competence 
Progress Software Corporation
14 Oak Park Drive
Bedford, MA 01730
--- 
+1-972-304-9084 (Office) 
+1-972-971-1700 (Mobile) 
---- 
Blog: http://opensourceknowledge.blogspot.com/


-- 
View this message in context: http://www.nabble.com/Newbie%3A-Mapping-JMS-Camel---Spring-to-POJO%2C-ActiveMQ-Queues-tp21558031s22882p21561805.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: Newbie: Mapping JMS Camel / Spring to POJO, ActiveMQ Queues

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Yeah the annotations is really nice once and quite amazing what you
can do in that single annotation.

You could also look at this tutorial that explains about using JMS
with Camel and Spring:
http://camel.apache.org/tutorial-jmsremoting.html

It however is focused on showing 3 difference client options how to
send a JMS message to a queue using Camel and Spring.


On Tue, Jan 20, 2009 at 8:11 AM, James Strachan
<ja...@gmail.com> wrote:
> 2009/1/20 Matvey <ma...@inbox.ru>:
>>
>> I need to configure Camel DSL / routes in such a way that on of my old
>> classes gets objects from JMS queue and sends objects to another JMS queue.
>>
>>
>> I can use Camel API to do that, but it seems like in such a case I almost do
>> not use Camel:
>>
>> CamelContext context = new DefaultCamelContext();
>>        context.addComponent("activemq",
>> ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
>>        context.start();
>>        Endpoint endpoint =
>> context.getEndpoint("activemq:test.queue?concurrentConsumers=2");
>>        PollingConsumer consumer = endpoint.createPollingConsumer();
>>
>>
>> It is still not clear for me how should I do that in a "Camel way", using
>> Spring, annotations and mapping.
>
> public class MyConsumer {
>
>  @Consume(uri="activemq:test.queue?concurrentConsumers=2")
>  public void myConsumeMethod(String payload) {... }
>
> }
>
>
>>  Also one more important question: we have 1000 messages in a queue, POJO
>> class gets a message, as it mapped for this queue (analog of onMessage()
>> method), I need some time to process this message (sure there are multiple
>> threads), and sometimes I can not process this message immediately, so I
>> have 2 options: 1. to wait (either with Thread.sleep() or waiting for an
>> event) or 2. put this message back to queue. The question is - may I just
>> wait (case 1) or it's necessary to throw an exception  and return this
>> message back to queue?
>
> You can just wait if you like. I guess it depends; are there other
> messages you could be getting on with processing - or do you have to
> wait for some external resource before processing any more messages?
>
> If you can't process any messages at all; just wait. If you just can't
> handle that message right now but its likely you can process others,
> you could always send a copy of the message to the back of the queue
> and consume the current message - though only do this if a small
> subset of the messages cannot be processed. i.e. if no messages can be
> processed you are far better just waiting rather than spinning your
> system as fast as it'll go (consuming & sending messages non stop to
> no avail :)
>
> --
> James
> -------
> http://macstrac.blogspot.com/
>
> Open Source Integration
> http://fusesource.com/
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/

Re: Newbie: Mapping JMS Camel / Spring to POJO, ActiveMQ Queues

Posted by James Strachan <ja...@gmail.com>.
2009/1/20 Matvey <ma...@inbox.ru>:
>
> I need to configure Camel DSL / routes in such a way that on of my old
> classes gets objects from JMS queue and sends objects to another JMS queue.
>
>
> I can use Camel API to do that, but it seems like in such a case I almost do
> not use Camel:
>
> CamelContext context = new DefaultCamelContext();
>        context.addComponent("activemq",
> ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
>        context.start();
>        Endpoint endpoint =
> context.getEndpoint("activemq:test.queue?concurrentConsumers=2");
>        PollingConsumer consumer = endpoint.createPollingConsumer();
>
>
> It is still not clear for me how should I do that in a "Camel way", using
> Spring, annotations and mapping.

public class MyConsumer {

  @Consume(uri="activemq:test.queue?concurrentConsumers=2")
  public void myConsumeMethod(String payload) {... }

}


>  Also one more important question: we have 1000 messages in a queue, POJO
> class gets a message, as it mapped for this queue (analog of onMessage()
> method), I need some time to process this message (sure there are multiple
> threads), and sometimes I can not process this message immediately, so I
> have 2 options: 1. to wait (either with Thread.sleep() or waiting for an
> event) or 2. put this message back to queue. The question is - may I just
> wait (case 1) or it's necessary to throw an exception  and return this
> message back to queue?

You can just wait if you like. I guess it depends; are there other
messages you could be getting on with processing - or do you have to
wait for some external resource before processing any more messages?

If you can't process any messages at all; just wait. If you just can't
handle that message right now but its likely you can process others,
you could always send a copy of the message to the back of the queue
and consume the current message - though only do this if a small
subset of the messages cannot be processed. i.e. if no messages can be
processed you are far better just waiting rather than spinning your
system as fast as it'll go (consuming & sending messages non stop to
no avail :)

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Newbie: Mapping JMS Camel / Spring to POJO, ActiveMQ Queues

Posted by James Strachan <ja...@gmail.com>.
2009/1/20 Matvey <ma...@inbox.ru>:
>
> I still have several questions about Spring/JMS/Camel/POJO mapping.
>
> Question 1
> for example I have Spring config file:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <beans xmlns="http://www.springframework.org/schema/beans"
>       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>       xmlns:camel="http://activemq.apache.org/camel/schema/spring"
>       xsi:schemaLocation="
>        http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
>        http://activemq.apache.org/camel/schema/spring
> http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">
>
>  <camelContext id="camel"
>                xmlns="http://activemq.apache.org/camel/schema/spring">
>    <package>org.apache.camel.example.spring</package>
>
>    <!-- define routes -->
>
>    <!-- now lets process messages from the queue locally -->
>    <route>
>      <from uri="jms:Updates"/>
>      <to uri="bean:updates"/>
>    </route>
>  </camelContext>
>
>  <!-- lets configure the default ActiveMQ broker URL -->
>  <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
>    <property name="connectionFactory">
>      <bean class="org.apache.activemq.ActiveMQConnectionFactory">
>        <property name="brokerURL" value="tcp://localhost:61616"/>
>      </bean>
>    </property>
>  </bean>
>
>  <bean id="updates" class="Updates"/>
>
>  <camel:template id="camelTemplate"/>
>
> </beans>
>
>
> i have Updates class:
>
>   public class Updates {
>
>        public static void getUpdate(Object body) throws Exception {
>            System.out.println("\n
> ******************************************************* \n");
>            System.out.println("Received: " + body.getClass());
>            System.out.println("Received: " + body.toString());
>            java.lang.Thread.currentThread().sleep(1500);
>        }
>    }
>
>
> my problem is that I can not explicitly configure URI for this method
> getUpdate only. it works now, but I need several other methods in this class
> and I'm not sure that this will work later.
>
> I have tried this one config
>     <route>
>      <from uri="jms:Updates"/>
>      <to uri="bean:updates:getUpdate"/>
>    </route>
>
> and several other options but without success (I get an exception because
> spring can not map it)
>
> Just do not know the syntax and can not figure it out at the moment.

There's more detail here...

http://camel.apache.org/pojo-consuming.html

e.g. you can just annotate your method with @Consume(uri="jms:Updates")

Or if you want to use an explicit route, use the method property of
the bean endpoint

See
http://camel.apache.org/bean.html

e.g.

<to uri="bean:updates?method=getUpdate"/>


> Question 2
> if I want to add route like this:
>
> <route>
>      <from uri="bean:updatesender"/>
>      <to uri="jms:Updates"/>
> </route>
>  <bean id="updatesender" class="UpdateSender"/>
>
> than how should look the send method like? for example:
>
> public Object sendUpdate()
> {
>  Properties up = new Properties();
>  up.setProperty("UpdatedParameter","FIOM:1244");
>  return up;
> }
> also can I declare it as:
> public static Object sendUpdate(Object up){return up;}

Maybe this will help describe how to send messages from inside your bean...
http://camel.apache.org/pojo-producing.html

-- 
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Re: Newbie: Mapping JMS Camel / Spring to POJO, ActiveMQ Queues

Posted by Matvey <ma...@inbox.ru>.
I still have several questions about Spring/JMS/Camel/POJO mapping.

Question 1
for example I have Spring config file:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://activemq.apache.org/camel/schema/spring"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://activemq.apache.org/camel/schema/spring
http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

  <camelContext id="camel"
                xmlns="http://activemq.apache.org/camel/schema/spring">
    <package>org.apache.camel.example.spring</package>

    <!-- define routes -->
 
    <!-- now lets process messages from the queue locally -->
    <route>
      <from uri="jms:Updates"/>
      <to uri="bean:updates"/>
    </route>
  </camelContext>

  <!-- lets configure the default ActiveMQ broker URL -->
  <bean id="jms" class="org.apache.camel.component.jms.JmsComponent">
    <property name="connectionFactory">
      <bean class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
      </bean>
    </property>
  </bean>

  <bean id="updates" class="Updates"/>

  <camel:template id="camelTemplate"/>

</beans>


i have Updates class:

   public class Updates {
        
        public static void getUpdate(Object body) throws Exception {
            System.out.println("\n
******************************************************* \n");
            System.out.println("Received: " + body.getClass());
            System.out.println("Received: " + body.toString());
            java.lang.Thread.currentThread().sleep(1500);
        }
    }


my problem is that I can not explicitly configure URI for this method
getUpdate only. it works now, but I need several other methods in this class
and I'm not sure that this will work later.

I have tried this one config 
     <route>
      <from uri="jms:Updates"/>
      <to uri="bean:updates:getUpdate"/>
    </route>

and several other options but without success (I get an exception because
spring can not map it)

Just do not know the syntax and can not figure it out at the moment.

Question 2
if I want to add route like this:

<route>
      <from uri="bean:updatesender"/>
      <to uri="jms:Updates"/>
</route>
  <bean id="updatesender" class="UpdateSender"/>

than how should look the send method like? for example: 

public Object sendUpdate()
{
  Properties up = new Properties(); 
  up.setProperty("UpdatedParameter","FIOM:1244");
  return up;
}
also can I declare it as:
public static Object sendUpdate(Object up){return up;}
-- 
View this message in context: http://www.nabble.com/Newbie%3A-Mapping-JMS-Camel---Spring-to-POJO%2C-ActiveMQ-Queues-tp21558031s22882p21565368.html
Sent from the Camel - Users mailing list archive at Nabble.com.