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

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

Introduction

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

Prototype

public MessageConverter getMessageConverter() 

Source Link

Document

Return the message converter for this template.

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  www . ja va2  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   w  ww .j  a va2  s .  c o  m
    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);
}