如何在Spring AMQP中发送和使用Object?

Pan*_*005 2 config amqp rabbitmq spring-amqp

我想使用Spring AMQP发送和使用自定义对象,如下所示.

制片人代码

记录=新记录("message1",新Date());
rabbitTemplate.convertAndSend(记录);

任何人都可以提供spring amqp @configuration设置来发送和使用上面的消息.谢谢!!!

Gar*_*ell 5

您应该看一下示例应用程序 ; 其中一些使用@Configuration.

但是,基本上,你需要......

@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();
}
Run Code Online (Sandbox Code Playgroud)

听众可以是POJO ......

public class Foo {

    public void handleMessage(Record foo) {
        System.out.println(foo);
    }
}  
Run Code Online (Sandbox Code Playgroud)

编辑:

我在这里为XML版本添加了一个Gist.