使用spring-amqp发送消息及同步接收消息
来源:互联网 发布:es7 js 注解功能 编辑:程序博客网 时间:2024/05/17 03:30
通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的。有兴趣的朋友 可以去spring-amqp官网下载例子。
先来看看HelloWorldConfiguration类
- package org.springframework.amqp.helloworld;
- import org.springframework.amqp.core.Queue;
- import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;
- import org.springframework.amqp.rabbit.connection.ConnectionFactory;
- import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class HelloWorldConfiguration extends AbstractRabbitConfiguration {
- protected final String helloWorldQueueName = "hello.world.queue";
- @Bean
- public ConnectionFactory connectionFactory() {
- SingleConnectionFactory connectionFactory = new SingleConnectionFactory(
- "localhost");
- connectionFactory.setUsername("guest");
- connectionFactory.setPassword("guest");
- return connectionFactory;
- }
- @Override
- public RabbitTemplate rabbitTemplate() {
- RabbitTemplate template = new RabbitTemplate(connectionFactory());
- // The routing key is set to the name of the queue by the broker for the
- // default exchange.
- template.setRoutingKey(this.helloWorldQueueName);
- // // Where we will synchronously receive messages from
- template.setQueue(this.helloWorldQueueName);
- return template;
- }
- @Bean
- public Queue helloWorldQueue() {
- return new Queue(this.helloWorldQueueName);
- }
- }
package org.springframework.amqp.helloworld;import org.springframework.amqp.core.Queue;import org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration;import org.springframework.amqp.rabbit.connection.ConnectionFactory;import org.springframework.amqp.rabbit.connection.SingleConnectionFactory;import org.springframework.amqp.rabbit.core.RabbitTemplate;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class HelloWorldConfiguration extends AbstractRabbitConfiguration {protected final String helloWorldQueueName = "hello.world.queue";@Beanpublic ConnectionFactory connectionFactory() {SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");connectionFactory.setUsername("guest");connectionFactory.setPassword("guest");return connectionFactory;}@Overridepublic RabbitTemplate rabbitTemplate() {RabbitTemplate template = new RabbitTemplate(connectionFactory());// The routing key is set to the name of the queue by the broker for the// default exchange.template.setRoutingKey(this.helloWorldQueueName);// // Where we will synchronously receive messages fromtemplate.setQueue(this.helloWorldQueueName);return template;}@Beanpublic Queue helloWorldQueue() {return new Queue(this.helloWorldQueueName);}}
此类定义了ConnectionFactory 、RabbitTemplate 、Queue
发送消息的程序如下:
- package org.springframework.amqp.helloworld;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Producer {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
- AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
- amqpTemplate.convertAndSend("Hello World");
- System.out.println("Sent: Hello World");
- }
- }
package org.springframework.amqp.helloworld;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Producer {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);amqpTemplate.convertAndSend("Hello World");System.out.println("Sent: Hello World");}}
同步接收消息如下:
- package org.springframework.amqp.helloworld;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Consumer {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);
- AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
- System.out.println("Received: " + amqpTemplate.receiveAndConvert());
- }
- }
package org.springframework.amqp.helloworld;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Consumer {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfiguration.class);AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);System.out.println("Received: " + amqpTemplate.receiveAndConvert());}}
这个例子是Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。
对于 HelloWorldConfiguration类的配置,也可以通过SPRING XML文件来配置。例如
rabbitConfiguration.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 创建connectionFactory -->
- <bean id="connectionFactory"
- class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
- <constructor-arg value="localhost" />
- <property name="username" value="guest" />
- <property name="password" value="guest" />
- </bean>
- <bean id="rabbitAdmin"
- class="org.springframework.amqp.rabbit.core.RabbitAdmin">
- <constructor-arg ref="connectionFactory" />
- </bean>
- <bean id="rabbitTemplate"
- class="org.springframework.amqp.rabbit.core.RabbitTemplate">
- <constructor-arg ref="connectionFactory"></constructor-arg>
- <property name="queue" value="hello.world.queue"></property>
- <property name="routingKey" value="hello.world.queue"></property>
- </bean>
- <!-- 声明Queue并设定Queue的名称 -->
- <bean id="helloWorldQueue"
- class="org.springframework.amqp.core.Queue">
- <constructor-arg value="hello.world.queue"></constructor-arg>
- </bean>
- </beans>
参考:http://wubin850219.iteye.com/blog/1050328
0 0
- 使用spring-amqp发送消息及同步接收消息
- 使用spring-amqp发送消息及异步接收消息
- rabbitmq学习9:使用spring-amqp发送消息及同步接收消息
- rabbitmq学习10:使用spring-amqp发送消息及异步接收消息
- 【Spring】使用Spring和AMQP发送接收消息(上)
- 【Spring】使用Spring和AMQP发送接收消息(中)
- 【Spring】使用Spring和AMQP发送接收消息(上)
- 【Spring】使用Spring和AMQP发送接收消息(下)
- rabbitmq学习9:使用spring-amqp发送消息及同步接受消息
- 使用Spring和AMQP发送接收消息(上)
- 使用Spring和AMQP发送接收消息(中)
- Golang使用amqp发送消息
- Spring AMQP 实现客户端发送消息
- spring消息之使用jms发送和接收消息
- 使用ActiveMQ 发送/接收消息
- ActiveMQ 整合 Spring 发送和接收消息
- ActiveMQ 部署及发送接收消息
- rabbitmq一---一对一发送及接收消息
- COM技术内幕 读书笔记——第二章 接 口
- 堆和栈的区别(转过无数次的文章)
- 同步工具类--闭锁
- 网络编程UDP协议——聊天软件
- c++ 多线程 互斥量
- 使用spring-amqp发送消息及同步接收消息
- mfc获取窗口句柄
- 排队问题
- 【金阳光测试】Android自动化测试第一季第二讲--MonkeyRunner
- 状态机在RPG游戏中的应用举例
- JAVA基础加强:枚举(Enum)
- openfire导入eclipse之二Could not locate home 编译运行
- 使用spring-amqp发送消息及异步接收消息
- HDU 3265——Posters(线段树+面积并+矩形分割)