rabbitmq(七)与spring整合快速入门
2015-05-10 22:31
531 查看
添加依赖
<dependencies> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.4.5.RELEASE</version> </dependency> </dependencies>
使用普通方式
import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; public class Plain { private static final String QUEUE_NAME = "spring_queue_0"; public static void main(String[] args) { ConnectionFactory connectionFactory = new CachingConnectionFactory("127.0.0.1"); AmqpAdmin admin = new RabbitAdmin(connectionFactory); admin.declareQueue(new Queue(QUEUE_NAME)); AmqpTemplate template = new RabbitTemplate(connectionFactory); //发送消息 template.convertAndSend(QUEUE_NAME, "hello world"); //接收消息 String message = (String) template.receiveAndConvert(QUEUE_NAME); System.out.println(message); } }
使用spring方式
代码:import org.springframework.amqp.core.AmqpTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; public class SpringWay { private static final String QUEUE_NAME = "spring_queue_1"; public static void main(String[] args) { ApplicationContext context = new GenericXmlApplicationContext("classpath:/spring/rabbit-context.xml"); AmqpTemplate template = context.getBean(AmqpTemplate.class); //发布消息 template.convertAndSend(QUEUE_NAME, "hello word"); //接收消息 String message = (String) template.receiveAndConvert(QUEUE_NAME); System.out.println(message); } }
配置(rabbit-context.xml):
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <rabbit:connection-factory id="connectionFactory" host="127.0.0.1"/> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"/> <rabbit:admin connection-factory="connectionFactory"/> <rabbit:queue name="spring_queue_1"/> </beans>
参考资料:spring-amqp快速入门
相关文章推荐
- spring-data-jpa快速入门(一)——整合阿里Druid
- rabbitMq入门,整合spring
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
- Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus
- 快速搭建springboot框架以及整合ssm+shiro+安装Rabbitmq和Erlang、Mysql下载与配置
- RabbitMQ入门-10-1(整合spring-发送同步消息)
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
- RabbitMQ入门-10(整合spring-发送异步消息)
- RabbitMQ入门-10-3(整合spring-发送异步消息注解实现)
- SpringBoot入门-快速整合Mybatis
- WebService之Axis2快速入门: Spring与axis整合发布为WebService
- webservice快速入门-使用spring整合apache-cxf(五)
- SSM(Spring+SpringMVC+MyBatis)框架快速整合入门教程
- Shiro第四篇【Shiro与Spring整合、快速入门、Shiro过滤器、登陆认证】
- webservice快速入门-使用spring整合apache-cxf(五)
- RabbitMQ入门-10-2(整合spring-发送同步消息注解实现)
- 第一章 Spring快速入门
- SpringBoot初识和快速入门(一)
- Spring Cloud 快速入门esclipse快速搭建微服务框架 (一)-注册与发现
- MyBatis入门程序之整合Spring