Srinivasula Reddy
Srinivasula Reddy

Reputation: 59

how to add queues inside container when spring boot application booting up?

Currently, I am listening to new queue created through API call by using spring boot and spring-amqp this is the piece code which is successfully working

@RestController
@RequestMapping("/api/register/")
@NoArgsConstructor
public class RegisterQueuesController {
    @Autowired
    SimpleReceiver simpleReceiver;

    @PostMapping(value = "/queues")
    public ResponseEntity<String> addAll(final @RequestBody RegisteringQueues registeringQueues) {
        simpleReceiver.processQueueEvents(registeringQueues);
        return new ResponseEntity<>("SUCCESS", HttpStatus.CREATED);
    }
}


@Component
public class SimpleReceiver {

    @Autowired
    private RabbitListenerEndpointRegistry listenerEndpointRegistry;


    @RabbitListener(id = "qEvent")
    public void processQueueEvents(RegisteringQueues registeringQueues) {
        ((DirectMessageListenerContainer) this.listenerEndpointRegistry.getListenerContainer("queueContainer"))
                .addQueueNames(registeringQueues.getQueue());
        // processBeanQueueEvents();
        System.out.println("Received a message with the new queue name: " + registeringQueues.getQueue());

    }

    @RabbitListener(id = "queueContainer")
    public void processMessages(TestQueues testQueues, @Header(AmqpHeaders.CONSUMER_QUEUE) String queue) {
        System.out.println("Received a message on queue: " + queue + "data: " + testQueues);
        //process testQueues
    }

//postman payload
// http://localhost:8080/api/register/queues 

    {
    
        "queue":[
            "queue1",
            "queue2",
            "queue3"
        ]
    }

`

but if application again boots up again I need to register queues inside container (DirectMessageListenerContainer) and add the queue,say queue1 to the listener container id "queueContainer" when ever application starts.

Am not able to listen the list of queues and add it to the listner container on bootup.Can you Please help in this regard

Upvotes: 1

Views: 560

Answers (1)

Gary Russell
Gary Russell

Reputation: 174729

You need to persist the queue names someplace (e.g. a db, redis etc) and send new messages to the control listener queue during start up.

Upvotes: 1

Related Questions