<?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: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/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.2.xsd">
<!-- 头都不一样 -->
<!-- 连接rabbitmq服务器 5672才是通讯端口 15672是网页的 -->
<rabbit:connection-factory id="conn" host="192.168.11.250" port="5672" username="admin" password="123456"/>
<!-- 创建队列(声明) 对列是放消息的容器 好多属性,是否自动删除,持久化,名字-->
<rabbit:queue name="miaosha"></rabbit:queue>
<!-- name="miaosha" miaosha 是什么? -->
<!-- 获取权限:否则队列无法创建 conn是连接rabbitmq服务器时的连接工厂-->
<rabbit:admin connection-factory="conn"/>
<!-- 模板:用于发消息(用于发消息!) 属性:id(必须) connection-factory。
service 类里面(发消息) RabbitTemplate rabbitTemplate;
rabbitTemplate.convertAndSend("miaosha(发给哪一个对列)",username+":iphone"(对列的内容)); -->
<rabbit:template id="rabbitTemplate" connection-factory="conn"></rabbit:template>
<!-- 收消息 -->
<bean id="miaoshaListener" class="com.mq.MiaoshaListen"></bean>
<!-- 注册监听器 -->
<bean class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queueNames" value="miaosha"></property>
<property name="messageListener" ref="miaoshaListener"></property>
<property name="connectionFactory" ref="conn"></property>
</bean>
</beans>
在applicationContext.xml中只需要
<import resource="spring-rabbitmq.xml"/><!-- 导入spring-rabbitmq.xml配置文件 -->
在service层,就相当于有一个类,是通过spring-rabbitmq.xml中 <rabbit:template id=“rabbitTemplate” connection-factory=“conn”></rabbit:template>实例化的对象
RedisTemplate redisTemplate;