Spring+RabbitMQ入门(annotation方式)

1、准备

----|jar:spring3.1下所有、aopalliance、commons.logging、spring.retry、spring.rabibt、spring.amqp、rabbitmq.client、aspectj。

----|编译工具:eclipse

----|rabbitMQ:rabbitMQ3.6.1

----|erlang

2、搭建

----|工程:java工程

--------|实体包:com.wind.main

------------|消息发送方:Producer.java:

package com.wind.main;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producer {
@Autowired
RabbitTemplate rabbitTemplate;
public void send(String msg){
rabbitTemplate.convertAndSend(msg);
}
}

------------|消息接收方:QueueListener.java:

package com.wind.main;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

@Component
public class QueueListener implements MessageListener{

@Override
public void onMessage(Message msg) {
System.out.println(msg);
}
}

--------|测试包:com.wind.test

------------|测试类TestRabbit.java:

package com.wind.test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.wind.main.Producer;

public class TestRabbit {
@Test
public void fun1() throws InterruptedException {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:applicationContext-rabbitmq.xml");
ctx.start();
Producer producer = (Producer) ctx.getBean(Producer.class);
producer.send("this is the test msg");
Thread.sleep(1000);
ctx.destroy();
}
}

--------|配置文件:applicationContex-rabbitmq.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">


<!-- 自动扫描指定包下的所有类上的@Component、@Controller等注解 -->
<context:component-scan base-package="com.wind.main" />
<!-- 引入外部propperties文件 -->
<context:property-placeholder location="classpath:rabbit.properties" />
<!-- rabbit的连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbit.host}" port="${rabbit.port}" username="${rabbit.username}"
password="${rabbit.password}" />


<rabbit:admin connection-factory="connectionFactory" />
<!-- rabbit模板对象,在这里绑定交换机和连接工厂 -->
<rabbit:template id="rabbitTemplate"
connection-factory="connectionFactory" exchange="my_queue" />
<!-- rabbit队列 -->
<rabbit:queue name="test_queue" />
<!-- rabbit的交换机,在这里绑定队列 -->
<rabbit:topic-exchange name="my_queue">
<rabbit:bindings>
<rabbit:binding queue="test_queue" pattern="#" />
</rabbit:bindings>
</rabbit:topic-exchange>
<!-- rabbit的监听容器,在这里绑定要监听的队列和用来监听的Bean -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener queues="test_queue" ref="queueListener" />
</rabbit:listener-container>


</beans>

--------|资源文件:rabbit.properties:

rabbit.username=guest
rabbit.password=guest
rabbit.port=5672
rabbit.host=localhost

3、测试

----|运行TestRabbit.java:

出现如下结果说明测试成功:

(Body:'this is the test msg' MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, appId=null, clusterId=null, type=null, correlationId=null, replyTo=null, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, deliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=my_queue, receivedRoutingKey=, deliveryTag=1, messageCount=0])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值