rabbitmq

  最新因为项目中会使用rabbitmq,所以这里做个简单的随笔。

  

看图所示,由生产者(producer)  交换机(exchange)  队列(queue)消费者(组成)

一般程序设计需要用到rabbitmq的时候,都是这种情况。

当消息发送到队列之时,可以选择先到交换机,通过交换机去选择把消息送到不同的队列中去。所以一般采用Topic Exchange方式去消息推送接收。

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

@Configuration
public class TopicRabbitConfig {

public final static String man = "topic.man";

public final static String woman = "topic.woman";

@Bean
TopicExchange exchange() {
return new TopicExchange("topicExchange");
}

@Bean
public Queue manQueue() {
return new Queue(man);
}

@Bean
public Queue womanQueue() {
return new Queue(woman);
}

@Bean
Binding bindingExchangeMessage() {
return BindingBuilder.bind(manQueue()).to(exchange()).with(man);
}

@Bean
Binding bindingExchangeMessage2() {
return BindingBuilder.bind(womanQueue()).to(exchange()).with("topic.#");
}


}
@Component
@RabbitListener(queues = "topic.man")
public class TopicReceiver {

@RabbitHandler
public void process(Map testMessage) {
System.out.println("TopicTotalReceiver消费者收到消息 MAN : " + testMessage.toString());
}
}

@Component
@RabbitListener(queues = "topic.woman")
public class TopicTotalReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("TopicTotalReceiver消费者收到消息 : " + testMessage.toString());
}
}

@RestController
public class SendMessageController {

@Autowired
private RabbitTemplate rabbitTemplate;


@GetMapping("/sendDirectMessage")
public String sendDirectMessage() {
String messageId = UUID.randomUUID().toString();
String data = "hello,rabbitMQ";
String createTime = LocalDateTime.now().format(DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("data", data);
map.put("createTime", createTime);
rabbitTemplate.convertAndSend("TestDirectExchange","TestDirectRouting",map);
return "ok";
}


@GetMapping("/sendDirectMessage1")
public String sendDirectMessage1() {
String messageId = UUID.randomUUID().toString();
String data = "message is man";
String createTime = LocalDateTime.now().format(DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("data", data);
map.put("createTime", createTime);
rabbitTemplate.convertAndSend("topicExchange","topic.man",map);
return "ok";
}


@GetMapping("/sendDirectMessage2")
public String sendDirectMessage2() {
String messageId = UUID.randomUUID().toString();
String data = "message is woman";
String createTime = LocalDateTime.now().format(DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("data", data);
map.put("createTime", createTime);
rabbitTemplate.convertAndSend("topicExchange","topic.woman",map);
return "ok";
}
}

可以自己测试一下结果,topicexchange选择送往哪个queue,可以看到绑定topic.#这个队列会接收man以及woman的信息
topic.man只会接收man的信息
你看可以试试就知道了!

这里还有消息确认机制,明天有空在写。

 

posted on 2020-03-29 23:13  Jason_LZP  阅读(128)  评论(0)    收藏  举报