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…


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"
	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"/>

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

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

    Can you post SpringApplicationContextHelper class.Please help me

    • germanogiudici
      April 27, 2012 at 9:08 am

      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

    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

      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


Get every new post delivered to your Inbox.

%d bloggers like this: