You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Ross Woolf <ro...@rosswoolf.com> on 2015/06/12 01:50:39 UTC

Having problems with CamelContext.addRoutes() and ActiveMQ

I am having problems getting queue data from CamelContext.  I am a
newbie to Camel and so I just don't see what I am doing wrong.

I am using:
Camel 2.15.2
ActiveMQ 5.11.1

I am using the following code to send data to the queue and it works
(all of this is just test code to learn Camel).

public static void putToEventQueue(String eventName, String
eventStatus, String eventMessage) {
   init(); //Starts up ActiveMQ if it is not already started.
   CamelContext context = new DefaultCamelContext();
   ProducerTemplate template = context.createProducerTemplate();
   template.sendBody("vm://localhost:eventQueue", new
EventMessage(eventName, eventStatus, eventMessage));
}

If I use the following code to get data from ActiveMQ then it works
(How I know that putToEventQueue is working).

public void getFromQueue() {
   CamelContext context = new DefaultCamelContext();
   ConsumerTemplate ct = context.createConsumerTemplate();
   Object test = ct.receiveBody("vm://localhost:eventQueue");
   while (test != null) {
      System.out.println("consumer Name:"+ ((EventMessage)
test).getEventName());
      System.out.println("consumer Message:"+ ((EventMessage)
test).getEventMessage());
      test = ct.receiveBody("vm://localhost:eventQueue");
   }
}

But if I use the following CamelContext.addRoutes() code to get at the
data it never retrieves any of the data from the queue.

public void getFromQueue() {
   CamelContext context = new DefaultCamelContext();
   context.addComponent("activemq",
activeMQComponent("vm://localhost?broker.persistent=false"));
   try {
      context.addRoutes(new RouteBuilder() {
         public void configure() throws Exception {
            from("activemq:queue:eventQueue").to("browse:eventReceived");
         }
      });
      context.start();
      Thread.sleep(10000);
      BrowsableEndpoint browse =
context.getEndpoint("browse:eventReceived", BrowsableEndpoint.class);
       List<Exchange> exchanges = browse.getExchanges();
       for (Exchange exchange : exchanges) { //If I set a break point
here I never see data in exchanges.
          Object test = exchange.getIn().getBody();
          System.out.println("consumer Name:"+ ((EventMessage)
test).getEventName());
          System.out.println("consumer Message:"+ ((EventMessage)
test).getEventMessage());
       }
      context.stop();
   } catch (Exception e) {
      e.printStackTrace();
   }
}

I have tried to look closely at the examples of using CamelContext to
create a route from ActiveMQ but I am missing something in the mix
because nothing ever makes it into the BrowsableEndpoint or exchanges
from the ActiveMQ queue.

Any suggestions in helping me to see what I am doing wrong is greatly
appreciated.

Re: Having problems with CamelContext.addRoutes() and ActiveMQ

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

When you use the vm transport, then make sure to use the correct name
as the name defined in the <broker>. If there is no name defined
there, then give it a name.

On Tue, Jul 21, 2015 at 10:17 PM, rwoolf <ro...@rosswoolf.com> wrote:
> I have done as you said, and I use activemq.xml and import a camel.xml file
> that defines the route.  But I still can't get things I put in the queue to
> then be processed via the route.
>
> activemq.xml
> <beans
>   xmlns="http://www.springframework.org/schema/beans"
>   xmlns:amq="http://activemq.apache.org/schema/core"
>   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-2.0.xsd
>   http://activemq.apache.org/schema/core
> http://activemq.apache.org/schema/core/activemq-core.xsd
>   http://camel.apache.org/schema/spring
> http://camel.apache.org/schema/spring/camel-spring.xsd">
>
>
>   <broker xmlns="http://activemq.apache.org/schema/core" useJmx="false">
>
>     <persistenceFactory>
>       <journalPersistenceAdapterFactory journalLogFiles="5"
> dataDirectory="/home/ross/cmsqueue"/>
>     </persistenceFactory>
>     <transportConnectors>
>       <transportConnector uri="vm://localhost"/>
>     </transportConnectors>
>
>   </broker>
>   <import resource="camel.xml"/>
> </beans>
>
> I have the following route in camel.xml.
>
> <beans
>    xmlns="http://www.springframework.org/schema/beans"
>    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>    xsi:schemaLocation="
>      http://camel.apache.org/schema/spring
> http://camel.apache.org/schema/spring/camel-spring.xsd
>      http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans.xsd">
>
>     <bean id="queue" class="com.tybera.caseinfo.workflow.QueueManager" />
>     <bean id="activemq"
> class="org.apache.activemq.camel.component.ActiveMQComponent" >
>         <property name="connectionFactory">
>           <bean class="org.apache.activemq.ActiveMQConnectionFactory">
>             <property name="brokerURL" value="vm://localhost?create=false"/>
>           </bean>
>         </property>
>     </bean>
>      <camelContext id="camel" trace="true"
> xmlns="http://camel.apache.org/schema/spring">
>                 <jmxAgent id="agent" createConnector="true"/>
>         <route>
>             <description>Example Camel Route</description>
>             <from uri="activemq:eventQueue"/>
>             <to uri="file:/home/ross/temp/camel2/"/>
>         </route>
>     </camelContext>
> </beans>
>
> Then in Java code I have:
>         public static void putToEventQueue(String eventName, String eventStatus,
>                         String eventMessage) {
>                 init();
>                 CamelContext context = new DefaultCamelContext();
>                 ProducerTemplate template = context.createProducerTemplate();
>                 template.sendBody("vm://localhost:eventQueue", eventMessage(
>                                 eventName, eventStatus, eventMessage));
>                 try {
>                         Thread.sleep(10000);
>                 } catch (InterruptedException e) {
>                         // TODO Auto-generated catch block
>                         e.printStackTrace();
>                 }
>         }
>
> Here is the init method:
>         private static void init() {
>                 if (broker == null) {
>                         try {
>                                 broker = BrokerFactory.createBroker("xbean:activemq.xml", true);
>                                 broker.setDataDirectory("/home/ross/cmsqueue");
>                                 broker.start();
>                         } catch (Exception e) {
>                                 // TODO Auto-generated catch block
>                                 e.printStackTrace();
>                         }
>                 }
>         }
>
>
> The problem I am still struggling with is anything that I put into the queue
> via the ProducerTemplate in the method putToEventQueue() code is never
> visible by the route defined in the XML.  It is fundamentally the exact same
> problem my original post indicated.
>
> I want in my code to be able to put things into a queue via Java code.  I
> want my code to post events into the queue, and then I want to be able to
> deal with those events via the routes.
>
> If I use a ConsumerTemplate in Java code I can see and get the items in the
> queue.  But trying to get the queue items via a route, it cannot see any
> items in the queue.  It seems they are looking at different queues, but I
> can't see anything in my code that would cause this.  I don't know how to
> correct.  All examples of using the ProducerTemplate are very simple and the
> indication is that I can use it to put things into a queue, but where I am
> going wrong is what confuses me.  It seems like it should all be quite
> simple and straight forward, but I can't get it to work.
>
> I know I have things working in the sense that I can change the "from" in
> the route from the queue to a file directory and things work in moving a
> file from one directory to another.  Or, if in the java code I use both the
> ProducerTemplate and another method using the ConsumerTemplate then I put
> the message into the queue and get the message from the queue.  But if I use
> the ProducerTemplate to put to the queue and try to get it using the route,
> nothing happens.  It is as if they are each dealing with a different queue.
>
> Any help in understanding how I can use the ProducerTemplate to put into the
> queue and then let the route take care of dealing with the items put into
> the queue would be much appreciated.
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Having-problems-with-CamelContext-addRoutes-and-ActiveMQ-tp5768117p5769700.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2nd edition: http://www.manning.com/ibsen2

Re: Having problems with CamelContext.addRoutes() and ActiveMQ

Posted by rwoolf <ro...@rosswoolf.com>.
I have done as you said, and I use activemq.xml and import a camel.xml file
that defines the route.  But I still can't get things I put in the queue to
then be processed via the route.

activemq.xml
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  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-2.0.xsd
  http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd
  http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">

  
  <broker xmlns="http://activemq.apache.org/schema/core" useJmx="false">

    <persistenceFactory>
      <journalPersistenceAdapterFactory journalLogFiles="5"
dataDirectory="/home/ross/cmsqueue"/>
    </persistenceFactory>
    <transportConnectors>
      <transportConnector uri="vm://localhost"/>      
    </transportConnectors>
        
  </broker>
  <import resource="camel.xml"/>
</beans>

I have the following route in camel.xml.

<beans
   xmlns="http://www.springframework.org/schema/beans"  
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
     http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd
     http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
  
    <bean id="queue" class="com.tybera.caseinfo.workflow.QueueManager" />
    <bean id="activemq"
class="org.apache.activemq.camel.component.ActiveMQComponent" >
        <property name="connectionFactory">
          <bean class="org.apache.activemq.ActiveMQConnectionFactory">
            <property name="brokerURL" value="vm://localhost?create=false"/>
          </bean>
        </property>
    </bean>
     <camelContext id="camel" trace="true"
xmlns="http://camel.apache.org/schema/spring">
		<jmxAgent id="agent" createConnector="true"/>
        <route>
            <description>Example Camel Route</description>
            <from uri="activemq:eventQueue"/>
            <to uri="file:/home/ross/temp/camel2/"/>
        </route>
    </camelContext>
</beans>

Then in Java code I have:
	public static void putToEventQueue(String eventName, String eventStatus,
			String eventMessage) {
		init(); 
		CamelContext context = new DefaultCamelContext();
		ProducerTemplate template = context.createProducerTemplate();
		template.sendBody("vm://localhost:eventQueue", eventMessage(
				eventName, eventStatus, eventMessage));
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Here is the init method:
	private static void init() {
		if (broker == null) {
			try {
				broker = BrokerFactory.createBroker("xbean:activemq.xml", true);
				broker.setDataDirectory("/home/ross/cmsqueue");
				broker.start();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


The problem I am still struggling with is anything that I put into the queue
via the ProducerTemplate in the method putToEventQueue() code is never
visible by the route defined in the XML.  It is fundamentally the exact same
problem my original post indicated.

I want in my code to be able to put things into a queue via Java code.  I
want my code to post events into the queue, and then I want to be able to
deal with those events via the routes.  

If I use a ConsumerTemplate in Java code I can see and get the items in the
queue.  But trying to get the queue items via a route, it cannot see any
items in the queue.  It seems they are looking at different queues, but I
can't see anything in my code that would cause this.  I don't know how to
correct.  All examples of using the ProducerTemplate are very simple and the
indication is that I can use it to put things into a queue, but where I am
going wrong is what confuses me.  It seems like it should all be quite
simple and straight forward, but I can't get it to work.

I know I have things working in the sense that I can change the "from" in
the route from the queue to a file directory and things work in moving a
file from one directory to another.  Or, if in the java code I use both the
ProducerTemplate and another method using the ConsumerTemplate then I put
the message into the queue and get the message from the queue.  But if I use
the ProducerTemplate to put to the queue and try to get it using the route,
nothing happens.  It is as if they are each dealing with a different queue.  

Any help in understanding how I can use the ProducerTemplate to put into the
queue and then let the route take care of dealing with the items put into
the queue would be much appreciated.



--
View this message in context: http://camel.465427.n5.nabble.com/Having-problems-with-CamelContext-addRoutes-and-ActiveMQ-tp5768117p5769700.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Having problems with CamelContext.addRoutes() and ActiveMQ

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

You can define routes in xml files too. And ActiveMQ comes with a
camel.xml sample out of the box. You can configure ActiveMQ to import
and use that xml file, or copy the xml to the main activemq
configuration file.

Having the <camelContext> in the xml file is a good idea as it allows
AMQ to control the lifecycle and start / stop camel correctly.

You can still use java code for routes, see how to do that at
http://camel.apache.org/spring.html

On Fri, Jun 12, 2015 at 1:50 AM, Ross Woolf <ro...@rosswoolf.com> wrote:
> I am having problems getting queue data from CamelContext.  I am a
> newbie to Camel and so I just don't see what I am doing wrong.
>
> I am using:
> Camel 2.15.2
> ActiveMQ 5.11.1
>
> I am using the following code to send data to the queue and it works
> (all of this is just test code to learn Camel).
>
> public static void putToEventQueue(String eventName, String
> eventStatus, String eventMessage) {
>    init(); //Starts up ActiveMQ if it is not already started.
>    CamelContext context = new DefaultCamelContext();
>    ProducerTemplate template = context.createProducerTemplate();
>    template.sendBody("vm://localhost:eventQueue", new
> EventMessage(eventName, eventStatus, eventMessage));
> }
>
> If I use the following code to get data from ActiveMQ then it works
> (How I know that putToEventQueue is working).
>
> public void getFromQueue() {
>    CamelContext context = new DefaultCamelContext();
>    ConsumerTemplate ct = context.createConsumerTemplate();
>    Object test = ct.receiveBody("vm://localhost:eventQueue");
>    while (test != null) {
>       System.out.println("consumer Name:"+ ((EventMessage)
> test).getEventName());
>       System.out.println("consumer Message:"+ ((EventMessage)
> test).getEventMessage());
>       test = ct.receiveBody("vm://localhost:eventQueue");
>    }
> }
>
> But if I use the following CamelContext.addRoutes() code to get at the
> data it never retrieves any of the data from the queue.
>
> public void getFromQueue() {
>    CamelContext context = new DefaultCamelContext();
>    context.addComponent("activemq",
> activeMQComponent("vm://localhost?broker.persistent=false"));
>    try {
>       context.addRoutes(new RouteBuilder() {
>          public void configure() throws Exception {
>             from("activemq:queue:eventQueue").to("browse:eventReceived");
>          }
>       });
>       context.start();
>       Thread.sleep(10000);
>       BrowsableEndpoint browse =
> context.getEndpoint("browse:eventReceived", BrowsableEndpoint.class);
>        List<Exchange> exchanges = browse.getExchanges();
>        for (Exchange exchange : exchanges) { //If I set a break point
> here I never see data in exchanges.
>           Object test = exchange.getIn().getBody();
>           System.out.println("consumer Name:"+ ((EventMessage)
> test).getEventName());
>           System.out.println("consumer Message:"+ ((EventMessage)
> test).getEventMessage());
>        }
>       context.stop();
>    } catch (Exception e) {
>       e.printStackTrace();
>    }
> }
>
> I have tried to look closely at the examples of using CamelContext to
> create a route from ActiveMQ but I am missing something in the mix
> because nothing ever makes it into the BrowsableEndpoint or exchanges
> from the ActiveMQ queue.
>
> Any suggestions in helping me to see what I am doing wrong is greatly
> appreciated.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/