Java, Spring, Spring Boot

How to setup IBM MQ and Active Mq Connection (Including Alias Queue) the simplest way, using Spring Boot ?

Here is a simple and short example on how you can setup the Connection Factory for different types of Broker. Also it contains the code logic if you need to use the ActiveMq Backoff Startegy.

// This simple connection factory will handle both the IBM MQ Queues as well as Active MQ queues along with Alias Queue.

class QueuesConnectionFactory {

    public QueuesConnectionFactory() {
    }

    public static ConnectionFactory createConnectionFactory(MqProperties yourConnectionProperties)
            throws JMSException {
        return createConnectionFactory(yourConnectionProperties, false);
    }

//MqProperties can be some Java class that is powered by yaml using Spring Boot.
    public static ConnectionFactory createConnectionFactory(MqProperties yourConnectionProperties,
                                                            boolean isAliasQueue)
            throws JMSException {

        ConnectionFactory connectionFactory;
        switch (yourConnectionProperties.getBrokerType()) {
            case IBM:
                MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
                mqQueueConnectionFactory.setHostName(yourConnectionProperties.getServerHost());
                mqQueueConnectionFactory.setTransportType(yourConnectionProperties.getTransportType());
                mqQueueConnectionFactory.setChannel("MY.SOME.CHANNEL");
                mqQueueConnectionFactory.setPort(yourConnectionProperties.getServerPort());
                mqQueueConnectionFactory
                        .setClientReconnectOptions(yourConnectionProperties.getClientReconnect().getValue());

                if (!isAliasQueue) {
                    mqQueueConnectionFactory.setQueueManager(yourConnectionProperties.getQueueManager());
                }
                connectionFactory = mqQueueConnectionFactory;
                break;
            case ACTIVE_MQ:
                if (yourConnectionProperties.isActiveMqFailOverEnabled()) {
                    connectionFactory = new ActiveMQConnectionFactory(
                            yourConnectionProperties.getActiveMqFailOverUri()
                                    + yourConnectionProperties.getServerHost()
                                    + ":"
                                    + yourConnectionProperties.getServerPort()
                                    + ")?"
                                    + "useExponentialBackOff="
                                    + yourConnectionProperties.getUseExponentialBackOff()
                                    + "&initialReconnectDelay="
                                    + yourConnectionProperties.getInitialReconnectDelay()
                                    + "&startupMaxReconnectAttempts="
                                    + yourConnectionProperties.getStartupMaxReconnectAttempts()
                                    + "&maxReconnectAttempts="
                                    + yourConnectionProperties.getMaxReconnectAttempts());
                } else {
                    connectionFactory = new ActiveMQConnectionFactory("tcp://"
                            + yourConnectionProperties.getServerHost()
                            + ":"
                            + yourConnectionProperties.getServerPort());
                }

                break;
            default:
                throw new IllegalArgumentException("Could not find the Broker Specified");
        }
        return connectionFactory;
    }
}

Leave a Reply