RabbitMQ:SpringBoot+RabbitMQ的简单实现之Headers模式

RabbitMQ:SpringBoot+RabbitMQ的简单实现之Headers模式


1.在pom中添加springboot对amqp的支持

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.在application.properties中添加RabbitMQ的简单配置信息

spring.rabbitmq.host=127.0.0.1
#5672是发送消息端口,15672是管理端的端口
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3.配置Queue(消息队列)

@Configuration
public class QueueConfig {

	@Bean(name = "headers_queue")
	public Queue headers_queue() {
		return new Queue("headers_queue");
	}
	@Bean
	public HeadersExchange exchange() {
		return new HeadersExchange("headers_exchange");
	}

	@Bean
	Binding bindingExchangeTopicQueue2(@Qualifier("headers_queue") Queue queue, HeadersExchange exchange) {
		 Map<String, Object> map = new HashMap<>();
	        map.put("One","A");
	        map.put("Two","B");
	    
		 //whereAll表示全部匹配
		 //return BindingBuilder.bind(queue).to(headersExchange).whereAll(map).match();
	        
		 //whereAny表示部分匹配
		 return BindingBuilder.bind(queue).to(headersExchange).whereAny(map).match();
	}
}

4.编写消息生产者

@Component
public class Sender_Headers {

	@Autowired
	private RabbitTemplate rabbitTemplate;
    
    /**
     * @param exchangeName 交换机名称
     * @param message      消息信息
     */
    public void send(String exchangeName,Message message) {
    	rabbitTemplate.convertAndSend(exchangeName,null, message);
    }
}

5.编写消息消费者

@Component
public class Receive_Headers {

	//监听器监听指定的Queue
    @RabbitListener(queues="headers_queue")    
    public void process(Message message) throws UnsupportedEncodingException {
    	MessageProperties messageProperties = message.getMessageProperties();
    	
    	String contentType = messageProperties.getContentType();
    	
        System.out.println("Receive-Headers:"+new String(message.getBody(), contentType));
    }

}

6.编写测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRabbitMQ_Headers {
    
    @Autowired
    private Sender_Headers sender_Headers; 
    
    @Test
    public void testRabbit_Headers() {
		/**
		 * headers模式不满足匹配规则
		 */
		test_match_no();

		/**
		 * headers模式满足全部匹配规则
		 */
		test_match_all();

		/**
		 * headers模式满足部分匹配规则
		 */
		test_match_any();
	}

	public void test_match_no() {
		/**
		 * 声明消息 (消息体, 消息属性)
		 */
		MessageProperties messageProperties = new MessageProperties();
			// 设置消息是否持久化。Persistent表示持久化,Non-persistent表示不持久化
			messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
			messageProperties.setContentType("UTF-8");
			messageProperties.setHeader("Three", "C");

		Message message = new Message("hello,rabbit_headers_no!".getBytes(), messageProperties);

		sender_Headers.send("headers_exchange", message);
	}

	public void test_match_all() {
		Map<String, Object> map_all = new HashMap<>();
			map_all.put("One", "A");
			map_all.put("Two", "B");

		/**
		 * 声明消息 (消息体, 消息属性)
		 */
		MessageProperties messageProperties = new MessageProperties();
			// 设置消息是否持久化。Persistent表示持久化,Non-persistent表示不持久化
			messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
			messageProperties.setContentType("UTF-8");
			messageProperties.getHeaders().putAll(map_all);

		Message message = new Message("hello,rabbit_headers_all!".getBytes(), messageProperties);

		sender_Headers.send("headers_exchange", message);
	}

	public void test_match_any() {
		Map<String, Object> map_any = new HashMap<>();
			map_any.put("One", "A");

		/**
		 * 声明消息 (消息体, 消息属性)
		 */
		MessageProperties messageProperties = new MessageProperties();
			// 设置消息是否持久化。Persistent表示持久化,Non-persistent表示不持久化
			messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
			messageProperties.setContentType("UTF-8");
			messageProperties.getHeaders().putAll(map_any);

		Message message = new Message("hello,rabbit_headers_any!".getBytes(), messageProperties);

		sender_Headers.send("headers_exchange", message);
	}
}

接下来就可以测试啦,首先启动接收端的应用,紧接着运行发送端的单元测试,接收端应用打印出来接收到的消息,测试即成功!

©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页