使用spring-amqp结合使用rabbitmq
2013-09-26 16:50
441 查看
maven 依赖包配置如下:
通过spring 获得到连接,并发送消息
context.xml 配置如下:
Foo 类 此类用于监听消息
<dependencies> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.2.0.RELEASE</version> </dependency> </dependencies>
通过spring 获得到连接,并发送消息
public static void main(final String... args) throws Exception { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml"); RabbitTemplate template = ctx.getBean(RabbitTemplate.class); template.convertAndSend("Hello, world!"); Thread.sleep(1000); ctx.destroy(); }
context.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" /> <!--路由设置 将队列绑定,属于topic类型--> <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" />
Foo 类 此类用于监听消息
public class Foo { public void listen(String foo) { System.out.println(foo); } }
相关文章推荐
- 使用spring-amqp结合使用rabbitmq
- Spring Boot 2.0.0.M7 使用异步消息服务-AMQP(RabbitMQ)
- RabbitMQ的Java应用(3) -- 使用spring-boot-starter-amqp开发生产者应用
- docker环境下的RabbitMQ部署,Spring AMQP使用
- rabbitmq学习10:使用spring-amqp发送消息及异步接收消息
- rabbitmq学习9:使用spring-amqp发送消息及同步接收消息
- RabbitMQ的Java应用(2) -- 使用Spring AMQP开发消费者应用
- RabbitMQ与spring结合之spring-rabbit使用(三)
- rabbitmq学习9:使用spring-amqp发送消息及同步接受消息
- docker环境下的RabbitMQ部署,Spring AMQP使用
- 使用Spring3.0的AOP结合log4j实现接口方法执行时间记录
- 译: 1. RabbitMQ Spring AMQP 之 Hello World
- umeditor上传功能后端与Spring结合,不使用官方jar
- RabbitMQ简介,安装,Spring集成 demo,以及使用(spring集成在最下面)
- 使用Spring和AMQP发送接收消息(上)
- Spring AMQP实例 以及Spring 配置文件动态注入属性使用实战
- blazeds与spring的结合使用
- Quartz与Spring结合使用及集群配置
- mvc和spring的结合使用
- Mybtis与Spring结合使用Log4J打印日志/Sql到控制台