Quick Integration With IBM MQ Using Apache Camel
In this article, look t an integration with IBM MQ using Apache Camel.
Join the DZone community and get the full member experience.
Join For FreeIn this article we will find out how easy it is to integrate a messaging server with Apache Camel. Apache Camel is one of the best known Integration framework. Here we are integrating Apache Camel with IBM MQ. You will be amazed to see how quick integration is and how easy to test it with Camel Spring-Boot based template. Docker images of IBM MQ are available for quick development and testing. Also client artifacts are now available in maven repository. So it is quick and easy to setup a development or test environment, now one should only focus on actual development.
There is another very important use-case where there could be a requirement to create a bridge between two different messaging middleware. Like IBM MQ and AMQ7 or AMQ 6.x and IBM MQ. Bridge refers to communication channel. Apache Camel would serve as the bridge between two different messaging server.
So let us start now.
1. Download camel-archetype-spring-boot using following command. If will ask for group-id, artifact-id, version and package. You can set them as per your wish or coding guidelines.
xxxxxxxxxx
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-spring-boot -DarchetypeVersion=3.3.0
2. Modify this template as per code shared in my personal GitHub link. We will go through important sections of the shared code.
- IBM MQ client library. These are available in maven repository.
- Camel latest library is being used. At the time of writing this blog latest available version of Camel is 3.3.0.
- camel-context.xml is being loaded from class-path using spring ImportResource annotation.
- camel-jms component is being used which is set with IBM MQ connectionfactory.
xxxxxxxxxx
<bean id="mqConnectionFactory"
class="com.ibm.mq.jms.MQConnectionFactory">
<property name="hostName" value="localhost" />
<property name="port" value="1414" />
<property name="queueManager" value="QM1" />
<property name="channel" value="DEV.APP.SVRCONN" />
<property name="transportType" value="1" />
<property name="shareConvAllowed" value="0" />
</bean>
<bean id="mqcredential"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory"
ref="mqConnectionFactory" />
<property name="username" value="app" />
<property name="password" value="" />
</bean>
<bean id="mq" class="org.apache.camel.component.jms.JmsComponent">
<property name="connectionFactory" ref="mqcredential" />
<property name="maxConcurrentConsumers" value="1" />
<property name="cacheLevelName" value="CACHE_CONSUMER" />
</bean>
- Route-1 is producing or sending messages to IBM MQ instance. Route-2 is receiving or consuming messages from IBM MQ instance.
3. Now let us pull and start IBM MQ docker image. I have used Podman. Podman is an alternative to docker and command syntax remain same. Docker can also be used instead of podman.
xxxxxxxxxx
podman run --env LICENSE=accept --env MQ_QMGR_NAME=QM1 --publish 1414:1414 --publish 9443:9443 --detach ibmcom/mq
4. Run Camel spring-boot application. We will finally see logs as following which send and receive message from broker.
xxxxxxxxxx
[chandrashekhar@localhost messaging]$ mvn spring-boot:run
---
---
2020-05-16 23:59:32.554 INFO 89538 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Total 2 routes, of which 2 are started
2020-05-16 23:59:32.554 INFO 89538 --- [ main] o.a.c.impl.engine.AbstractCamelContext : Apache Camel 3.3.0 (CamelContext: MyCamel) started in 0.117 seconds
2020-05-16 23:59:32.575 INFO 89538 --- [ main] io.undertow : starting server: Undertow - 2.0.30.Final
2020-05-16 23:59:32.580 INFO 89538 --- [ main] org.xnio : XNIO version 3.3.8.Final
2020-05-16 23:59:32.591 INFO 89538 --- [ main] org.xnio.nio : XNIO NIO Implementation Version 3.3.8.Final
2020-05-16 23:59:32.656 INFO 89538 --- [ main] o.s.b.w.e.u.UndertowServletWebServer : Undertow started on port(s) 8080 (http) with context path ''
2020-05-16 23:59:32.659 INFO 89538 --- [ main] poc.integration.MySpringBootApplication : Started MySpringBootApplication in 3.516 seconds (JVM running for 3.913)
2020-05-16 23:59:33.562 INFO 89538 --- [2 - timer://foo] timer : SENDING MESSAGE
2020-05-16 23:59:33.706 INFO 89538 --- [2 - timer://foo] timer : SENT MESSAGE
2020-05-16 23:59:33.736 INFO 89538 --- [er[DEV.QUEUE.1]] get : GOT MESSAGE with headers: {firedTime=Sat May 16 23:59:33 IST 2020, JMS_IBM_Character_Set=UTF-8, JMS_IBM_Encoding=273, JMS_IBM_Format=MQSTR , JMS_IBM_MsgType=8, JMS_IBM_PutApplType=28, JMS_IBM_PutDate=20200516, JMS_IBM_PutTime=18293362, JMSCorrelationID=null, JMSCorrelationIDAsBytes=null, JMSDeliveryMode=2, JMSDestination=queue:///DEV.QUEUE.1, JMSExpiration=0, JMSMessageID=ID:414d5120514d31202020202020202020e9edbf5e02214521, JMSPriority=4, JMSRedelivered=false, JMSReplyTo=null, JMSTimestamp=1589653773618, JMSType=null, JMSXAppID=MySpringBootApplication , JMSXDeliveryCount=1, JMSXGroupID=null, JMSXUserID=app }
2020-05-16 23:59:33.737 INFO 89538 --- [er[DEV.QUEUE.1]] get : GOT MESSAGE with Body 2020-05-16T23:59:33
5. Browse Queue.
x
[chandrashekhar@localhost messaging]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
52f0f72d56ed docker.io/ibmcom/mq:latest 5 hours ago Up 5 hours ago 0.0.0.0:1414->1414/tcp laughing_almeida
[chandrashekhar@localhost messaging]$
[chandrashekhar@localhost messaging]$ podman exec --tty --interactive 52f0f72d56ed bash
bash-4.4$ /opt/mqm/samp/bin/amqsbcg DEV.QUEUE.1 QM1
AMQSBCG0 - starts here
**********************
MQOPEN - 'DEV.QUEUE.1'
MQGET of message number 1, CompCode:0 Reason:0
****Message descriptor****
StrucId : 'MD ' Version : 2
Report : 0 MsgType : 8
Expiry : -1 Feedback : 0
Encoding : 273 CodedCharSetId : 1208
Format : 'MQHRF2 '
Priority : 4 Persistence : 1
MsgId : X'414D5120514D31202020202020202020E9EDBF5E02174521'
CorrelId : X'000000000000000000000000000000000000000000000000'
BackoutCount : 0
ReplyToQ : ' '
ReplyToQMgr : 'QM1 '
** Identity Context
UserIdentifier : 'app '
---
---
6. It may happen that you don't see a message while browsing the message, you can comment out a second route in the example and then test. As the consumer route is not available, the queue will have messages available.
That's it. I hope you will find this article helpful and interesting.
Opinions expressed by DZone contributors are their own.
Comments