Friday, 10 February 2017

Spring jms code with ibm websphere mq example

  Spring jms code with ibm websphere mq example


In this tutorial I am going to discuss How to send,read and listen Messages to IBM MQ using spring jmstemplate. I am going to cover how to configure the IBM MQ, connection factory, Message Queue,Listener container and JmsTemplate.


I used below technologies to execute below program

1.Maven 4.0.0
2.JDK 1.8
3.Spring (spring-core) 4.0.0.RELEASE
4.Spring (spring-jms) 4.0.0.RELEASE
5.Websphere MQ 7.5.0.1
6. springTestVersion 4.0.0.RELEASE
7. junitVersion 4.6

Prerequisite: System installed with IBM WebSphere MQ.

Click here to download full code of this example.

For example, created 3 files (App-context.xml,main.properties and spring.xml under \src\main\resources\)
created 2 files MessageService.java and RequestSendMessageCreator.java under \src\main\java\com\test\testspringjmsmq
created 1 file MessageServiceTest.java under \src\test\java\com\test\testspringjmsmq\
created 2 files DemoMain.java and TestMessageListener.java under \src\main\java\com\example\listener
1 pom.xml file which is automatic created by spring project.
Spring jms code with ibm websphere mq example

POM.xml: To start off we need to obtain the WebSphere MQ JARs. These JARs are proprietary - hence they will not resolve through a public Maven repository like Maven Central. These JARs need to be obtained from the WebSphere MQ installation directory and manually deployed to our local Maven repository. The config below defines the WebSphere MQ dependencies in our Maven POM file.
Spring jms code with ibm websphere mq example

Spring jms code with ibm websphere mq example

main.properties: This properties file contains information about QueueManager, Queue, host and port(listener port).

fromRtpQueue  is Queue name where listener will read messages.
RES.IDS.QUEUE is Queue name where JmsTemplate send messages.
queue_Idsmanager is Queue manager name.
queue_Idshostname is system ip address or localhost.
queue_Idsport is listener port of Queue manager.

Spring jms code with ibm websphere mq example

spring.xml: Spring's JMSTemplate class is the key to simplifying access to the conventional JMS API. It abstracts the common and repetitive boiler-plate code by handling the creation and closing of connections and sessions, sending and receiving of messages and handling of exceptions. JMSTemplate and a few other beans need to be defined in our Spring config.

Spring jms code with ibm websphere mq example

Spring jms code with ibm websphere mq example

Spring jms code with ibm websphere mq example

App-context.xml: This file import spring.xml and create bean for MessageService class.

Spring jms code with ibm websphere mq example


DemoMain.java: This class have main method and method to load application context file.

Spring jms code with ibm websphere mq example

TestMessageListener.java: This class is used for listen text messages from IBM MQ. Whenever any messages will come on configured Queue(I will tell how to configure queue for listener later on this post), this class will listen messages from that queue and as per requirement process.

Spring jms code with ibm websphere mq example

MessageService.java: This class is used for send messages to IBM MQ. For sending message, I used send(destination,messageCreator) of JmsTemplate where messageCreator is object of RequestSendMessageCreator. In RequestSendMessageCreator class you can create message as per your requirement(will discuss on this later on post) and destination is default destination where we need to send message. We can set destination in JmsTemplate or using annotate in MessageService class.
Spring jms code with ibm websphere mq example

RequestSendMessageCreator.java: This class is used for creating message.

Spring jms code with ibm websphere mq example

MessageServiceTest.java: This class will use for Junit test.

Spring jms code with ibm websphere mq example

How to run :  You can run this example by using DemoMain class or Junit test through MessageServiceTest.


Related Topic

IBM MQ reason code 2087 (AMQ4048)



18 comments:

  1. Hi Nitesh,
    Is there any other way that i can create mq message without using websphere?
    if yes so how?

    ReplyDelete
    Replies
    1. Some of messaging queues apart of websphere mq. I have not tried with others, I will do poc on activeMQ and will post.

      Sparrow

      written by Alex MacCaw
      Sparrow is a lightweight queue written in Ruby that “speaks memcache”

      Starling

      written by Blaine Cook at Twitter
      Starling is a Message Queue Server based on MemCached
      written in Ruby
      stores jobs in memory (message queue)
      documentation: some good tutorials, for example the railscast about starling and workling or this blog post about starling

      Kestrel

      written by Robey Pointer
      Starling clone written in Scala (a port of Starling from Ruby to Scala)
      Queues are stored in memory, but logged on disk

      RabbitMQ

      RabbitMQ is a Message Queue Server in Erlang
      stores jobs in memory (message queue)

      Apache ActiveMQ

      ActiveMQ is an open source message broker in Java

      Beanstalkd

      written by Philotic, Inc. to improve the response time of a Facebook application
      in-memory workqueue service mostly written in C
      Docu: http://nubyonrails.com/articles/about-this-blog-beanstalk-messaging-queue

      Amazon SQS

      Amazon Simple Queue Service
      Kafka

      Written at LinkedIn in Scala
      Used by LinkedIn to offload processing of all page and other views
      Defaults to using persistence, uses OS disk cache for hot data (has higher throughput then any of the above having persistence enabled)
      Supports both on-line as off-line processing

      ZMQ

      The socket library that acts as a concurrency framework
      Faster than TCP, for clustered products and supercomputing
      Carries messages across inproc, IPC, TCP, and multicast
      Connect N-to-N via fanout, pubsub, pipeline, request-reply
      Asynch I/O for scalable multicore message-passing apps

      EagleMQ

      EagleMQ is an open source, high-performance and lightweight queue manager.
      Written in C
      Stores all data in memory and support persistence.
      It has its own protocol. Supports work with queues, routes and channels.

      IronMQ

      IronMQ
      Written in Go
      Fully managed queue service
      Available both as cloud version and on-premise

      Delete
  2. Caused by: javax.jms.JMSException: MQJMS2005: failed to create MQQueueManager for "hostname:queuemanager. Reason code 2009 when I check in ibm site connection broker is there , please help on this exception

    ReplyDelete
    Replies
    1. Hi,
      You must have to use required mq jar from your local mq installation directory, otherwise you will get this error.

      Delete
  3. Hi Nitesh,

    How can we set multi site using MQ. Please guide

    Thanks

    ReplyDelete
  4. hi,i have downloaded the code and tried to run by giving my websphere mq details.but it is showing connection factor cant be established. Do I need to do anything more?

    ReplyDelete
    Replies
    1. Hi,

      You need to get MQ JARs from the WebSphere MQ installation directory and manually place to your local repository.Program will connect with MQ installed in your system so need to replace MQ Jar with yours.

      Delete
  5. Hi, i'm getting error while starting up my standalone listener, can you please guide me over - com.ibm.msg.client.jms.DetailedIllegalStateException: MQJCA1025: The message consumer must not have a message listener. An application attempted to set a message listener for a JMS message consumer. This exception occurs only if the application is running in a managed environment. Modify the application so that it does use a message listener.

    ReplyDelete
  6. very good and beneficial for any one fairly new. Thanks a ton.
    Do you have any idea how can I deploy this project on Websphere and run as a middleware application.

    ReplyDelete
  7. HI Team,I have data in xml format like XML tags and value. But when i tried to retrieve through jmstemplate.receive getting string values (xml tags are ignoring )..pls help to get the whole xml files

    ReplyDelete
  8. i have verified the MQ Queue values is like xml tags and values..but getting raw string values through jmstemplate.receive

    ReplyDelete