如何在Spring Boot API中实现RabbitMQ消费者部分(receiver)

时间:2019-02-21 16:17:45

标签: java spring spring-boot rabbitmq

我将把数据发送到RabbitMq生产者(消息发送者),并从RabbitMq消费者(消息接收者)获得负责任的数据。生产者部分工作正常。现在我的问题是如何在Spring Boot API的侧面实现消费者部分(接收者部分)。下面是我的spring boot 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));

            }

        }

1 个答案:

答案 0 :(得分:0)

  

直接交换背后的路由算法很简单-消息进入绑定键与消息的路由键完全匹配的队列。

spring amqp

注意:检查路由键和使用Rabbitmq管理控制台绑定的队列,以了解发生了什么或共享Rabbitmq配置。