org.opentestsystem.delivery.logging.RabbitConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.logging.RabbitConfiguration.java

Source

/***************************************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2017 Regents of the University of California
 *
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 *
 * SmarterApp Open Source Assessment Software Project: http://smarterapp.org
 * Developed by Fairway Technologies, Inc. (http://fairwaytech.com)
 * for the Smarter Balanced Assessment Consortium (http://smarterbalanced.org)
 **************************************************************************************************/

package org.opentestsystem.delivery.logging;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Address;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AnonymousQueue;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;

@Configuration
public class RabbitConfiguration {

    @Value("${rabbitmq.addresses:}")
    private String addresses;

    @Value("${rabbitmq.host:localhost}")
    private String host;

    @Value("${rabbitmq.port:-1}")
    private int port;

    @Value("${rabbitmq.username:guest}")
    private String username;

    @Value("${rabbitmq.password:guest}")
    private String password;

    @Value("${rabbitmq.vhost:/}")
    private String vhost;

    @Bean
    public ConnectionFactory connectionFactory() {
        final CachingConnectionFactory factory = new CachingConnectionFactory(determineHost());
        if (isNotBlank(addresses)) {
            factory.setAddresses(addresses);
        }
        factory.setPort(port);
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost(vhost);
        return factory;
    }

    @Bean
    public AmqpAdmin amqpAdmin(final ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    public Queue springCloudBusQueue() {
        return new AnonymousQueue();
    }

    @Bean
    public TopicExchange springCloudBus() {
        return new TopicExchange("springCloudBus");
    }

    @Bean
    public Binding binding(final TopicExchange topicExchange, final Queue queue) {
        return BindingBuilder.bind(queue).to(topicExchange).with("#");
    }

    @Bean
    public ConfigRefreshListener configRefreshListener(final LoggingConfigRefresher loggingConfigRefresher,
            @Qualifier("integrationObjectMapper") final ObjectMapper objectMapper) {
        return new ConfigRefreshListener(loggingConfigRefresher, objectMapper);
    }

    @Bean
    public SimpleMessageListenerContainer listenerContainer(final ConnectionFactory connectionFactory,
            final Queue springCloudBus, final ConfigRefreshListener configRefreshListener) {
        final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setQueues(springCloudBus);
        container.setMessageListener(configRefreshListener);
        return container;
    }

    private String determineHost() {
        if (isBlank(addresses)) {
            return host;
        }
        final Address[] addressArray = Address.parseAddresses(addresses);
        return addressArray[0].getHost();
    }

}