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 ...
随机推荐
- Ubuntu Server Swap 分区设置
方案一:仅在内存耗尽的情况下才使用 swap 分区 # 首先进入 sudo 模式 sysctl vm.swappiness=0 # 临时生效 echo "vm.swappiness = 0& ...
- 面试题目<转载>
1:检测一个变量是否有设置的函数是否?是否为空的函数是?(2分) 2:echo(),print(),print_r()的区别(3分) 3:表单中 get与post提交方法的 ...
- Kettle部署笔记
1.启动脚本(启动job) /u02/www/data-integration/kitchen.sh -file:/u02/www/data-integration/job.kjb -logfile= ...
- WPF GridView动态添加项并读取数据
假设数据库有如下表, 首先我们创建一个WPF工程,界面如下 <Window x:Class="WpfApplication2.MainWindow" xmlns=" ...
- js实现CheckBox全选或者不全选
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">< ...
- java第一次笔试+面试总结
今天是自己第一次java笔试和面试,总体感觉比预期好一点. 笔试题第一面是问答题,主要考查java基础,一共有18题,我有6道题没有写出来.第二面主要是算法题,一共有8道题,我大概写出来4道题,第三面 ...
- Pytorch 1.0升级到Pytorch 1.1.0
Pytorch 1.0Pytorch 1.0于2018-12-8发布,详见https://github.com/pytorch/pytorch/releases/tag/v1.0.0 主要更新JIT全 ...
- 【转载】C#使用Except方法求取两个List集合的差集数据
在C#语言的编程开发中,针对List集合的运算有时候需要计算两个List集合的差集数据,集合的差集是取在该集合中而不在另一集合中的所有的项.A集合针对B集合的差集数据指的是所有在A集合但不在B集合的元 ...
- DataGrip像navicat一样导入导出表数据,不是导出导入insert和update这种
用的是mysql,其他也一样 首先是导出: 然后: 然后就可以导出了,导出去别的工具能不能拿来导入不知道... 然后是导入: 然后:
- MNIST手写数据集在运行中出现问题解决方案
今天在运行手写数据集的过程中,出现一个问题,代码没有问题,但是运行的时候一直报错,错误如下: urllib.error.URLError: <urlopen error [SSL: CERTIF ...