Example usage for org.springframework.amqp.rabbit.core RabbitTemplate sendAndReceive

List of usage examples for org.springframework.amqp.rabbit.core RabbitTemplate sendAndReceive

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core RabbitTemplate sendAndReceive.

Prototype

@Override
    @Nullable
    public Message sendAndReceive(final String exchange, final String routingKey, final Message message)
            throws AmqpException 

Source Link

Usage

From source file:org.resthub.rpc.AMQPHessianProxy.java

/**
 * Send request message//from  w  w w.  j  a  v a2  s  .com
 * 
 * @param connectionFactory Spring connection factory
 * @param method Method to call
 * @param args Method arguments
 * @return Response to the sent request
 * @throws IOException
 */
private Message sendRequest(ConnectionFactory connectionFactory, Method method, Object[] args)
        throws IOException {
    RabbitTemplate template = this._factory.getTemplate();

    byte[] payload = createRequestBody(method, args);

    MessageProperties messageProperties = new MessageProperties();
    messageProperties.setContentType("x-application/hessian");
    if (_factory.isCompressed()) {
        messageProperties.setContentEncoding("deflate");
    }

    Message message = new Message(payload, messageProperties);
    Message response = template.sendAndReceive(_factory.getRequestExchangeName(_factory.getServiceInterface()),
            _factory.getRequestQueueName(_factory.getServiceInterface()), message);

    return response;
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java

@Test
public void testAtomicSendAndReceiveWithExchangeAndRoutingKey() throws Exception {
    final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory());
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<Message> received = executor.submit(new Callable<Message>() {

        public Message call() throws Exception {
            Message message = null;// w w w .  j av  a  2 s . c o m
            for (int i = 0; i < 10; i++) {
                message = template.receive(ROUTE);
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return message;
        }

    });
    Message message = new Message("test-message".getBytes(), new MessageProperties());
    Message reply = template.sendAndReceive("", ROUTE, message);
    assertEquals(new String(message.getBody()),
            new String(received.get(1000, TimeUnit.MILLISECONDS).getBody()));
    assertNotNull("Reply is expected", reply);
    assertEquals(new String(message.getBody()), new String(reply.getBody()));
    // Message was consumed so nothing left on queue
    reply = template.receive(ROUTE);
    assertEquals(null, reply);
}