SpringBoot简单集成rabbitMQ

概述:

首先引入依赖包。

 

        
        
            org.springframework.boot
            spring-boot-starter-amqp
        


Springboot集成了rabbitMQ,不需要设置exchange,queue等,方便了很多,当然也可以设置队列,交换器,路由等高级信息。

以前需要设置很多,送上以前的例子,关于exchange、channels  queue等参考 http://blog.csdn.net/whycold/article/details/41119807

 

 

//以前配置rabbitMQ的代码
@Configuration
public class RabbitConfig {

	public static final String DEMO_QUEUE = "demo.queue";
	public static final String DEMO_EXANGE = "demo.exchange";
	public static final String DEMO_ROUTINGKEY = "demo.routingkey";

	@Bean
	@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 因为生产者设置回掉方法,所以template必须为prototype
	public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
		rabbitTemplate.setMessageConverter(messageConverter());
		return rabbitTemplate;
	}

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

	@Bean
	public Queue queue() {
		return new Queue(RabbitConfig.DEMO_QUEUE, true);
	}

	@Bean
	public DirectExchange exchange() {
		return new DirectExchange(RabbitConfig.DEMO_EXANGE);
	}

	@Bean
	public Binding binding() {
		return BindingBuilder.bind(queue()).to(exchange()).with(RabbitConfig.DEMO_ROUTINGKEY);
	}

}

 

 

 

1、rabbitMQ不同于activeMQ,队列不能自动创建

代码中创建队列的两种方式。

 

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;

/**
 * @Auther: 
 * @description:
 * @Date: 14:05 2017/10/9
 */
public class RabbitConfig {

    @Bean
    public Queue helloQueue() {
        return new Queue("hello", true);
    }
    
        /**
     *  自定义MQFactoty demo 配合下面的的reviceMessage使用
     * @param configurer
     * @param connectionFactory
     * @return
     */
//    @Bean
//    public SimpleRabbitListenerContainerFactory customContainerFactory(
//            SimpleRabbitListenerContainerFactoryConfigurer configurer,
//            ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setPrefetchCount(10);
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
//
//   @RabbitListener(queues = "#{queueName}", containerFactory = "customContainerFactory")
//    public void receiveMessage(String message) {
//        System.out.println(message);
//    }


//方式2
    @Autowired
    RabbitAdmin rabbitAdmin;

    @PostConstruct
    public void init (){
        Queue queue = new Queue(Constans.orderQueueKey_invail,true);
        rabbitAdmin.declareQueue(queue);
        Queue failqueue = new Queue(Constans.orderQueueKey_failure,true);
        rabbitAdmin.declareQueue(failqueue);
    }
}

 

 

 

2、消息生产者

 

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 
 * @description:
 * @Date: 11:32 2017/10/9
 */

@Component
public class MessageProducer {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    /**
     * @description 可以自定义发送消息的方法
     * @param queueName
     * @param message
     */
    public void sendMessage(String queueName, String message) {
        System.out.println(" send message:" + message);
        this.rabbitTemplate.convertAndSend(queueName, message);
    }

}

 

 

 

3、消息消费者

设置ack为手工确认,默认是自动确认,收到消息就从队列里删除消息。

 

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther: [email protected]
 * @description:
 * @Date: 11:33 2017/10/9
 */

@Component
public class MessageListener {

    /**
     * @param message
     * @param channel
     */
    @RabbitHandler
    @RabbitListener(queues = "hello")
    public void process(Message message, Channel channel) {
        System.out.println("Receiver : " +  message);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

 

 

4、配置文件

 

#rabbitMQ
spring.rabbitmq.host=ipaddress
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.maxConcurrentConsumers=2
spring.rabbitmq.concurrentConsumers=2
## 消息手动确认
spring.rabbitmq.listener.simple.acknowledge-mode=manual 
## 发布确认 异步
#spring.rabbitmq.publisher-confirms=true

 

 

 

 

 

 

你可能感兴趣的:(MQ,SpringCloud芝士,#,SpringCloud个人笔记)