[rabbitmq-discuss] How to configure reply queue?

Rajasekhar P sekhar434145 at gmail.com
Tue Nov 12 08:17:55 GMT 2013


Hi Russell,

Thanks for the support. I have followed the documentation am getting
response as null. Below is the producer and consumer code.

PRODUCER -

// rabbittemplate
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setRoutingKey(this.queueName);
template.setQueue(this.queueName);
template.setMessageConverter(new JsonMessageConverter());
return template;
}

String msg = "CUSTOMER1";
Object res = this.rabbitTemplate.convertSendAndReceive(msg);
System.out.println("Message sent :" +msg);
System.out.println("Response : " + res.toString());

CONSUMER -

SimpleMessageListenerContainer container = new
SimpleMessageListenerContainer();
container.setConnectionFactory(this.connectionFactory);
container.setQueueNames(this.queueName);
container.setMessageListener(new *MessageListenerAdapter*(new
TestMessageHandler(this.queueName), new JsonMessageConverter()));
container.start();


       // receive messages
public void handleMessage(String msg) {
System.out.println("Received  :" + msg);
}

*Result - *
Message sent :CUSTOMER1
java.lang.NullPointerException


Here, inside handleMessage based on message I want get the results from DB
and need to send back. Am confusing how set the response to that
client(Producer)?


On Mon, Nov 11, 2013 at 8:57 PM, Gary Russell <grussell at gopivotal.com>wrote:

> Please read the documentation...
> http://docs.spring.io/spring-amqp/reference/html/amqp.html#d4e460
>
> It explains that, when you use a fixed reply queue, and roll your own
> configuration like this, you have to wire up a listener container for the
> replies (with the rabbitTemplate as the listener).
>
> If you use a listener-container on the consumer side (with a
> MessageListenerAdapter), it will automatically send the result to the
> replyTo. Your consumer can be a simple POJO method that returns a list of
> Objects. Use a JsonMessageConverter in the adapter to convert them to JSON.
>
>
>
>
> On Mon, Nov 11, 2013 at 9:56 AM, Rajasekhar P <sekhar434145 at gmail.com>wrote:
>
>> Thank you Russell for the response. Still am stuck with achieving above
>> program, could please tell where am failing while doing above code. Below
>> is the code at producer end.
>>
>>
>>
>> CONFIGURATION -
>>
>> public RabbitTemplate rabbitTemplate() {
>> RabbitTemplate template = new RabbitTemplate(connectionFactory());
>>  template.setRoutingKey(this.queueName);
>> template.setQueue(this.queueName);
>> template.setMessageConverter(new JsonMessageConverter());
>>  return template;
>> }
>>
>> public ConnectionFactory connectionFactory() {
>>  CachingConnectionFactory connectionFactory = new
>> CachingConnectionFactory(
>> "localhost");
>>  connectionFactory.setUsername("guest");
>> connectionFactory.setPassword("guest");
>> return connectionFactory;
>>  }
>>
>>
>> PRODUCER CODE -
>>
>>
>> String corrId = UUID.randomUUID().toString();
>>  String replyQueueName = *channel*.queueDeclare().getQueue();
>>  MessageProperties prop = new MessageProperties();
>>  prop.setCorrelationId(corrId.getBytes());
>> prop.setReplyTo(replyQueueName);
>> Message message = new Message(query.getBytes(), prop);
>>  Message msg = rabbitTemplate.sendAndReceive(message);
>>
>>
>> Is this correct at producer end? Here how to Channel to retrieve
>> reqplyQueueName? Could you please correct me on this and please tell me how
>> to write consumer code to send the result(List of Objects) in JSON format
>> back to that particular request in replyqueue? Thanks in advance.
>>
>>
>> On Fri, Nov 8, 2013 at 7:20 PM, Gary Russell <grussell at gopivotal.com>wrote:
>>
>>> http://docs.spring.io/spring-amqp/reference/html/amqp.html#request-reply
>>>
>>> The simplest is to configure the RabbitTemplate to use a fixed reply
>>> queue, with a reply listener, and use one of it's
>>> convertSendAndReceive(...) (or sendAndReceive) methods; the template will
>>> take care of the correlation.
>>>
>>> If you want to do it asynchronously and do your own correlation (get the
>>> reply asynchronously on another thread) just use one of the send() methods
>>> and wire up a listener container to get the results.
>>>
>>>
>>>
>>> On Fri, Nov 8, 2013 at 12:36 AM, Rajasekhar P <sekhar434145 at gmail.com>wrote:
>>>
>>>> Hi Team,
>>>>
>>>> We are trying to make asynchronous call in RabbitMQ using Spring AMQP,
>>>> could any one please tell me how to configure *replyqueue,
>>>> correlationId, (properties) *using spring amqp?
>>>>
>>>>  String corrId = java.util.UUID.randomUUID().toString();
>>>>  BasicProperties props = new BasicProperties
>>>>                                 .Builder()
>>>>                                 .correlationId(corrId)
>>>>                                 .replyTo(replyQueueName)
>>>>                                 .build();
>>>>
>>>>  channel.basicPublish("", requestQueueName, props, message.getBytes());
>>>>
>>>>
>>>> Thanks in advance.
>>>>
>>>> _______________________________________________
>>>> rabbitmq-discuss mailing list
>>>> rabbitmq-discuss at lists.rabbitmq.com
>>>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>>>>
>>>>
>>>
>>> _______________________________________________
>>> rabbitmq-discuss mailing list
>>> rabbitmq-discuss at lists.rabbitmq.com
>>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>>>
>>>
>>
>> _______________________________________________
>> rabbitmq-discuss mailing list
>> rabbitmq-discuss at lists.rabbitmq.com
>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>>
>>
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131112/bc95591c/attachment.htm>


More information about the rabbitmq-discuss mailing list