设为首页 收藏本站
查看: 697|回复: 0

[经验分享] Spring MVC 接入 rabbitMQ

[复制链接]

尚未签到

发表于 2017-7-3 06:05:16 | 显示全部楼层 |阅读模式
依赖包



        <dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.6.8.RELEASE</version>
</dependency>
spring-xxx.xml 配置文件



    <bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="username" value="${mq.user}" />
<property name="password" value="${mq.pwd}" />
<property name="host" value="${mq.address}" />
<property name="port" value="${mq.port}" />
<property name="virtualHost" value="${mq.vhost}" />
</bean>
<bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<bean id="serializerMessageConverter"
class="org.springframework.amqp.support.converter.SimpleMessageConverter" />
<!-- 创建rabbitTemplate 消息模板类 -->
<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<constructor-arg ref="connectionFactory" />
<property name="exchange" value="${mq.exchange}" />
<property name="routingKey" value="${mq.routingKey}" />
<property name="queue" value="${mq.queue}" />
<property name="messageConverter" ref="serializerMessageConverter" />
</bean>
<bean id="queue" class="org.springframework.amqp.core.Queue">
<constructor-arg index="0" value="${mq.queue}"></constructor-arg>
<constructor-arg index="1" value="true"></constructor-arg>
<constructor-arg index="2" value="false"></constructor-arg>
<constructor-arg index="3" value="false"></constructor-arg>
</bean>
<!--
<bean id="directExchange" class="org.springframework.amqp.core.DirectExchange">
<constructor-arg index="0" value="${mq.routingKey}"></constructor-arg>
<constructor-arg index="1" value="true"></constructor-arg>
<constructor-arg index="2" value="false"></constructor-arg>
</bean>

<util:map id="arguments">
</util:map>

<bean id="binding" class="org.springframework.amqp.core.Binding">
<constructor-arg index="0" value="${mq.queue}"></constructor-arg>
<constructor-arg index="1" value="QUEUE"></constructor-arg>
<constructor-arg index="2" value="${mq.exchange}"></constructor-arg>
<constructor-arg index="3" value="${mq.routingKey}"></constructor-arg>
<constructor-arg index="4" value="#{arguments}"></constructor-arg>
</bean>

-->
<bean id="rmqProducer" class="com.xxxx.RmqProducer">
<property name="rabbitTemplate" ref="rabbitTemplate" />
</bean>
<bean id="rmqConsumer" class="com.xxxx.RmqConsumer" />
<bean id="messageListenerAdapter"
class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="rmqConsumer" />
<property name="defaultListenerMethod" value="rmqConsumeMessage"></property>
<property name="messageConverter" ref="serializerMessageConverter"></property>
</bean>
<bean id="listenerContainer"
class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="queues" ref="queue"></property>
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="messageListener" ref="messageListenerAdapter"></property>
</bean>
profile.xml 中相关配置



mq.address=xxxx
mq.exchange=xxxxx
mq.routingKey=xxx
mq.queue=xxx
mq.port=5672
mq.user=xxxx
mq.pwd=xxxx
mq.timeout=5000
mq.vhost=lms
相关类文件



public class RmqProducer {
private static final Logger LOGGER = LoggerFactory.getLogger(RmqConsumer.class);
private RabbitTemplate rabbitTemplate;
/**
* 发送信息
*/
public void sendMessage(RabbitMessage msg) {
try {
// 发送信息
            rabbitTemplate.convertAndSend(msg);
} catch (Exception e) {
LOGGER.error("rmq消费者任务处理出现异常", e);
}
}
public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
}


public class RmqConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(RmqConsumer.class);
public void rmqConsumeMessage(Object obj) {
LOGGER.info("rmq 消费者任务:{}", JSON.toJSONString(obj));
// TODO 具体的消费策略
    }
}
使用



    • 使用时只需要注入相应的bean即可使用
    • 如果有多个队列,注意以下Bean的定义


    • rmqProducer
    • rmqConsumer
    • messageListenerAdapter
    • LisetenerContainer


备注


  • 如果已经在rabbit的图形化界面bind相关的exchange 和 quene,橙色斜体加粗子部分可以省略;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-390573-1-1.html 上篇帖子: 菜鸟前端学算法 下篇帖子: 消息队列RabbitMQ与Spring集成
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表