amqp java,使用Spring AMQP接收和发送Java对象

I want to implement Spring AMQP example for sending and receiving Java Objects using listener. I tried this:

Send Java Object

ConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");

AmqpAdmin admin = new RabbitAdmin(connectionFactory);

admin.declareBinding(BindingBuilder.bind(new Queue(QUEUE_PROCESSING_TRANSACTION, false)).to(new TopicExchange(EXCHANGE_PROCESSING)).with(ROUTING_KEY_PROCESSING_TRANSACTION));

AmqpTemplate template = new RabbitTemplate(connectionFactory);

TransactionsBean obj = new TransactionsBean();

obj.setId(Long.valueOf(111222333));

Receive and send Back another Java Object:

ConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");

AmqpAdmin admin = new RabbitAdmin(connectionFactory);

admin.declareBinding(BindingBuilder.bind(new Queue(QUEUE_PROCESSING_TRANSACTION, false))

.to(new TopicExchange(EXCHANGE_PROCESSING)).with(ROUTING_KEY_PROCESSING_TRANSACTION));

AmqpTemplate template = new RabbitTemplate(connectionFactory);

TransactionsBean obj = (TransactionsBean) template.receiveAndConvert(QUEUE_PROCESSING_TRANSACTION);

System.out.println(" !!!!!!! Received id " + obj.getTransaction_id());

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();

container.setConnectionFactory(connectionFactory);

container.setQueues(new Queue(QUEUE_PROCESSING_TRANSACTION, false));

container.setMessageListener(new MessageListener() {

@Override

public void onMessage(Message message) {

// Receive here Java object and send back another object

}

});

Can you show me how to extend the code without complex annotations just simple listeners?

解决方案

The simplest way is to use a @RabbitListener - made even easier when using Spring Boot since he will wire up infrastructure beans (template, admin, etc).

@SpringBootApplication

public class So51009346Application {

public static final String QUEUE_PROCESSING_TRANSACTION = "q1";

public static void main(String[] args) {

SpringApplication.run(So51009346Application.class, args);

}

@Bean

public ApplicationRunner runner(RabbitTemplate template) {

return args -> {

ReplyObject reply = (ReplyObject) template.convertSendAndReceive("ex", "rk", new RequestObject());

System.out.println(reply);

};

}

@Bean

public Queue queue() {

return new Queue(QUEUE_PROCESSING_TRANSACTION);

}

@Bean

public TopicExchange te() {

return new TopicExchange("ex");

}

@Bean

public Binding binding() {

return BindingBuilder.bind(queue()).to(te()).with("rk");

}

}

class RequestObject implements Serializable {

private static final long serialVersionUID = 1L;

}

class ReplyObject implements Serializable {

private static final long serialVersionUID = 1L;

}

@Component

class Listener {

@RabbitListener(queues = So51009346Application.QUEUE_PROCESSING_TRANSACTION)

public ReplyObject process(RequestObject ro) {

return new ReplyObject();

}

}

If you don't want to use that annotation for some reason, you can wire up a container using a MessageListenerAdapter...

@SpringBootApplication

public class So51009346Application {

public static final String QUEUE_PROCESSING_TRANSACTION = "q1";

public static void main(String[] args) {

SpringApplication.run(So51009346Application.class, args);

}

@Bean

public ApplicationRunner runner(RabbitTemplate template) {

return args -> {

ReplyObject reply = (ReplyObject) template.convertSendAndReceive("ex", "rk", new RequestObject());

System.out.println(reply);

};

}

@Bean

public SimpleMessageListenerContainer container(ConnectionFactory cf, Listener listener) {

SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);

container.setQueueNames(QUEUE_PROCESSING_TRANSACTION);

container.setMessageListener(new MessageListenerAdapter(listener, "process"));

return container;

}

@Bean

public Queue queue() {

return new Queue(QUEUE_PROCESSING_TRANSACTION);

}

@Bean

public TopicExchange te() {

return new TopicExchange("ex");

}

@Bean

public Binding binding() {

return BindingBuilder.bind(queue()).to(te()).with("rk");

}

}

class RequestObject implements Serializable {

private static final long serialVersionUID = 1L;

}

class ReplyObject implements Serializable {

private static final long serialVersionUID = 1L;

}

@Component

class Listener {

public ReplyObject process(RequestObject ro) {

return new ReplyObject();

}

}

You can, of course, wire up the container yourself, as in your question, using the adapter, but it's generally better to let Spring manage it as a @Bean or you will miss some functionality (e.g. event publishing for failures, idle container). The adapter gets a reference to your request/reply listener and the method name to call.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值