content: package com.rabbitmq.test.config;
  
  import com.rabbitmq.test.com.rabbitmq.test.hello.Receiver;
  import lombok.Data;
  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.connection.SimpleRoutingConnectionFactory;
  import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
  import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  
  /**
   * Created by wuhuachuan on 16/3/28.
   */
  @Configuration
  @Data
  public class RabbitMQConfig {
  
      final static String queueName = "spring-boot";
  
      @Bean
      Queue queue() {
          return new Queue(queueName, false);
      }
  
      @Bean
      TopicExchange exchange() {
          return new TopicExchange("spring-boot-exchange");
      }
  
      @Bean
      Binding binding(Queue queue, TopicExchange exchange) {
          return BindingBuilder.bind(queue).to(exchange).with(queueName);
      }
  
      @Bean
      ConnectionFactory connectionFactory() {
          CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.0.108", 5672);
          connectionFactory.setUsername("guest");
          connectionFactory.setPassword("guest");
  //        connectionFactory.setVirtualHost(vhost);
          connectionFactory.setPublisherConfirms(true); // enable confirm mode
          return connectionFactory;
      }
  
      @Bean
      SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
  
          SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
          container.setConnectionFactory(connectionFactory);
          container.setQueueNames(queueName);
          container.setMessageListener(listenerAdapter);
          return container;
      }
  
      @Bean
      Receiver receiver() {
          return new Receiver();
      }
      @Bean
      MessageListenerAdapter listenerAdapter(Receiver receiver) {
          return new MessageListenerAdapter(receiver, "receiveMessage");
      }
  }
  