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

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

Introduction

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

Prototype

@Override
    @Nullable
    public Object receiveAndConvert() throws AmqpException 

Source Link

Usage

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

@Test
public void testAtomicSendAndReceiveWithConversion() throws Exception {
    final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory());
    template.setRoutingKey(ROUTE);/*from   w w  w. j  a  v a 2 s.  c om*/
    template.setQueue(ROUTE);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<String> received = executor.submit(new Callable<String>() {

        public String call() throws Exception {
            Message message = null;
            for (int i = 0; i < 10; i++) {
                message = template.receive();
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return (String) template.getMessageConverter().fromMessage(message);
        }

    });
    String result = (String) template.convertSendAndReceive("message");
    assertEquals("message", received.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("message", result);
    // Message was consumed so nothing left on queue
    result = (String) template.receiveAndConvert();
    assertEquals(null, result);
}

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

@Test
public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessor() throws Exception {
    final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory());
    template.setRoutingKey(ROUTE);//from ww w.j  a v  a  2s.  c om
    template.setQueue(ROUTE);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    // Set up a consumer to respond to our producer
    Future<String> received = executor.submit(new Callable<String>() {

        public String call() throws Exception {
            Message message = null;
            for (int i = 0; i < 10; i++) {
                message = template.receive();
                if (message != null) {
                    break;
                }
                Thread.sleep(100L);
            }
            assertNotNull("No message received", message);
            template.send(message.getMessageProperties().getReplyTo(), message);
            return (String) template.getMessageConverter().fromMessage(message);
        }

    });
    String result = (String) template.convertSendAndReceive((Object) "message", new MessagePostProcessor() {
        public Message postProcessMessage(Message message) throws AmqpException {
            try {
                byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8");
                return new Message(newBody, message.getMessageProperties());
            } catch (Exception e) {
                throw new AmqpException("unexpected failure in test", e);
            }
        }
    });
    assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS));
    assertEquals("MESSAGE", result);
    // Message was consumed so nothing left on queue
    result = (String) template.receiveAndConvert();
    assertEquals(null, result);
}