• Spring MVC 接入 rabbitMQ


    依赖包

            <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的定义
      1. rmqProducer
      2. rmqConsumer
      3. messageListenerAdapter
      4. LisetenerContainer

    备注

    • 如果已经在rabbit的图形化界面bind相关的exchange 和 quene,橙色斜体加粗子部分可以省略;
  • 相关阅读:
    微信小程序保存图片功能实现
    小程序清除缓存功能如何实现
    小程序自定义函数—数字千位转换
    小程序身份证号检测函数
    小程序 的 textarea 组件 层级问题如何解决
    Markdown 语法背一下咯
    跨域了解一下?
    sort命令的k选项大讨论【转】
    Shell脚本中实现切换用户并执行命令操作【转】
    Ansible Tower系列 四(使用tower执行一个命令)【转】
  • 原文地址:https://www.cnblogs.com/shanguanghui/p/6559096.html
Copyright © 2020-2023  润新知