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的更多相关文章

  1. spring amqp rabbitmq fanout配置

    基于spring amqp rabbitmq fanout配置如下: 发布端 <rabbit:connection-factory id="rabbitConnectionFactor ...

  2. 译: 1. RabbitMQ Spring AMQP 之 Hello World

    本文是译文,原文请访问:http://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html RabbitMQ 是一个Brocker (消息队 ...

  3. 消息中间件——RabbitMQ(九)RabbitMQ整合Spring AMQP实战!(全)

    前言 1. AMQP 核心组件 RabbitAdmin SpringAMQP声明 RabbitTemplate SimpleMessageListenerContainer MessageListen ...

  4. RabbitMQ与Spring的框架整合之Spring AMQP实战

    1.SpringAMQP用户管理组件RabbitAdmin. RabbitAdmin类可以很好的操作RabbitMQ,在Spring中直接进行注入即可.注意,autoStartup必须设置为true, ...

  5. RabbitMQ 的消息持久化与 Spring AMQP 的实现剖析

    文章目录 1. 原生的实现方式 2. Spring AMQP 的实现方式   要从奔溃的 RabbitMQ 中恢复的消息,我们需要做消息持久化.如果消息要从 RabbitMQ 奔溃中恢复,那么必须满足 ...

  6. Spring AMQP 发送消息到 RabbitMQ 收到 x-queue-type 错误

    在使用 Spring AMQP 发送消息到 RabbitMQ 的时候收到错误信息: inequivalent arg 'x-queue-type' for queue 'com.ossez.real. ...

  7. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  8. 五.Spring与RabbitMQ集成--HelloWorld

    spring对RabbitMQ做了很好的集成,我们称之为spring AMQP,其官方文档写得十分详尽,文档地址:https://docs.spring.io/spring-amqp/referenc ...

  9. 六.Spring与RabbitMQ集成--stock trading(股票交易系统)

    周末继续写博客,算起来,关于rabbitMQ这个中间件的研究已经持续至两个星期了,上一篇文章使用sring amqp实现了同步和异步的消息接收功能.这一节继续实用spring amqp实现一个股票交易 ...

  10. rabbitMQ教程(三) spring整合rabbitMQ代码实例

    一.开启rabbitMQ服务,导入MQ jar包和gson jar包(MQ默认的是jackson,但是效率不如Gson,所以我们用gson) 二.发送端配置,在spring配置文件中配置 <?x ...

随机推荐

  1. Python+VSCode+Git【转】

    Python+VSCode+Git 学习总结 - 秦无邪 - 博客园

  2. [LOJ 6485]LJJ学二项式定理(单位根反演)

    也许更好的阅读体验 \(\mathcal{Description}\) 原题链接 \(T\)组询问,每次给\(n,s,a_0,a_1,a_2,a_3\)求 \(\begin{aligned}\left ...

  3. Python Django mysqlclient安装和使用

    一.安装mysqlclient 网上看到很过通过命令:pip install mysqlclient 进行安装的教程,但是我却始终安装失败,遇到的错误千奇百怪,后来通过自己下载mysqlclient客 ...

  4. jquery easyui form表单一开始就自动启用验证了,修改为form提交的时候在开启验证

    <form method="post" action="<%=path %>" class="easyui-form" d ...

  5. win7实现tensorflow中的物体识别

    实现条件: 1.win7 2.python 3.运行所需要的库:matplotlib.lxml.pillow.Cython 具体参考:https://github.com/tensorflow/mod ...

  6. POJ1861(Network)-Kruskal

    题目在这 Sample Input 4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1 Sample Output 1 4 1 2 1 3 2 3 3 4 题目意思:4个点 ...

  7. Java软件编码习惯

    1.再删除某个类时候,一定别忘记把对应的import也删除掉: 可以手动删除,也可以 Ctrl+Shift+O快捷键自动删除和导入.

  8. zabbix server for Centos 6.3

    1.安装LNMP 参照http://lnmp.org/install.html 2.安装zabbix service 2.1下载zabbix,并解压 wget http://nchc.dl.sourc ...

  9. Window Server 2008 概述

    Windows历史版本 Windows工作站发展历史 Windows95——>Windows98——>Windows2000——>WindowsXP——>Vista——> ...

  10. python返回值的缺省设置

    有时候并不需要返回所有的值,但是原始函数的return语句中又有较多参数时: 方法一:修改原始返回值,只返回需要的参数 方法二:如果原始函数时第三方库或者python自带库,则直接修改可能不太好,于是 ...