简体   繁体   English

当 spring 启动应用程序启动时,如何在容器内添加队列?

[英]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目前,我正在使用 spring boot 和 spring-amqp 收听通过 API 调用创建的新队列,这是成功运行的代码

@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.但是如果应用程序再次启动,我需要在容器(DirectMessageListenerContainer)中注册队列并在应用程序启动时添加队列,比如 queue1 到侦听器容器 ID“queueContainer”。

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我无法收听队列列表并将其添加到启动时的listner容器中。你能在这方面提供帮助吗

You need to persist the queue names someplace (eg a db, redis etc) and send new messages to the control listener queue during start up.您需要将队列名称保存在某个地方(例如数据库、redis 等),并在启动期间将新消息发送到控制侦听器队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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