Home > My Work > How to send a jms message to an activemq queue with camel and spring

How to send a jms message to an activemq queue with camel and spring

these are the necessary dependencies for using camel to send a message to an activemq queue…

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jaxb</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-camel</artifactId>
            <version>${activemq.version}</version>
        </dependency>       

In this spring context i’m defining a camel context with the endpoint pointing to the queue…

<?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:context="http://www.springframework.org/schema/context"
     xmlns:camel="http://camel.apache.org/schema/spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	 http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
 

<!-- my activemq component -->
    <bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="brokerURL" value="tcp://myserver:61616"/>
        <property name="transacted" value="true"/>        
        <property name="usePooledConnection" value="true"/>
    </bean>

<!-- this is the camel context -->
    <camel:camelContext id="myCamelContext" trace="true">
        <camel:endpoint id="myQueue" uri="activemq:queue:myqueue/myService?jmsMessageType=Text"/>
        <camel:dataFormats>
            <camel:jaxb id="JaxbContext" contextPath="it.germanogiudici.types.mymessagetype.v1_0" prettyPrint="true"/>
        </camel:dataFormats>
    </camel:camelContext>
</beans>

Finally here I’m using the ProducerTemplate to send the message…

//here I'm using an helper class to retrieve the camel context bean from spring context
                CamelContext camelContext = (CamelContext) SpringApplicationContextHelper.getInstance().getBean("myCamelContext");
                Endpoint myQueue = (Endpoint) SpringApplicationContextHelper.getInstance().getBean("myQueue");
                logProcess.debug("endpoint a cui spedire l'evento: " + myQueue.getEndpointUri());
                ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
                producerTemplate.sendBody(myQueue, depositEvent);
About these ads
Categories: My Work Tags: , , ,
  1. syam
    April 25, 2012 at 5:37 am | #1

    Can you post SpringApplicationContextHelper class.Please help me

    • germanogiudici
      April 27, 2012 at 9:08 am | #2

      Hy siam, SpringApplicationContextHelper is a singleton class for accessing the spring context. This code is really old, actually is possible to use more elegant approaches. This is the class code:

      public class SpringApplicationContextHelper {
      	
      	private static SpringApplicationContextHelper springApplicationContextHelper;
      	private ApplicationContext appContext;
      	private static boolean contextLoaded;
      
      	private SpringApplicationContextHelper() {
      		this.appContext = new ClassPathXmlApplicationContext("classpath:springApplicationContext.xml");
      	}
      
      	public static synchronized SpringApplicationContextHelper getInstance() {
      		if (springApplicationContextHelper == null) {
      			try {
      				springApplicationContextHelper = new SpringApplicationContextHelper();
      				contextLoaded = true;
      			} catch (Throwable e) {
      				contextLoaded = false;
      				springApplicationContextHelper = null;
      			}
      		}
      		return springApplicationContextHelper;
      	}
      
      	public ApplicationContext getContext() {
      		return appContext;
      	}
      
      	public Object getBean(String beanName) {
      		return appContext.getBean(beanName);
      	}
      
      	public static boolean isContextLoaded() {
      		return contextLoaded;
      	}
      	
      }
      
  2. Eva
    February 20, 2013 at 4:49 am | #3

    I intend to book mark this specific article,
    “How to send a jms message to an activemq queue with camel and spring
    My notes” on my very own blog. Do you care in case I personallydo it?
    Regards ,Jake

    • germanogiudici
      February 20, 2013 at 9:54 am | #4

      No problem to bookmark my articles!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: