我将把我的DATA发送到RabbitMq生产者(消息发送者)并从RabbitMq消费者(消息接收者)获取负责任的数据。生产者部分工作正常。现在我的问题是如何在Spring引导API的一侧实现消费者部分(接收器部分)。 .Below是我的春季启动API,我写了一个类ProducerAndConsumer。
ProducerAndConsumer.class
@Component
public class ProducerAndConsumer {
@Autowired
private RabbitTemplate rabbitTemplate;
//MessageProducer part (send part)
public boolean sendMessage(String message) {
rabbitTemplate.convertAndSend(RobbitMqConfig.ROUTING_KEY, message);
System.out.println("Is listener returned ::: ==========="+rabbitTemplate.isReturnListener());
return rabbitTemplate.isReturnListener();
}
//Consumer part (receiver part)
@RabbitListener(queues = RobbitMqConfig.QUEUE_NAME1)
public void receiveMessage ( final Message message){
System.out.println("Received message====Receiver=====" + message.getPayload());
}
}
API部分
@PostMapping(value = {"/sendFilesName"})
public ResponseEntity<?> sendFilesName(@RequestBody SendFileNameRequest sendFileNameRequest, HttpServletRequest request) throws ParseException {
System.out.println("FileNameArray="+sendFileNameRequest.getFileNameArray());
if(sendFileNameRequest.getFileNameArray().size()!=0) {
List<String> message = sendFileNameRequest.getFileNameArray();
**//see here i send my message array data**
if(producerAndConsumer.sendMessage(message.toString())){
**//here i want implement my receiver part how to?**
return ResponseEntity.ok(new ApiResponse(true, "fileName List sent successfully", "",true));
}else {
return ResponseEntity.ok(new ApiResponse(false, "fileName List sent Fails", "",true));
}
}else {
return ResponseEntity.ok(new ApiResponse(false, "fileName List not present ", "",true));
}
}
直接交换背后的路由算法很简单 - 消息进入队列,其绑定密钥与消息的路由密钥完全匹配。
注意:使用rabbitmq管理控制台检查路由密钥和绑定的队列,以确定正在进行的操作或共享rabbitmq配置。