spring-AMQP-RabbitMQ
1.spring整合rabbitMQ配置文件 rabbitmq-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-1.4.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 定义RabbitMQ的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="127.0.0.1" port="5672" username="xuejianjun" password="xuejianjun"
virtual-host="/xuejianjun" />
<!-- 定义Rabbit模板,指定连接工厂以及定义exchange -->
<!-- 定义模板,将消息发送到交换机 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="fanoutExchange" />
<!-- 将消息发送到队列,需要做出如下修改: -->
<!-- <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" queue="对列名" /> -->
<!-- <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="fanoutExchange" routing-key="foo.bar" /> -->
<!-- MQ的管理,包括队列、交换器等 -->
<!--配置管理的目的,就是为了自动声明下列的队列,交换器等的 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 定义队列,自动声明 -->
<rabbit:queue name="myQueue" auto-declare="true"/>
<!-- 定义交换器,自动声明, -->
<!-- 交换机不存在就自动声明,存在就不声明 -->
<rabbit:fanout-exchange name="fanoutExchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="myQueue"/>
</rabbit:bindings>
</rabbit:fanout-exchange>
<!-- <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="cn.itcast.rabbitmq.spring.Foo" />
</beans>
2.测试类

SpringMain.java
================================================================================================================================
package cn.itcast.rabbitmq.spring;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(final String... args) throws Exception {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:spring/rabbitmq-context.xml");
//RabbitMQ模板
RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
//发送消息
template.convertAndSend("Hello, world!");
Thread.sleep(1000);// 休眠1秒
ctx.destroy(); //容器销毁
}
}
=============================================================================================================================
Foo.java 注意:Foo的含义,就是距离的意思,例如中文中有:张三,李四等
package cn.itcast.rabbitmq.spring;
/**
* 消费者
* @author zhijun
*
*/
public class Foo {
//具体执行业务的方法
public void listen(String foo) {
System.out.println("消费者: " + foo);
}
}
spring-AMQP-RabbitMQ的更多相关文章
- spring amqp rabbitmq fanout配置
基于spring amqp rabbitmq fanout配置如下: 发布端 <rabbit:connection-factory id="rabbitConnectionFactor ...
- 译: 1. RabbitMQ Spring AMQP 之 Hello World
本文是译文,原文请访问:http://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html RabbitMQ 是一个Brocker (消息队 ...
- 消息中间件——RabbitMQ(九)RabbitMQ整合Spring AMQP实战!(全)
前言 1. AMQP 核心组件 RabbitAdmin SpringAMQP声明 RabbitTemplate SimpleMessageListenerContainer MessageListen ...
- RabbitMQ与Spring的框架整合之Spring AMQP实战
1.SpringAMQP用户管理组件RabbitAdmin. RabbitAdmin类可以很好的操作RabbitMQ,在Spring中直接进行注入即可.注意,autoStartup必须设置为true, ...
- RabbitMQ 的消息持久化与 Spring AMQP 的实现剖析
文章目录 1. 原生的实现方式 2. Spring AMQP 的实现方式 要从奔溃的 RabbitMQ 中恢复的消息,我们需要做消息持久化.如果消息要从 RabbitMQ 奔溃中恢复,那么必须满足 ...
- Spring AMQP 发送消息到 RabbitMQ 收到 x-queue-type 错误
在使用 Spring AMQP 发送消息到 RabbitMQ 的时候收到错误信息: inequivalent arg 'x-queue-type' for queue 'com.ossez.real. ...
- rabbitMQ第五篇:Spring集成RabbitMQ
前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...
- 五.Spring与RabbitMQ集成--HelloWorld
spring对RabbitMQ做了很好的集成,我们称之为spring AMQP,其官方文档写得十分详尽,文档地址:https://docs.spring.io/spring-amqp/referenc ...
- 六.Spring与RabbitMQ集成--stock trading(股票交易系统)
周末继续写博客,算起来,关于rabbitMQ这个中间件的研究已经持续至两个星期了,上一篇文章使用sring amqp实现了同步和异步的消息接收功能.这一节继续实用spring amqp实现一个股票交易 ...
- rabbitMQ教程(三) spring整合rabbitMQ代码实例
一.开启rabbitMQ服务,导入MQ jar包和gson jar包(MQ默认的是jackson,但是效率不如Gson,所以我们用gson) 二.发送端配置,在spring配置文件中配置 <?x ...
随机推荐
- SAS学习笔记54 RTF文件格式
Style RTF Control Word Example Code Italicize \i title '\i italicized title'; Underline \ul title '\ ...
- Mybatis整合(Redis、Ehcache)实现二级缓存
目的: Mybatis整合Ehcache实现二级缓存 Mybatis整合Redis实现二级缓存 Mybatis整合ehcache实现二级缓存 ssm中整合ehcache 在POM中导入相关依赖 < ...
- 禁止服务向 eureka 上注册配置文件
### 禁止向注册中心注册服务eureka.client.register-with-eureka=false
- Asp.Net Mvc Area二级域名
参考:https://blog.maartenballiauw.be/post/2009/05/20/aspnet-mvc-domain-routing.html 参考:https://www.cnb ...
- C#-Parallel
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Li ...
- element 右键菜单
右键菜单 这是一个右键菜单的组件 <template> <div id="dropMenu" :style="style" style=&qu ...
- redis-desktop-manager 0.9.3 安装(最后一个免费版本)
使用Chocolatey(Windows包管理工具)安装 官方安装说明 https://chocolatey.org/install 安装redis-desktop-manager 官方安装说明 ht ...
- Authorization Bypass in RSA NetWitness
https://www.cnblogs.com/iAmSoScArEd/ SEC Consult Vulnerability Lab Security Advisory < 20190515-0 ...
- 由于找不到MSVCP140.dll,无法继续执行代码。重新安装程序可能会解决此问题。
msvcp140.dll文件下载,解决找不到msvcp140.dll的问题: 如果您的系统是64位的请将32位的dll文件复制到C:\Windows\System32目录 如果您的系统是64位的请将3 ...
- 【Java】接口和抽象类总结
一.接口 1.1 成员变量(其实是常量) 1.2 方法 二.抽象类 2.1 成员变量 2.2 方法 一.接口 1.1 成员变量(其实是常量) 成员变量默认用 public static final修饰 ...