Buscar este blog

domingo, 24 de mayo de 2015

JBoss + Camel JMS + Spring Example

In this post I´ll show you how to configure Apache Camel JMS to read messages from a Queue in JBoss.

Versions:
  • Apache Camel 2.12.0
  • Spring 3.2.4.RELEASE
  • JBoss EAP 6.2
  • HornetQ
The server side and producer configuration can be found in helloworld-jms, in https://github.com/jboss-developer/jboss-eap-quickstarts/tree/6.2.x.
Basically, there is a queue named "testQueue" and a simple app that send messages to it. In helloworld example there is a producer and a consumer, but in this case the consumer can be omited because it will be a camel route.

Camel project config

Pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>es.cixtec.arq.pruebas</groupId>
  <artifactId>camel-dynamic-route</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    
 <properties>
  <camel.version>2.12.0</camel.version>
  <version.jboss.as> 7.3.0.Final-redhat-14</version.jboss.as>     
  <spring.version>3.2.4.RELEASE</spring.version>
  <slf4j.version>1.6.6</slf4j.version>  
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
 
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>


   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
   </plugin>


   <plugin>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-maven-plugin</artifactId>
    <version>${camel.version}</version>
   </plugin>  

  </plugins>
  
 </build>
 
 <dependencies>
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-core</artifactId>
   <version>${camel.version}</version>   
  </dependency>
  
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-jms</artifactId>
   <version>${camel.version}</version>   
  </dependency>
  
  <dependency>
   <groupId>org.jboss.as</groupId>
   <artifactId>jboss-as-jms-client-bom</artifactId>
   <version>${version.jboss.as}</version>
   <type>pom</type>
  </dependency>
        
  <dependency>
   <groupId>org.apache.camel</groupId>
   <artifactId>camel-spring</artifactId>
   <version>${camel.version}</version>   
  </dependency>
  
  
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
  </dependency>

  <dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>${slf4j.version}</version>
  </dependency>
  
 </dependencies>
 
</project>

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

 <!-- ********************************************* -->
 <!-- ********** JMS CONFIG *********************** -->
 <!-- ********************************************* -->
 <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
  <property name="environment">
   <props>
    <prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</prop>
    <prop key="java.naming.provider.url">remote://localhost:4447</prop>
    <prop key="java.naming.security.principal">quickstartUser</prop>
    <prop key="java.naming.security.credentials">quickstartPwd1!</prop>    
   </props>
  </property>
 </bean>
 
 <bean id="jmsQueueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiTemplate" ref="jndiTemplate"/>  
  <property name="jndiName" value="jms/RemoteConnectionFactory"/>  
 </bean>

 <bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
  <property name="connectionFactory" ref="jmsQueueConnectionFactory" />
 </bean>

 <!-- ********************************************* -->


 <!-- ****************************** -->
 <!-- ****** RUTA ****************** -->
 <!-- ****************************** -->
 <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
  <route id="mainGestionRoute">
   <from uri="jms:queue:testQueue?username=quickstartUser&amp;password=quickstartPwd1!" />

   <log message="JMSCorrelationID: ${header.JMSCorrelationID}"/>
   <log message="JMSDeliveryMode: ${header.JMSDeliveryMode}"/>
   <log message="JMSDestination: ${header.JMSDestination}"/>
   <log message="JMSExpiration: ${header.JMSExpiration}"/>
   <log message="JMSMessageID: ${header.JMSMessageID}"/>
   <log message="JMSPriority: ${header.JMSPriority}"/>
   <log message="JMSRedelivered: ${header.JMSRedelivered}"/>
   <log message="JMSReplyTo: ${header.JMSReplyTo}"/>
   <log message="JMSMessageID: ${header.JMSMessageID}"/>
   <log message="JMSTimestamp: ${header.JMSTimestamp}"/>
   <log message="JMSType: ${header.JMSType}"/>
   <log message="JMSXGroupID: ${header.JMSXGroupID}"/>
   
   <to uri="log:receivedMessage?level=INFO" />
  </route>
 </camelContext>

</beans>

This route just prints the message and JMS headers received.

No hay comentarios:

Publicar un comentario