grails 3 with IBM MQ

82 views
Skip to first unread message

Harry

unread,
Feb 22, 2017, 11:30:03 PM2/22/17
to Grails Dev Discuss
Hi All,

Currently, i'm using JMS integraton with ActiveMQ , i want to switch to IBM MQ , what are the steps . 

Please let me now the how to integrate with grails 3+ 


thanks
Harry

Ronny Løvtangen

unread,
Feb 23, 2017, 12:58:04 AM2/23/17
to grails-de...@googlegroups.com
Are you using the JMS plugin?
You need to add the necessary dependencies and configure a different connectionFactory.

Here’s our resources.groovy from a project that is using both ActiveMQ and IBM MQ:


import com.ibm.mq.jms.JMSC
import com.ibm.mq.jms.MQQueueConnectionFactory
import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.activemq.RedeliveryPolicy
import org.springframework.jms.connection.SingleConnectionFactory
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter
import org.springframework.jms.core.JmsTemplate
import org.springframework.jms.listener.DefaultMessageListenerContainer

// Place your Spring DSL code here
beans = {
    // IBM MQ
    ibmMQJmsConnectionFactory(UserCredentialsConnectionFactoryAdapter) { bean ->
        bean.primary = true
        targetConnectionFactory = { MQQueueConnectionFactory cf ->
            hostName = grailsApplication.config.ibmMQ.hostName
            port = 1414
            channel = "SYSTEM.DEF.SVRCONN"
            transportType = JMSC.MQJMS_TP_CLIENT_MQ_TCPIP
            queueManager = grailsApplication.config.ibmMQ.queueManager
        }
        username = grailsApplication.config.ibmMQ.username
        password = grailsApplication.config.ibmMQ.password
        // Redelivery must be configured in IBM WebSphere MQ Explorer ("Backout requeue queue" and "Backout threshold")
    }

    ibmMQJmsTemplate(JmsTemplate) {
        connectionFactory = ref(ibmMQJmsConnectionFactory)
        receiveTimeout = 2000
        sessionTransacted = true
        sessionAcknowledgeMode = 0
    }

    ibmMQListenerContainer(DefaultMessageListenerContainer) {
        connectionFactory = ref(ibmMQJmsConnectionFactory)
        sessionTransacted = true
        destinationName = ""
    }

    // ActiveMQ
    activeMQJmsConnectionFactory(SingleConnectionFactory) {
        targetConnectionFactory = { ActiveMQConnectionFactory cf ->
            brokerURL = grailsApplication.config.activeMQ.brokerURL
            def policy = new RedeliveryPolicy()
            policy.initialRedeliveryDelay = 1000
            policy.maximumRedeliveries = 5
            redeliveryPolicy = policy
            //nonBlockingRedelivery = true
        }
    }

    activeMQJmsTemplate(JmsTemplate) {
        connectionFactory = ref(activeMQJmsConnectionFactory)
        receiveTimeout = 2000
        sessionTransacted = true
        sessionAcknowledgeMode = 0
    }

    activeMQListenerContainer(DefaultMessageListenerContainer) {
        connectionFactory = ref(activeMQJmsConnectionFactory)
        sessionTransacted = true
        destinationName = ""
    }
}


application.groovy:


jms {
    containers {
        ibmMQ {
            meta {
                parentBean = 'ibmMQListenerContainer'
            }
            connectionFactoryBean = "ibmMQJmsConnectionFactory"
        }
        activeMQ {
            meta {
                parentBean = 'activeMQListenerContainer'
            }
            connectionFactoryBean = "activeMQJmsConnectionFactory"
        }
    }
    adapters {
        ibmMQ {
            meta {
                parentBean = 'standardJmsListenerAdapter'
            }
        }
        activeMQ {
            meta {
                parentBean = 'standardJmsListenerAdapter'
            }
        }
    }
    templates {
        ibmMQ {
            meta {
                parentBean = "ibmMQJmsTemplate"
            }
        }
        activeMQ {
            meta {
                parentBean = "activeMQJmsTemplate"
            }
        }
    }
}




--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/2bce9eb8-94f8-4e0d-aa0f-4c0cc38ac073%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Harry

unread,
Feb 23, 2017, 11:23:29 AM2/23/17
to Grails Dev Discuss
Thanks a lot for the code snippet, really appreciate your response

I'm using jms plugin , this is my current setup with activemq, In my Service class service , i used jmsService to send and used standard jms template ( parentBean: standardJmsTemplate) . Can i use the same logic in my service class with ibmMQJmsTemplate , How did you leverage ibmMQJmsTemplate  to send messages ? 

build.gradle
==============

compile 'org.grails.plugins:jms:2.0.0.M1'
runtime 'org.apache.activemq:activemq-spring:5.11.1'
compile group: 'org.apache.activemq', name: 'activemq-kahadb-store', version: '5.11.1'


resources.groovy
=================

import org.apache.activemq.ActiveMQConnectionFactory
import org.springframework.jms.connection.SingleConnectionFactory

jmsConnectionFactory(SingleConnectionFactory) {
targetConnectionFactory = { ActiveMQConnectionFactory cf ->
            switch (Environment.current) {
                case Environment.DEVELOPMENT:        
       brokerURL = 'tcp://localhost:61616'
                    break
                default:
       brokerURL = 'tcp://localhost:61616'
                    break
            }
}
 }



application.yml
==============

jms:
    templates:
        other:
            meta:
                parentBean: standardJmsTemplate

spring:

    groovy:
        template:
            check-template-location: false

    activemq:
        password: *****
        user: *****
        in-memory: true        
        pooled: false


Service class
===========

class SubmitterService {

static exposes = ["jms"]
JmsService jmsService
@Queue
def send(msg) {
jmsService.send(queue: "Queue", XMl.toString(),"standard", null)
logger.info "Sent to queue.."

Ronny Løvtangen

unread,
Feb 23, 2017, 4:00:29 PM2/23/17
to grails-de...@googlegroups.com
We send only to ActiveMQ, which is done like this:

class ActiveMQService {

    static exposes = ["jms"]
    def jmsService

    void sendMessageToTopic(String topicName, String message) {
        jmsService.send(topic: topicName, message, "activeMQ")
    }
}

Switching “activeMQ” with “ibmMQ” in the params to send() would work, I guess.

If you don’t need to connect to both IBM MQ and ActiveMQ, you could configure IBM MQ as “standard". We consumed messages from IBM MQ and sent to ActiveMQ, so we needed to support both connections simultaneously, but only consuming messages from IBM MQ.

There’s been a few issues with the latest versions of JMS plugins in conjunction with recent Grails versions, which caused us some headache. E.g. https://github.com/gpc/jms/pull/28, which is fixed but unfortunately not released. I wouldn’t hold my breath, given the release history. In our newest applications we just configure Spring JMS ourselves instead of relying on the plugin.


I found some code in a different project that sends messages to IBM MQ using low level JMS API instead of Spring JMS, in case that’s interesting:


import com.ibm.mq.jms.JMSC
import com.ibm.mq.jms.MQQueueConnectionFactory
import groovy.util.logging.Slf4j
import org.joda.time.DateTime
import org.springframework.beans.factory.InitializingBean
import javax.jms.Connection
import javax.jms.MessageProducer
import javax.jms.Queue
import javax.jms.Session
import javax.jms.TextMessage

@Slf4j
class TiosMessageProducer implements InitializingBean {
    String ibmMqHostName
    Integer ibmMqPort
    String ibmMqChannel
    String ibmMqQueueManager
    String ibmMqUsername
    String ibmMqPassword
    String dropsMsgQueueName
    Boolean performDelivery
    Boolean testIbmMqConnectionOnStartup

    private MQQueueConnectionFactory ibmMqConnectionFactory

    @Override
    void afterPropertiesSet() throws Exception {
        ibmMqConnectionFactory = new MQQueueConnectionFactory()
        ibmMqConnectionFactory.setHostName(ibmMqHostName)
        ibmMqConnectionFactory.setPort(ibmMqPort)
        ibmMqConnectionFactory.setChannel(ibmMqChannel)
        ibmMqConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP)
        ibmMqConnectionFactory.setQueueManager(ibmMqQueueManager)

        if (testIbmMqConnectionOnStartup) {
            ibmMqConnectionFactory.createConnection(ibmMqUsername, ibmMqPassword)?.close()
        }
    }

    void deliverMessageToTios(String xml) {
       
        Connection ibmMqConnection = ibmMqConnectionFactory.createConnection(ibmMqUsername, ibmMqPassword)
        try {
            if (!performDelivery) {
                log.debug "Skipping actual delivery due to configuration property"
                return
            }

            Session session = ibmMqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)
            try {
                Queue destination = session.createQueue(dropsMsgQueueName)
                MessageProducer producer = session.createProducer(destination)

                try {
                    TextMessage msg = session.createTextMessage(xml)
                    producer.send(msg)
                    log.debug "Message delivered."
                } finally {
                    producer.close()
                }
            } finally {
                session.close()
            }
        } finally {
            ibmMqConnection.close()
            log.debug "Closed connection to IBM MQ"
        }
    }
}


Ronny

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages