1. 程式人生 > >SpringBoot整合RabbitMQ之基礎例項2

SpringBoot整合RabbitMQ之基礎例項2

此文承接SpringBoot整合RabbitMQ之基礎例項1的所有配置

配置交換機,佇列及繫結關係

package com.etoak.crazy.config.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitMQConfig {
		
		//定義一個Topic型別的交換機
		@Bean("topicExchange")
		public TopicExchange getTopicExchange() {
			return new TopicExchange("TopicTest");
		}
		
		//定義一個Fanout型別的交換機
		@Bean("fanoutExchange")
		public FanoutExchange getFanoutExchange() {
			return new FanoutExchange("FanoutTest");
		}
		
		//分別建立三個佇列
		@Bean("queue1")
		public Queue getQueue1() {
			return new Queue("Queue1");
		}
		
		@Bean("queue2")
		public Queue getQueue2() {
			return new Queue("Queue2");
		}
		
		@Bean("queue3")
		public Queue getQueue3() {
			return new Queue("Queue3");
		}
		
		//將佇列2繫結給Topic交換器,key為#.etoak.#
		@Bean
	    public Binding bindSecond(@Qualifier("queue2") Queue queue,@Qualifier("topicExchange") TopicExchange exchange){
	        return BindingBuilder.bind(queue).to(exchange).with("#.etoak.#");
	    }
		
		//將佇列3繫結給Fanout交換器
		@Bean
	    public Binding bind3(@Qualifier("queue3") Queue queue,@Qualifier("fanoutExchange") FanoutExchange exchange){
	        return BindingBuilder.bind(queue).to(exchange);
	    }
}

建立生產者

package com.etoak.crazy.study.rabbitmq.producer;

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

@Component
public class ProducerDemo {
	
	@Autowired
	private AmqpTemplate rabbitTemplate;
	
	public void send() {
		
		//不指定交換機,預設使用Direct交換機,點對點發送一條訊息給佇列1
		rabbitTemplate.convertAndSend("","Queue1","第一條訊息");
		
		//指定Topic型別交換機,key為符合#.etoak.#格式
		rabbitTemplate.convertAndSend("TopicTest","www.etoak.com","一條符合規則的訊息");
		//指定Topic型別交換機,key為不符合#.etoak.#格式
		rabbitTemplate.convertAndSend("TopicTest","toak","一條不符合規則的訊息");
		
		//指定Fanout型別交換機,不指定key
		rabbitTemplate.convertAndSend("FanoutTest","","廣播的訊息");
		
	}
}

分別建立消費者(此處展示消費者1,剩餘消費者將1改為2,3即可)

package com.etoak.crazy.study.rabbitmq.consumer;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
//監聽名為Queue1的佇列
@RabbitListener(queues = "Queue1")
public class ConsumerDemo1 {

	@RabbitHandler
    public void process(String message) {
        System.out.println("消費者1接收訊息為  : " + message);
    }
}

測試

package com.etoak.crazy.test.rabbitmq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.etoak.crazy.study.rabbitmq.producer.ProducerDemo;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqDemoTest {
	
	@Autowired
	ProducerDemo producerdemo;

	@Test
	public void hello() throws Exception {
		producerdemo.send();
	}

}

結果
在這裡插入圖片描述