Book Image

Instant Apache ServiceMix How-to

By : Henryk Konsek
Book Image

Instant Apache ServiceMix How-to

By: Henryk Konsek

Overview of this book

<p>Creating reliable integration solution can be easy if you choose the right tools for the job. Apache Camel and ServiceMix are the leading integration technologies dedicated to dealing with the complexity of the messaging solutions. Learn how to efficiently integrate multiple systems with bleeding edge open source Apache software.</p> <p>"Instant Apache ServiceMix How-to" is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises that will help you take advantage of the real power that is behind the leading Apache integration stack.</p> <p>This book guides the reader in how to install ServiceMix and how to get it up and running. It will take you through a number of practical recipes – starting with the basic commands of Apache Karaf container and ending with the deployment of JMS and web service solutions.</p>
Table of Contents (7 chapters)

Routing to an external ActiveMQ broker (Should know)


In the previous recipe, you learned how to connect to the embedded Active MQ broker coming with ServiceMix out of the box. In real-world, however, you would probably need to connect to the standalone JMS broker.

Getting ready

In this recipe, we will assume that you have a standalone ActiveMQ broker installed, started, and is operational. To make the examples as simple as possible, we will also assume that the broker is exposed via the tcp://192.168.1.1:61616 TCP transport.

How to do it...

  1. Create a new Camel routing project using Maven (if you don't know how to do it, refer to the Creating and deploying a new Camel route (Must know) recipe).

  2. In the camel-context.xml file, add the new Camel routing rule that produces to or consumes from the Camel ActiveMQ component connected to the standalone router.

  3. Build and deploy the routing module to your ServiceMix instance (if you don't know how to do it, refer to the Creating and deploying a new Camel route (Must know) recipe).

How it works...

Connecting to the external ActiveMQ broker is very similar to working with the embedded ActiveMQ broker provided with ServiceMix. The only difference is that instead of using the default settings of the Camel ActiveMQ component, you need to configure your route to consume from (or produce to) the standalone ActiveMQ broker.

The Camel ActiveMQ component can be configured in many ways. One of the possible solutions is to register the component manually in the Spring application context. The following example demonstrates how to register and configure an ActiveMQ component in the Spring context:

<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
  <camel:route>
         <camel:from uri="timer:jmsMessageTrigger?period=5000"/>
         <camel:to uri="activemq:myQueue"/>
   </camel:route>
</camel:camelContext>
<bean id="activemq" 
    class="org.apache.activemq.camel.component.ActiveMQComponent">
      <property name="brokerURL" value="tcp://192.168.1.1:61616"/>
</bean>

There's more...

You can connect to many types of messaging brokers with ServiceMix. The Camel routing engine allows ServiceMix to integrate with any JMS-compatible messaging solutions. Camel also comes with support for some non-JMS messaging systems (such as XMPP, AMPQ, or Amazon SQS). The following is some additional information regarding the messaging support in ServiceMix.

Generic JMS connectivity

If you need to connect to a JMS broker other than ActiveMQ, use the Camel JMS component instead of the ActiveMQ component. The camel JMS component (http://camel.apache.org/jms.html) can be used to connect to any JMS-compatible messaging server (including ActiveMQ). Keep in mind, however, that if you connect to the ActiveMQ broker, it is better to stick to the dedicated ActiveMQ component, as the latter is optimized for the Apache message broker. As a result, you can expect easier configuration and slighter better performance when using a dedicated ActiveMQ component.

ActiveMQ connection pooling

A common mistake regarding the usage of JMS is to open a new client connection for each message sent to the broker. Creating a new connection to the broker is an expensive operation. The typical solution to optimize the JMS connection management is to reuse the already opened ones. This approach is called "Connection pooling", ActiveMQ comes with the connection factory that supports connection pooling; it is named org.apache.activemq.pool.PooledConnectionFactory. You should remember it whenever you configure your ActiveMQ connection in Camel.