In My Scenario I need to create a lots of queues dynamically at run time that is why I don't want to use @Bean instead want to write a function that create queue and I will call it whenever necessary. Here When i use @bean annotation it creates queue on rabbitmq server.
@Bean
public Queue productQueue(final String queueName) {
return new Queue(queueName);
}
But with the same code without @Bean
public Queue productQueue(final String queueName) {
return new Queue(queueName);
}
when call this function doesn't create queue on rabbitmq server
Queue queue = <Object>.productQueue("product-queue");
CodePudding user response:
The Queue
object must be a bean in the context and managed by Spring. To create queues dynamically at runtime, define the bean with scope prototype
:
@Bean
@Scope("prototype")
public Queue productQueue(final String queueName) {
return new Queue(queueName);
}
and create queues at runtime using ObjectProvider
:
@Autowired
private ObjectProvider<Queue> queueProvider;
Queue queue1 = queueProvider.getObject("queueName1");
Queue queue2 = queueProvider.getObject("queueName2");