JBoss Fuse - AMQ With Camel


Advertisements

In this chapter, we will learn the basics of how ActiveMQ works with Camel.

Configuring to ActiveMQ Component

Before we can use ActiveMQ queue or topic in our code we have to configure ActiveMQComponent. Minimal configuration of ActiveMQComponent can be done as shown in the following program −

<bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
   <property name = "brokerURL" value = "tcp://localhost:61616"/>
   <property name = "userName" value = "admin"/>
   <property name = "password" value = "admin"/>
</bean>
  • brokerURL − Specifies host and port for AMQ Broker.

  • username − Specifies username to use for connecting to AMQ Broker.

  • password − specifies password for connecting to AMQ Broker.

Connecting to Queue

Now that we have configured ActiveMQComponent, we can use it in our CamelContext as endpoint.

We will use AMQ endpoint in the following format −

Activemq:[queue|topic]:[queueName|topicName]

Writing Messages to AMQ

<?xml version = "1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
   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.xsd
   http://camel.apache.org/schema/spring
   http://camel.apache.org/schema/spring/camel-spring.xsd">

After deploying this bundle in Fuse container, you should be able to see messages posted to AMQ which were placed as files in D:/src/data.

Input

D:/src/data/input.txt

Test me

Output

Writing Messages to AMQ

Reading from AMQ

<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->

<beans xmlns = "http://www.springframework.org/schema/beans"
   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.xsd
   http://camel.apache.org/schema/spring
   http://camel.apache.org/schema/spring/camel-spring.xsd">
	
   <camelContext xmlns = "http://camel.apache.org/schema/spring">
      <!-- here is a sample which processes the input files
         (leaving them in place - see the 'noop' flag)
          then performs content based routing on the message using XPath -->
			 
      <route>
         <from uri = "activemq:queue:TestQ"/>
         <to uri = "file:///d:/src"/>
      </route>
   </camelContext>
	
   <bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
      <property name = "brokerURL" value = "tcp://localhost:61616"/>
      <property name = "userName" value = "admin"/>
      <property name = "password" value = "admin"/>
   </bean>
	
</beans>

Input

After deploying this bundle, you should see a file being generated in D:/src and messages are consumed. Also Consumer should be shown for that Queue.

Reading from AMQ

Output

D:/src

Test me

Writing to Topic

<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
   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.xsd
   http://camel.apache.org/schema/spring 
   http://camel.apache.org/schema/spring/camel-spring.xsd">
	
   <camelContext xmlns = "http://camel.apache.org/schema/spring">
      <!-- here is a sample which processes the input files
         (leaving them in place - see the 'noop' flag)
          then performs content based routing on the message using XPath -->
			 
      <route>
         <from uri = "file:///d:/src"/>
         <to uri = "activemq:topic:TestTopic” />
      </route>
   </camelContext>
	
   <bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
      <property name = "brokerURL" value = "tcp://localhost:61616"/>
      <property name = "userName" value = "admin"/>
      <property name = "password" value = "admin"/>
   </bean>
	
</beans>

Reading from Topic

<?xml version = "1.0" encoding = "UTF-8"?>
<!-- Configures the Camel Context-->
<beans xmlns = "http://www.springframework.org/schema/beans"
   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.xsd
   http://camel.apache.org/schema/spring 
   http://camel.apache.org/schema/spring/camel-spring.xsd">
	
   <camelContext xmlns = "http://camel.apache.org/schema/spring">
      <!-- here is a sample which processes the input files
         (leaving them in place - see the 'noop' flag)
         then performs content based routing on the message using XPath -->
			
      <route>
         <from uri = "activemq:topic:TestTopic"/>
         <to uri = "file:///d:/src2"/>
      </route>
   </camelContext>
	
   <bean id = "activemq" class = "org.apache.activemq.camel.component.ActiveMQComponent">
      <property name = "brokerURL" value="tcp://localhost:61616"/>
      <property name = "userName" value = "admin"/>
      <property name = "password" value = "admin"/>
   </bean>
	
</beans>

Input

D:/src/file1.xml

<order>
   <data>
      <value>value1</value>
   </data>
</order>

<order>
   <data>
      <value>value2</value>
   </data>
</order>

<order>
   <data>
      <value>value3</value>
   </data>
</order>

Output

D:/src/

<order>
   <data>
      <value>value1</value>
   </data>
</order>

<order>
   <data>
      <value>value2</value>
   </data>
</order>

<order>
   <data>
      <value>value3</value>
   </data>
</order>
Advertisements