One of the fundamental integration technologies in the Java world is JMS (Java Message Servi ce). While ServiceMix can connect to any JMS-compliant messaging system, this recipe demonstrates how to connect to the Apache ActiveMQ message broker.
ServiceMix comes with an embedded ActiveMQ JMS broker that listens to the client connections on the port 61616
. In order to use the embedded broker, you don't have to perform any additional administrative activities. The embedded ActiveMQ broker distributed out of the box with ServiceMix is configured with default settings and is ready to be used.
Create a new Camel routing Maven project (if you don't know how to do it, refer to the Creating and deploying a new Camel route (Must know) recipe).
In the
camel-context.xml
file, add a new Camel routing rule that produces to or consumes from the Camel ActiveMQ component with the default configuration.Build and deploy the routing module to your ServiceMix instance (if you don't know how to do it, please refer to the Creating and deploying a new Camel route (Must know) recipe).
ServiceMix starts the embedded AcvtiveMQ broker on port 61616
. This is also the default port of the Camel ActiveMQ component. This basically means that when you deploy to your Camel route, similar to the one presented here, Camel will automatically connect to the embedded ActiveMQ broker instance:
<camel:route> <camel:from uri="timer://fireJmsMessage?period=5000"/> <camel:to uri="activemq:myQueue"/> </camel:route>
Since Camel ActiveMQ with the default configuration also listens on the same port (and on localhost loopback address), it will by default connect to the ActiveMQ instance embedded in ServiceMix.
In the preceding example, we created the Camel route that sends message to the embedded ActiveMQ broker's queue named myQueue
every five seconds.
ActiveMQ itself is a comprehensive and advanced messaging solution. It is possible to configure both your broker and client component (that is, the Camel endpoint) with a variety of parameters and options. This section will describe some of them, but remember that there is much more available out there.
You may be wondering if it is necessary to create the myQueue
queue from the preceding example. Fortunately ActiveMQ will create the queue for us as soon as it will receive the first message addressed to the latter queue. We say that the queue is created on demand.
If, for some reason, you need to change the default port used by the embedded ActiveMQ broker (61616
), just change the value of the activemq.port
property in SERVICEMIX_HOME/etc/system.properties
. Remember that you also need to change the port in the component configuration of your Camel routes. Refer to the Camel ActiveMQ component documentation at http://camel.apache.org/activemq.html for more details.
If you want to learn more about ActiveMQ itself, we recommend the excellent book from Manning Publishing, ActiveMQ in Action (http://www.manning.com/snyder), written by the topic experts Bruce Snyder, Dejan Bosanac, and Rob Davies. You can also visit the project Apache site at http://activemq.apache.org, where you can find the latest news regarding ActiveMQ.