{{announcement.body}}
{{announcement.title}}

Quick Integration With IBM MQ Using Apache Camel

DZone 's Guide to

Quick Integration With IBM MQ Using Apache Camel

In this article, look t an integration with IBM MQ using Apache Camel.

· Integration Zone ·
Free Resource

In 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.

Shell
 




xxxxxxxxxx
1


 
1
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.

XML
 




xxxxxxxxxx
1
21


 
1
<bean id="mqConnectionFactory"
2
        class="com.ibm.mq.jms.MQConnectionFactory">
3
        <property name="hostName" value="localhost" />
4
        <property name="port" value="1414" />
5
        <property name="queueManager" value="QM1" />
6
        <property name="channel" value="DEV.APP.SVRCONN" />
7
        <property name="transportType" value="1" />
8
        <property name="shareConvAllowed" value="0" />
9
    </bean>
10
    <bean id="mqcredential"
11
        class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
12
        <property name="targetConnectionFactory"
13
            ref="mqConnectionFactory" />
14
        <property name="username" value="app" />
15
        <property name="password" value="" />
16
    </bean>
17
    <bean id="mq" class="org.apache.camel.component.jms.JmsComponent">
18
        <property name="connectionFactory" ref="mqcredential" />
19
        <property name="maxConcurrentConsumers" value="1" />
20
        <property name="cacheLevelName" value="CACHE_CONSUMER" />
21
    </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.

Shell
 




xxxxxxxxxx
1


 
1
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.

Shell
 




xxxxxxxxxx
1
15


1
 
          
2
[chandrashekhar@localhost messaging]$ mvn spring-boot:run
3
---
4
---
5
2020-05-16 23:59:32.554  INFO 89538 --- [           main] o.a.c.impl.engine.AbstractCamelContext   : Total 2 routes, of which 2 are started
6
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
7
2020-05-16 23:59:32.575  INFO 89538 --- [           main] io.undertow                              : starting server: Undertow - 2.0.30.Final
8
2020-05-16 23:59:32.580  INFO 89538 --- [           main] org.xnio                                 : XNIO version 3.3.8.Final
9
2020-05-16 23:59:32.591  INFO 89538 --- [           main] org.xnio.nio                             : XNIO NIO Implementation Version 3.3.8.Final
10
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 ''
11
2020-05-16 23:59:32.659  INFO 89538 --- [           main] poc.integration.MySpringBootApplication  : Started MySpringBootApplication in 3.516 seconds (JVM running for 3.913)
12
2020-05-16 23:59:33.562  INFO 89538 --- [2 - timer://foo] timer                                    : SENDING MESSAGE
13
2020-05-16 23:59:33.706  INFO 89538 --- [2 - timer://foo] timer                                    : SENT MESSAGE
14
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         }
15
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.

Shell
 




x
33


 
1
[chandrashekhar@localhost messaging]$ podman ps
2
CONTAINER ID  IMAGE                       COMMAND  CREATED      STATUS          PORTS                   NAMES
3
52f0f72d56ed  docker.io/ibmcom/mq:latest           5 hours ago  Up 5 hours ago  0.0.0.0:1414->1414/tcp  laughing_almeida
4
[chandrashekhar@localhost messaging]$ 
5
 
          
6
[chandrashekhar@localhost messaging]$  podman exec --tty --interactive 52f0f72d56ed  bash
7
 
          
8
bash-4.4$ /opt/mqm/samp/bin/amqsbcg DEV.QUEUE.1 QM1
9
 
          
10
AMQSBCG0 - starts here
11
**********************
12
 
13
 MQOPEN - 'DEV.QUEUE.1'
14
 
15
 
16
 MQGET of message number 1, CompCode:0 Reason:0
17
****Message descriptor****
18
 
          
19
  StrucId  : 'MD  '  Version : 2
20
  Report   : 0  MsgType : 8
21
  Expiry   : -1  Feedback : 0
22
  Encoding : 273  CodedCharSetId : 1208
23
  Format : 'MQHRF2  '
24
  Priority : 4  Persistence : 1
25
  MsgId : X'414D5120514D31202020202020202020E9EDBF5E02174521'
26
  CorrelId : X'000000000000000000000000000000000000000000000000'
27
  BackoutCount : 0
28
  ReplyToQ       : '                                                '
29
  ReplyToQMgr    : 'QM1                                             '
30
  ** Identity Context
31
  UserIdentifier : 'app         '
32
---
33
---


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. 

Topics:
apache camel, ibm mq, integration, podman, spring boot, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}