io.curly.artifact.integration.config.AmqpConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.artifact.integration.config.AmqpConfiguration.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package io.curly.artifact.integration.config;

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.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * The bindings and Queue declaration must be on the two sides, no matter what library is used on top of the AMQP API
 * the clients must implements the declarations of Exchange, Queues and Bindings
 *
 * @author Joo Evangelista
 */
@Configuration
public class AmqpConfiguration {

    @Bean
    Queue tagQueue() {
        return new Queue("tag.queue", true);
    }

    @Bean
    Queue notifierQueue() {
        return new Queue("artifactory.notification.queue", true);
    }

    @Bean
    Queue categoryQueue() {
        return new Queue("category.queue", true);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("artifactory-exchange");
    }

    @Bean
    TopicExchange notificationExchange() {
        return new TopicExchange("notification-exchange");
    }

    @Bean
    Binding tagBinding(TopicExchange exchange, Queue tagQueue) {
        return BindingBuilder.bind(tagQueue).to(exchange).with(tagQueue.getName());
    }

    @Bean
    Binding notifierBinding(TopicExchange notificationExchange, Queue notifierQueue) {
        return BindingBuilder.bind(notifierQueue).to(notificationExchange).with(notifierQueue.getName());
    }

    @Bean
    Binding categoryBinding(TopicExchange exchange, Queue categoryQueue) {
        return BindingBuilder.bind(categoryQueue).to(exchange).with(categoryQueue.getName());
    }

    @Bean
    MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        return rabbitTemplate;
    }
}