Package org.springframework.amqp.support.converter

Examples of org.springframework.amqp.support.converter.MessageConverter


    public static void main(String[] args) {
        final ApplicationContext rabbitConfig = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
        final ConnectionFactory rabbitConnectionFactory = rabbitConfig.getBean(ConnectionFactory.class);
        final Queue rabbitQueue = rabbitConfig.getBean(Queue.class);
        final MessageConverter messageConverter = new SimpleMessageConverter();

        // create a listener container, which is required for asynchronous message consumption.
        // AmqpTemplate cannot be used in this case
        final SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer();
        listenerContainer.setConnectionFactory(rabbitConnectionFactory);
        listenerContainer.setQueueNames(rabbitQueue.getName());

        // set the callback for message handling
        listenerContainer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                final BigOperation bigOp = (BigOperation) messageConverter.fromMessage(message);

                // simply printing out the operation, but expensive computation could happen here
                System.out.println("Received from RabbitMQ: " + bigOp);
            }
        });
View Full Code Here


    }

    @Override
    protected Message createMessage(Object object, MessageProperties messageProperties) {
        String contentType = messageProperties.getContentType();
        MessageConverter converter = converters.get(contentType);
        if(converter == null) //Try to fall back
            converter = this.fallbackConverter;
        if(converter == null) //Can't even fall back, punt
            throw new MessageConversionException("Cannot find converter for content type of "+contentType);
       
        return converter.toMessage(object, messageProperties);
    }
View Full Code Here

        MessageProperties messageProperties = message.getMessageProperties();
        String contentType = messageProperties.getContentType();
        if(messageProperties == null)
            throw new MessageConversionException("Cannot decode a message with no properties!");
       
        MessageConverter converter = converters.get(contentType);
        if(converter == null) //Try to fall back
            converter = this.fallbackConverter;
        if(converter == null) //Can't even fall back, punt
            throw new MessageConversionException("Cannot find converter for content type of "+contentType);
       
        return converter.fromMessage(message);
    }
View Full Code Here

        public void run() {
            org.apache.camel.Message message = exchange.getIn();
            SpringAMQPMessage inMessage = new SpringAMQPMessage(message);
            exchange.setIn(inMessage); //Swap out the old message format

            MessageConverter msgConverter;
            if(endpoint.getAmqpTemplate() instanceof RabbitTemplate) {
                RabbitTemplate rabbitTemplate = (RabbitTemplate) endpoint.getAmqpTemplate();
                msgConverter = rabbitTemplate.getMessageConverter();
            } else {
                LOG.warn("Cannot find RabbitMQ AMQP Template, falling back to simple message converter");
View Full Code Here

        public void run() {
            org.apache.camel.Message message = exchange.getIn();
            SpringAMQPMessage inMessage = new SpringAMQPMessage(message);
            exchange.setIn(inMessage); //Swap out the old message format

            MessageConverter msgConverter;
            if(endpoint.getAmqpTemplate() instanceof RabbitTemplate) {
                RabbitTemplate rabbitTemplate = (RabbitTemplate) endpoint.getAmqpTemplate();
                msgConverter = rabbitTemplate.getMessageConverter();
            } else {
                LOG.warn("Cannot find RabbitMQ AMQP Template, falling back to simple message converter");
View Full Code Here

        Assert.assertEquals(((BigOperation) amqpTemplate.receiveAndConvert(rabbitQueue.getName())).getName(), "foo");
    }

    @Test
    public void testAsynchronous() throws Exception {
        final MessageConverter messageConverter = new SimpleMessageConverter();
        final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory);
        container.setQueueNames(rabbitQueue.getName());

        final CountDownLatch fooLatch = new CountDownLatch(1);
        final CountDownLatch barLatch = new CountDownLatch(2);
        final List<BigOperation> receievedMessageHolder = new ArrayList<BigOperation>(2);
        container.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                receievedMessageHolder.add((BigOperation) messageConverter.fromMessage(message));
                fooLatch.countDown();
                barLatch.countDown();
            }
        });
        container.setErrorHandler(new ErrorHandler() {
View Full Code Here

        public void run() {
            org.apache.camel.Message message = exchange.getIn();
            SpringAMQPMessage inMessage = new SpringAMQPMessage(message);
            exchange.setIn(inMessage); //Swap out the old message format

            MessageConverter msgConverter;
            if(endpoint.getAmqpTemplate() instanceof RabbitTemplate) {
                RabbitTemplate rabbitTemplate = (RabbitTemplate) endpoint.getAmqpTemplate();
                msgConverter = rabbitTemplate.getMessageConverter();
            } else {
                LOG.warn("Cannot find RabbitMQ AMQP Template, falling back to simple message converter");
View Full Code Here

    connectionFactory.setHost("localhost");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    assertNotNull(connectionFactory);

    MessageConverter messageConverter = new SimpleMessageConverter();
    MessageProperties  messageProperties = new MessageProperties();
    messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
View Full Code Here

  }

  @Test
  public void convertMessageConversionExceptionOnSend() {
    Message<String> message = createTextMessage();
    MessageConverter messageConverter = mock(MessageConverter.class);
    willThrow(org.springframework.amqp.support.converter.MessageConversionException.class)
        .given(messageConverter).toMessage(eq(message), anyMessageProperties());
    messagingTemplate.setAmqpMessageConverter(messageConverter);

    thrown.expect(org.springframework.messaging.converter.MessageConversionException.class);
View Full Code Here

  }

  @Test
  public void convertMessageConversionExceptionOnReceive() {
    org.springframework.amqp.core.Message message = createAmqpTextMessage();
    MessageConverter messageConverter = mock(MessageConverter.class);
    willThrow(org.springframework.amqp.support.converter.MessageConversionException.class)
        .given(messageConverter).fromMessage(message);
    messagingTemplate.setAmqpMessageConverter(messageConverter);
    given(rabbitTemplate.receive("myQueue")).willReturn(message);
View Full Code Here

TOP

Related Classes of org.springframework.amqp.support.converter.MessageConverter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.