Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.

Embedded ActiveMQ Broker with Camel running in Apache Tomcat example

Available as of Camel 2.11

This example shows how you can embed Apache ActiveMQ Broker and Camel in a web application, which can run on Apache Tomcat or other web containers.

This example embeds ActiveMQ Broker and a Camel application which will continuously send a message per second to an inbox queue.
Then another Camel route will route messages from the inbox to the outbox queue.

Building and Running example

You will need to build this example first:

  mvn install

Which will create a .war file in the target directly. You can then deploy this .war file in any web container such as Apache Tomcat, by copying the .war file to its /webapp directory.

For example to start Apache Tomcat

bin/catalina.sh run

And then build the example and deploy to Apache Tomcat

mvn install
cp target/camel-example-activemq-tomcat.war /opt/apache-tomcat-7.0.26/webapps/

Source code

This example is a web application which mean we have a web.xml file in the src/main/webapp/WEB-INF directory. The code is as follows:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- this is a standard web.xml file, where we use Spring Web to boot our application -->
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>My Web Application</display-name>

	<!-- location of spring XML files -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:broker.xml,
			classpath:camel-config.xml
		</param-value>
	</context-param>

	<!-- the listener that kick-starts Spring, which loads the XML files and start our application -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

</web-app>

We have two Spring XML files that embed

  • broker.xml to embed Apache ActiveMQ broker
  • camel-config.xml to embed Apache Camel with the routes

The broker.xml file is located in the src/main/resources directory and contains:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- this is a spring XML file where we have ActiveMQ Broker embedded -->
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:broker="http://activemq.apache.org/schema/core"
	   xsi:schemaLocation="
	   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- create an ActiveMQ broker -->
	<!-- do not use the shutdown hook as it would cause the broker to shutdown when you press ctrl + c,
	     instead we will let Spring shutdown the broker -->
	<!-- notice this is a basic AMQ broker configuration, for production usage there is many more
	     options you may need to configure accordingly to your needs -->
	<broker id="broker" brokerName="myBroker" useShutdownHook="false" useJmx="true"
				   persistent="true" dataDirectory="activemq-data"
				   xmlns="http://activemq.apache.org/schema/core">

		<transportConnectors>
			<!-- vm transport for intra-jvm communication -->
			<transportConnector name="vm" uri="vm://myBroker"/>
			<!-- tcp for external communication -->
			<transportConnector name="tcp" uri="tcp://0.0.0.0:61616"/>
		</transportConnectors>

	</broker>

</beans>

The camel-config.xml file is located in the src/main/resources directory and contains:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- this is a spring XML file where we have Camel embedded -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       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">


	<!-- Here we define Camel, notice the namespace it uses -->
  <camelContext xmlns="http://camel.apache.org/schema/spring">
	  <!-- Camel route to feed the ActiveMQ inbox queue once per second -->
	  <route id="timerToInRoute">
		  <from uri="timer:foo?period=1s"/>
		  <transform>
			  <simple>Message at ${date:now:yyyy-MM-dd HH:mm:ss}</simple>
		  </transform>
		  <to uri="activemq:queue:inbox"/>
	  </route>

	  <!-- Camel route to move messages from the ActiveMQ inbox to its outbox queue -->
	  <route id="inToOutRoute">
		  <from uri="activemq:queue:inbox"/>
		  <log message="Routing message from inbox to outbox queue with data ${body}"/>
		  <to uri="activemq:queue:outbox"/>
	  </route>

  </camelContext>

	<!-- create a Camel ActiveMQ component to use, using the Spring bean style -->
	<!-- we use the vm protocol to communicate intra-jvm which is much faster than tcp -->
	<bean id="activemq" class="org.apache.camel.component.activemq.ActiveMQComponent">
		<!-- vm://myBroker is the vm protocol, and myBroker is the broker name -->
		<property name="brokerURL" value="vm://myBroker?create=false&amp;waitForStart=5000"/>
	</bean>

</beans>

JMX

You can use JConsole to get details about the running ActiveMQ and Camel. This is done by starting up jconsole, and then under local processes,
select the process which has catalina in the name (catalina is Apache Tomcat).

See Also

© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram