org.apigw.monitoring.config.JmsConfig.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.monitoring.config.JmsConfig.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */

package org.apigw.monitoring.config;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageListener;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.apigw.monitoring.jms.messagelistener.MonitoringMessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.util.StringUtils;

@Configuration
public class JmsConfig {

    private static final Logger log = LoggerFactory.getLogger(JmsConfig.class);

    @Value("${monitoring.activemq.brokerUrl}")
    private String brokerUrl;

    @Value("${monitoring.monitoringQueue}")
    private String monitoringDestination;

    @Value("${monitoring.redelivery.backOffMultiplier:}")
    private String backOffMultiplier;

    @Value("${monitoring.redelivery.collisionAvoidancePercent:}")
    private String collisionAvoidancePercent;

    @Value("${monitoring.redelivery.initialRedeliveryDelay:}")
    private String initialRedeliveryDelay;

    @Value("${monitoring.redelivery.maximumRedeliveries:}")
    private String maximumRedeliveries;

    @Value("${monitoring.redelivery.maximumRedeliveryDelay:}")
    private String maximumRedeliveryDelay;

    @Value("${monitoring.redelivery.redeliveryDelay:}")
    private String redeliveryDelay;

    @Value("${monitoring.redelivery.useCollisionAvoidance:}")
    private String useCollisionAvoidance;

    @Value("${monitoring.redelivery.useExponentialBackOff:}")
    private String useExponentialBackOff;

    @Bean
    public ConnectionFactory connectionFactory() {
        final ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        factory.setBrokerURL(brokerUrl);

        /*
         * when true, submit individual transacted acks immediately rather than
         * with transaction completion. This allows the acks to represent
         * delivery status which can be persisted on rollback Used in
         * conjunction with org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter#setRewriteOnRedelivery(boolean) true
         */
        factory.setTransactedIndividualAck(true);

        return new PooledConnectionFactory(factory);
    }

    @Bean
    MonitoringMessageListener monitoringMessageListener() {
        return new MonitoringMessageListener();
    }

    @Bean
    DefaultMessageListenerContainer container(MessageListener messageListener,
            ConnectionFactory connectionFactory) {
        log.debug(String.format("Setting up DefaultMessageListenerContainer for destination: %s",
                monitoringDestination));
        DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
        container.setMessageListener(messageListener);
        container.setConnectionFactory(connectionFactory);
        container.setDestinationName(monitoringDestination);
        container.setSessionTransacted(true); //Setting this flag to "true" will use a short local JMS transaction when running outside of a managed transaction.
        return container;
    }

    @Bean
    JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(connectionFactory);
        return jmsTemplate;
    }

    @Bean
    public RedeliveryPolicy redeliveryPolicy() {
        RedeliveryPolicy policy = new RedeliveryPolicy();
        log.debug("************ Start of Redelivery policy configuration ************");

        if (!StringUtils.isEmpty(backOffMultiplier)) {
            log.debug(String.format("Overriding backOffMultiplier with value %s", backOffMultiplier));
            policy.setBackOffMultiplier(Long.valueOf(backOffMultiplier));
        } else {
            log.debug(String.format("backOffMultiplier is set to the default value %s",
                    policy.getBackOffMultiplier()));
        }

        if (!StringUtils.isEmpty(collisionAvoidancePercent)) {
            log.debug(
                    String.format("Overriding collisionAvoidancePercent with value %s", collisionAvoidancePercent));
            policy.setCollisionAvoidancePercent(Short.valueOf(collisionAvoidancePercent));
        } else {
            log.debug(String.format("collisionAvoidancePercent is set to the default value %s",
                    policy.getCollisionAvoidancePercent()));
        }

        if (!StringUtils.isEmpty(initialRedeliveryDelay)) {
            log.debug(String.format("Overriding initialRedeliveryDelay with value %s", initialRedeliveryDelay));
            policy.setInitialRedeliveryDelay(Long.valueOf(initialRedeliveryDelay));
        } else {
            log.debug(String.format("initialRedeliveryDelay is set to the default value %s",
                    policy.getInitialRedeliveryDelay()));
        }

        if (!StringUtils.isEmpty(maximumRedeliveries)) {
            log.debug(String.format("Overriding maximumRedeliveries with value %s", maximumRedeliveries));
            policy.setMaximumRedeliveries(Integer.valueOf(maximumRedeliveries));
        } else {
            log.debug(String.format("maximumRedeliveries is set to the default value %s",
                    policy.getMaximumRedeliveries()));
        }

        if (!StringUtils.isEmpty(maximumRedeliveryDelay)) {
            log.debug(String.format("Overriding maximumRedeliveryDelay with value %s", maximumRedeliveryDelay));
            policy.setMaximumRedeliveryDelay(Integer.valueOf(maximumRedeliveryDelay));
        } else {
            log.debug(String.format("maximumRedeliveryDelay is set to the default value %s",
                    policy.getMaximumRedeliveryDelay()));
        }

        if (!StringUtils.isEmpty(redeliveryDelay)) {
            log.debug(String.format("Overriding redeliveryDelay with value %s", redeliveryDelay));
            policy.setRedeliveryDelay(Integer.valueOf(redeliveryDelay));
        } else {
            log.debug(String.format("redeliveryDelay is set to the default value %s", policy.getRedeliveryDelay()));
        }

        if (!StringUtils.isEmpty(useCollisionAvoidance)) {
            log.debug(String.format("Overriding useCollisionAvoidance with value %s", useCollisionAvoidance));
            policy.setUseCollisionAvoidance(Boolean.valueOf(useCollisionAvoidance));
        } else {
            log.debug(String.format("useCollisionAvoidance is set to the default value %s",
                    policy.isUseCollisionAvoidance()));
        }

        if (!StringUtils.isEmpty(useExponentialBackOff)) {
            log.debug(String.format("Overriding useExponentialBackOff with value %s", useExponentialBackOff));
            policy.setUseExponentialBackOff(Boolean.valueOf(useExponentialBackOff));
        } else {
            log.debug(String.format("useExponentialBackOff is set to the default value %s",
                    policy.isUseExponentialBackOff()));
        }

        log.debug("************ End of Redelivery policy configuration ************");
        return policy;
    }
}