Search code examples
rabbitmqconfigamqpspring-amqp

How to send & consume Object in Spring AMQP?


I want to send and consume the custom object as below using Spring AMQP.

Producer code

Record record = new Record("message1", new Date());
rabbitTemplate.convertAndSend(record);

Can anyone provide spring amqp @configuration settings for sending and consuming messages as above. Thanks!!!


Solution

  • You should take a look at the Sample Applications; some of them use @Configuration.

    But, essentially, you need...

    @Bean
    public SimpleMessageListenerContainer container() {
        SimpleMessageListenerContainer container =
                new SimpleMessageListenerContainer(connectionFactory());
        MessageListenerAdapter adapter = new MessageListenerAdapter(myListener());
        container.setMessageListener(adapter);
        container.setQueues(foo());
        return container;
    }
    
    @Bean
    public Object myListener() {
        return new Foo();
    }
    

    and the listener can be a POJO...

    public class Foo {
    
        public void handleMessage(Record foo) {
            System.out.println(foo);
        }
    }  
    

    EDIT:

    I added a Gist here for XML version.