Direct exchange(直连交换机)
2019-07-31 本文已影响0人
初心myp
默认交换机的本质就是名称为空的直连交换机
直连交换机是根据消息携带的路由键、将消息投递给对应队列的。
工作流程:
将一个队列绑定在某个交换机上,同时赋予该绑定一个路由键,当一个携带着路由键为R的消息,被发送给直连交换机时,交换机会把他路由给绑定值同样为R的队列
具体实现:
首先我们需要一个交换机的配置类:
package com.chuxin.fight.demo.rabbitmq.direct;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @FileName: RabbitDirectConfig
* @Description: 配置队列
* @author: <a href="mailto: muyuanpei@camelotchina.com">myp</a>
* @create: 2018-11-13 17:15
* @Copyright: (c) 2018年 北京柯莱特科技有限公司
*/
@Configuration
public class RabbitDirectConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
}
@Bean
public Queue directQueue() {
return new Queue("direct");
}
//-------------------配置默认的交换机模式,可以不需要配置以下-----------------------------------
@Bean
DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
//绑定一个key "direct",当消息匹配到就会放到这个队列中
@Bean
Binding bindingExchangeDirectQueue(Queue directQueue, DirectExchange directExchange) {
return BindingBuilder.bind(directQueue).to(directExchange).with("direct");
}
// 推荐使用 helloQueue() 方法写法,这种方式在 Direct Exchange 模式 多此一举,没必要这样写
//---------------------------------------------------------------------------------------------
}
然后编写对应的接受者:
package com.chuxin.fight.demo.rabbitmq.direct;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @FileName: DirectReceiver
* @Description: 监听队列--接受者
* @author: <a href="mailto: muyuanpei@camelotchina.com">myp</a>
* @create: 2018-11-13 17:19
* @Copyright: (c) 2018年 北京柯莱特科技有限公司
*/
@Component
@RabbitListener(queues = "hello")
public class DirectReceiver {
@RabbitHandler
public void process(String message) {
System.out.println("接收者 DirectReceiver," + message);
}
}
package com.chuxin.fight.demo.rabbitmq.direct;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @FileName: HelloReceiver
* @Description: 监听队列--接受者
* @author: <a href="mailto: ***@163.com">myp</a>
* @create: 2018-11-13 17:18
*/
@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {
@RabbitHandler
public void process(String message) {
System.out.println("接收者 helloReceiver," + message);
}
}
最后编写测试类,测试我们的配置和编码:
package com.chuxin.fight.demo.rabbitmq.direct;
import com.chuxin.fight.demo.DemoApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @FileName: RabbitDirectTest
* @Description: 直连交换机模式
* @author: <a href="mailto: muyuanpei@camelotchina.com">myp</a>
* @create: 2018-11-13 17:23
* @Copyright: (c) 2018年 北京柯莱特科技有限公司
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class RabbitDirectTest {
@Autowired
private AmqpTemplate rabbitTemplate;
@Test
public void sendHelloTest() {
String context = "发送的第一个消息";
//String context2 = "发送的第二个消息";
//String context3 = "发送的第三个消息";
String routeKey = "hello";
context = "routeKey:" + routeKey + ",context:" + context;
System.out.println("sendHelloTest : " + context);
this.rabbitTemplate.convertAndSend(routeKey, context);
//this.rabbitTemplate.convertAndSend(routeKey, context2);
//this.rabbitTemplate.convertAndSend(routeKey, context3);
}
@Test
public void sendDirectTest() {
String context = "此消息在,默认的交换机模式队列下,有 DirectReceiver 可以收到";
String routeKey = "direct";
String exchange = "directExchange";
context = "context:" + exchange + ",routeKey:" + routeKey + ",context:" + context;
System.out.println("sendDirectTest : " + context);
// 推荐使用 sendHello() 方法写法,这种方式在 Direct Exchange 多此一举,没必要这样写
this.rabbitTemplate.convertAndSend(exchange, routeKey, context);
}
}
这样就完成了一个直连交换机的配置与编码。由于自己的服务器过期了,不能展示相应的打印结果。。。