Archive

Archive for April, 2010

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

April 6, 2010 4 comments

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);
Advertisements
Categories: Uncategorized Tags: , , ,