Camel route to IBM WebSphere MQ with OSGI blueprint and Karaf
With IBM MQ widely used in the enterprise a common integration requirement is to send and receive message to/from an MQ queue. In this post I’m looking at the minimum configuration required to define an Apache Camel route that runs in Karaf and delivers messages from a folder into a queue.
Karaf is a container for many artifact types (e.g. web applications, messaging and database systems) including Camel routes. It caters for multiple deployment options, OSGI blueprints among them. A blueprint allows logic to be written in XML as configuration wiring beans together, similar to a Spring application context, and thus enable Java code and compilation free applications.
Next, I’ll go over the steps to create a file to MQ data flow. If you want a running example you’ll need an WebSphere MQ server installation to host the destination queue. In addition, you’ll need to install Karaf but is fairly trivial to do.
First, we’ll deploy the IBM and Apache Camel ingredients that make our solution. From IBM web site download 8.0.0.15-WS-MQ-Install-Java-All.jar. Unzip it and you’ll get com.ibm.mq.osgi.allclientprereqs_8.0.0.15.jar and com.ibm.mq.osgi.allclient_8.0.0.15.jar. Each must be deployed in Karaf using the following command:
karaf@root()>bundle:install -s wrap:file:///<dir-path-on-your-pc>/com.ibm.mq.osgi.<rest-of-above-file-name>.jar
Second, to bring in Karaf the features required by the Camel route, run the following commands:
karaf@root()>repo-add camel 3.4.0
karaf@root()>feature:install camel-blueprint camel-jms
Up to this point no bespoke integration has been deployed. Our actual route is defined using Spring DSL in the following OSGI blueprint XML file:
<?xml version=”1.0" encoding=”UTF-8"?>
<blueprint xmlns=”http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm=”http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
xsi:schemaLocation=”
http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd">
<bean id=”ibmmq” class=”org.apache.camel.component.jms.JmsComponent”>
<property name=”connectionFactory”>
<bean class=”com.ibm.mq.jms.MQQueueConnectionFactory”>
<property name=”transportType” value=”1" />
<property name=”channel” value=”<your-mq-channel>” />
<property name=”hostName” value=”<your-mq-server>” />
<property name=”port” value=”<port-channel-listens-on>" />
<property name=”queueManager” value=”<queue-manager-holding-the-queue>" />
</bean>
</property>
</bean><camelContext id=”mq-example-dataflow” xmlns=”http://camel.apache.org/schema/blueprint">
<route id=”file2mq”>
<from uri=”file:c:/lab/camel/in?noop=true”/>
<to uri=”ibmmq:<destination-queue-name>”/>
<log message=”File moved to queue.”/>
</route>
</camelContext></blueprint>
Last, assuming you saved the content above in mq-route.xml, you can deploy it into Karaf using the following command:
karaf@root()>install -s blueprint:file:///<dir-path-on-your-pc>/mq-route.xml
If things go well, you should see the Camel route deployed by running:
karaf@root()> camel:route-list
Context Route Status Total # Failed # Inflight # Uptime
— — — — — — — — — — — — — — — — — — — — — — — — — —
mq-example-dataflow file2mq Started 3 0 0 8 minutes
To test the route drop a file in c:/lab/camel/in folder. Provided the account you’re using to run Karaf has write access onto the destination queue, you should see the route execution in Karaf log, as follows:
karaf@root()> log:display
18:31:20.911 INFO [pipe-install -s blueprint:file:///<dir-path-on-your-pc>/mq-route.xml] Blueprint bundle mq-route.xml/0.0.0 has been started
18:31:20.912 INFO [Blueprint Event Dispatcher: 1] Attempting to start CamelContext: mq-example-dataflow
18:31:20.916 INFO [Blueprint Event Dispatcher: 1] JMX is enabled
18:31:20.925 INFO [Blueprint Event Dispatcher: 1] Apache Camel 3.4.0 (mq-example-dataflow) is starting
18:31:20.955 INFO [Blueprint Event Dispatcher: 1] Endpoint is configured with noop=true so forcing endpoint to be idempotent as well
18:31:20.955 INFO [Blueprint Event Dispatcher: 1] Using default memory based idempotent repository with cache max size: 1000
18:31:20.967 INFO [Blueprint Event Dispatcher: 1] Route: file2mq started and consuming from: file://c:/lab/camel/in
18:31:20.977 INFO [Blueprint Event Dispatcher: 1] Total 1 routes, of which 1 are started
18:31:20.978 INFO [Blueprint Event Dispatcher: 1] Apache Camel 3.4.0 (mq-example-dataflow) started in 0.051 seconds
18:31:22.040 INFO [Camel (mq-example-dataflow) thread #5 — file://c:/lab/camel/in] File moved to queue.
You can also check the MQ destination queue which now should have a copy of the test file.