Discussion:
[rabbitmq-discuss] How to configure reply queue?
Rajasekhar P
2013-11-08 05:36:01 UTC
Permalink
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131108/18f5b5ad/attachment.htm>
Gary Russell
2013-11-08 13:50:44 UTC
Permalink
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.
Post by Rajasekhar P
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131108/cd4b42fc/attachment.htm>
Rajasekhar P
2013-11-11 14:56:30 UTC
Permalink
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.
Post by Gary Russell
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.
Post by Rajasekhar P
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131111/42fb0ee1/attachment.htm>
Gary Russell
2013-11-11 15:27:01 UTC
Permalink
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.
Post by Rajasekhar P
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.
Post by Gary Russell
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.
Post by Rajasekhar P
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131111/4dc8b993/attachment.htm>
Rajasekhar P
2013-11-12 08:17:55 UTC
Permalink
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)?
Post by Gary Russell
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.
Post by Rajasekhar P
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.
Post by Gary Russell
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.
Post by Rajasekhar P
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>
Gary Russell
2013-11-12 13:35:59 UTC
Permalink
You need to return a result from your listener pojo

public Object handleMessage(String msg)

or

public List<Object> handleMessage(String msg)

etc.
Post by Rajasekhar P
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)?
<<snip>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20131112/7622ed25/attachment.htm>
Rajasekhar P
2013-11-12 16:23:25 UTC
Permalink
Hi Russell,

Here is the my listerner POJO , still am getting null response. I didnt do
any reply queue/corrID configuration.

// receive messages
public Object handleMessage(String query) {
Test test = new Test("Pandit", "Pillai");
return test;
}

Producer -

Test response = (Test)this.rabbitTemplate.convertSendAndReceive(msg);
System.out.println("Response : " + response);

Result -
*Response : null*


Could you please help me . Thanks in advance.
Post by Gary Russell
You need to return a result from your listener pojo
public Object handleMessage(String msg)
or
public List<Object> handleMessage(String msg)
etc.
Post by Rajasekhar P
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)?
<<snip>>
_______________________________________________
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/6647bb9b/attachment.htm>
Rajasekhar P
2013-11-15 08:57:38 UTC
Permalink
Thank you Russell. I could able to resolve after making change in
replyQueue manually in configuration for the request.
Post by Rajasekhar P
Hi Russell,
Here is the my listerner POJO , still am getting null response. I didnt do
any reply queue/corrID configuration.
// receive messages
public Object handleMessage(String query) {
Test test = new Test("Pandit", "Pillai");
return test;
}
Producer -
Test response = (Test)this.rabbitTemplate.convertSendAndReceive(msg);
System.out.println("Response : " + response);
Result -
*Response : null*
Could you please help me . Thanks in advance.
Post by Gary Russell
You need to return a result from your listener pojo
public Object handleMessage(String msg)
or
public List<Object> handleMessage(String msg)
etc.
Post by Rajasekhar P
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)?
<<snip>>
_______________________________________________
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/20131115/1a76ee3c/attachment.htm>
Continue reading on narkive:
Loading...