mercredi 18 mars 2015

spring-amqp Multiple queues with different routingKey

I have recently started learning Spring and spring-amqp so this question might appear very basic so please excuse me for that.


I have multiple queues which are on different hosts and have different QueueName, RoutingKey, vhost, user, password. I am writing the publishing logic for these queues and was not able to decide if I should have one configuration class per queue or can it be done in XML.


The method of creating a class to have all the information about the queue (host, vhost, username etc) is working fine as described in this example. I created a @Configuration class and defined all the beans for that Queue. But then I need to do



ApplicationContext context = new AnnotationConfigApplicationContext(MyQueueConfiguration.class);
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
amqpTemplate.convertAndSend("Hello World!!");


So my requirement is:



  1. Since I have many queues that need to be instantiated at the application startup, once tomcat starts the connections/channels to the queue/rabbit cluster should be established.

  2. Then as soon as there is POST request that comes to my application I need to publish the message to one of the queue based on a POST parameter.


So for each queue do I always need to do:



ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);


Or is there a way to Spring load the all my Queue configuration classes and just use the object like:



// calling code when I get a POST request
MyQueueConfigurationClass.publishMessage(payload, queueName);

// The implementation code
public boolean publishMessage(String payload, String queueName){

// Get Bean for the **queueName** somehow
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
// Use the bean to send the message
amqpTemplate.convertAndSend(payload);

}



  1. So how can I get the amqpTemplate for the exact queue without doing new AnnotationConfigApplicationContext() everytime?

  2. What is the harm of doing new AnnotationConfigApplicationContext every time a request comes to my service? [I am guessing that creating a new object for each request is not a good idea]


Aucun commentaire:

Enregistrer un commentaire