Tomcat关闭RocketMQ消费者资源未释放,导致内存不足 SSH架构 解决方案

描述:在linux的tomcat部署的ssh项目集成了阿里rocketmq,每次重启tomcat发现阿里的控制台就会多一个mq消费者在线,这样一来除了广播模式,发布消息到消息消费都会受到影响,系统开辟的进程多了起来,还会导致tomcat无法关闭。

理想方案:理想化就是在tomcat关闭的时候自动释放mq消费者资源。

解决方案:由于是ssh项目,大多采用xml装配bean方式,废话不多说,直接上代码

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:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
       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/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- RocketMQ的生命周期 -->
    <bean id="rocketMqLifeCycle" class="com.jade.abc.mq.MqConsumerServices" init-method="init" destroy-method="destroy">
    </bean>
    
</beans>

java

package com.jade.abc.mq;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.*;
import com.kc.weixin.tool.Setting;
import com.kc.weixin.tool.TemplateMessage;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MqConsumerServices {

    private static Consumer consumer;

    static {
        // 微信消息消费者初始化
        try {
            String topic = MqRoule.TOPIC_WX_MSG;
            String tag = "TagA";
            consumer = ONSFactory.createConsumer(MqTemplate.properties);
            consumer.subscribe(topic, tag, new MessageListener() {
                @Override
                public Action consume(Message message, ConsumeContext context) {
                    String openid = null;
                    String data = null;
                    if (message != null) {
                        String msgBody = new String(message.getBody());
                        openid = msgBody.split("#@#")[0];
                        data = msgBody.split("#@#")[1];
                        JSONObject jsonObject = JSON.parseObject(data);
                        TemplateMessage.sendTemplateMessageWLSLSPTX(Setting.AccessToken, openid, jsonObject);
                    }
                    System.out.println("Receive: " + message);
                    System.out.println(new String(message.getBody()));
                    return Action.CommitMessage;
                }
            });
            consumer.start();

            System.out.println(String.format("----- %s-Service[GroupId:%s,Topic:%s,Tag:%s] MqConsumer Started at %tc ! -----", "VX-Msg", topic, Setting.groupId, tag, new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建方法,会被xml主动调用
     */
    public void init() {
       // System.out.println("this is init method3");
    }

    /**
     * 摧毁方法,会被xml主动调用
     */
    public void destroy() {
        try {
            consumer.shutdown();
            System.out.println(String.format("----- Ali-RocketMQ Consumers Shutdown Success at %tc !-----",new Date()));
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(String.format("----- [Warn] Ali-RocketMQ Consumers Shutdown Failed  !-----"));
            System.out.println(String.format("----- [Warn-Message] :%s -----",e.getMessage()));
            System.out.println("----- Please close ali-rocketmq  consumer-online services manually -----");
        }
    }

}

这样就可以完美解决,mq消费者线程无法关闭的问题。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

若光672

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值