Spring AMQPAMQP 消息解决方案
Spring AMQP 是基于 Spring 框架的 AMQP 消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO。同时有 Java 和 .NET 的版本。
示例代码:
public static void main(final String... args) throws Exception { ConnectionFactory cf = new CachingConnectionFactory(); // set up the queue, exchange, binding on the broker RabbitAdmin admin = new RabbitAdmin(cf); Queue queue = new Queue("myQueue"); admin.declareQueue(queue); TopicExchange exchange = new TopicExchange("myExchange"); admin.declareExchange(exchange); admin.declareBinding( BindingBuilder.bind(queue).to(exchange).with("foo.*")); // set up the listener and container SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf); Object listener = new Object() { public void handleMessage(String foo) { System.out.println(foo); } }; MessageListenerAdapter adapter = new MessageListenerAdapter(listener); container.setMessageListener(adapter); container.setQueueNames("myQueue"); container.start(); // send something RabbitTemplate template = new RabbitTemplate(cf); template.convertAndSend("myExchange", "foo.bar", "Hello, world!"); Thread.sleep(1000); container.stop(); }
评论