spring 消息列队
来源:互联网 发布:小财神打水软件 编辑:程序博客网 时间:2024/05/21 22:41
spring-amqp结合使用rabbitmq
Spring AMQP提供了org.springframework.amqp.core.AmqpTemplate来发送与接收消息。AMQP模板实现支持发送与接收POJOs而非javax.jms.Message实例。他们还提供了一种方式来自定义用于编排对象的MessageConverter。Spring与JMS用户会发现JmsTemplate与新的AmqpTemplate之间的相似性。
下面的代码片段介绍了如何联合使用Spring AMQP与RabbitMQ处理同步消息。RabbitMQ是VMware的产品,并且是官方Spring AMQP示例中所用的默认AMQP实现。
Maven项目添加依赖包spring-rabbit
<dependencies> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.3.5.RELEASE</version> </dependency></dependencies>
just Java
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();}
Or, the Spring way
1.applicationContext-amqp.xml
<rabbit:connection-factory id="connectionFactory" /><rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="myExchange" routing-key="foo.bar"/><rabbit:admin connection-factory="connectionFactory" /><rabbit:queue name="myQueue" /><rabbit:topic-exchange name="myExchange"> <rabbit:bindings> <rabbit:binding queue="myQueue" pattern="foo.*" /> </rabbit:bindings></rabbit:topic-exchange><rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="foo" method="listen" queue-names="myQueue" /></rabbit:listener-container><bean id="foo" class="foo.Foo" />
2.Method Foo
public class Foo { public void listen(String foo) { System.out.println(foo); }}
3.Method Main
public static void main(final String... args) throws Exception { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-amqp.xml"); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); template.convertAndSend("Hello, world!"); Thread.sleep(1000); ctx.destroy();}
0 0
- spring 消息列队
- 消息列队
- 关于WINDOWS消息列队
- C 消息列队
- MQ 消息列队
- linux消息列队
- JavaScripe 中的 消息列队
- yii配置rabbitmq消息列队
- 消息列队机制(个人理解)
- yii配合rabbitmq邮件消息列队
- 大型网站架构之分布式消息列队
- 战马列队
- 数据结构--列队
- spring消息
- Spring 消息
- 列队顺逆报数
- MSMQ列队简介
- 列队在田野之中
- 超详细VMware Workstation 10安装OS X Mavericks
- Linux 下软件配置,编译与安装的基本过程
- OpenCV:卷积运算
- Maven 生命周期
- 5.两分钟让你明白app后端有啥用
- spring 消息列队
- Windows下Resin的配置与部署
- iOS ARC内存管理
- C# lable实时显示不断变化的值
- LeetCode36——Valid Sudoku
- 【Apache Samza 系列】实时流数据处理框架Samza中文教程 (三)-- 概念
- 大数据如何改变教育(转载)
- ie浏览器的问题
- android启动错误