spring-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: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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值