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);
}
}

猜你喜欢

转载自www.cnblogs.com/curedfisher/p/11896773.html