简体   繁体   中英

How to Implement RabbitMQ consumer part(receiver ) inside the Spring boot API

I am going to do send my DATA toRabbitMq producer(message sender) and get responsible data from RabbitMq consumer(message receiver). producer part is working fine .now my problem is how to implement consumer part (receiver part) in side the Spring boot API. .Below is My spring boot API and i written ProducerAndConsumer one class.

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 part

 @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));

            }

        }

The routing algorithm behind a direct exchange is simple - a message goes to the queues whose binding key exactly matches the routing key of the message.

spring amqp

Note: Check the routing key and queues binded using rabbitmq admin console to figure out whats going on or share the rabbitmq configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM