tomcat项目-多线程定时任务

1.web.xml注册监听器

<listener>
 <listener-class>com.fastrise.fastdev.business.baseinfo.time.MultiLyzListener</listener-class>
</listener>

2.编写公用定时器调用类

备注:通过java反射机制,传入定时器实现类类类型,构造函数参数(如果为空,则不需要传),公共类自动初始化任务实现类

package com.fastrise.fastdev.business.baseinfo.time;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 多线公用程定时器
 *
 * @author lirong
 * @date 2022-12-21
 */
public class MultiThreadTimerManager extends Thread {
    private Object[] arg;
    private Class clazz;
    private long interval;

    /**
     * @param clazz    定时任务业务处理类
     * @param interval 任务每次执行间隔时间
     * @param arg      任务参数(按clazz类属性定义顺序传参,如clazz类属性多余传参个数,后面不赋值)
     */
    public MultiThreadTimerManager(Class clazz, long interval, Object... arg) {
        this.clazz = clazz;
        this.arg = arg;
        this.interval = interval;

        this.start();
    }

    @Override
    public void run() {

        /**
         * 执行时间从下一分钟内开始扫描
         */
        Date date = new Date(System.currentTimeMillis() + 1000 * 60);

        Object task = null;
        try {
            task = this.clazz.newInstance();
            Field[] fields = this.clazz.getDeclaredFields();
            for (int i = 0; null != arg && i < fields.length; i++) {
                Field field = fields[i];
                field.setAccessible(true);
                if (i < arg.length) {
                    field.set(task, arg[i]);
                }
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        new Timer().schedule((TimerTask) task, date, interval);
    }
}

3.编写具体定时任务实现类(实现TimerTask类)

package com.fastrise.fastdev.business.baseinfo.time;

import cn.hutool.core.date.DateUtil;
import com.fastrise.fastdev.business.baseinfo.pdjnum.model.WorkAreaWeight;
import com.fastrise.fastdev.business.baseinfo.pdjnum.service.PdjnumService;
import com.fastrise.fastdev.common.utils.ResultMsg;
import com.fastrise.fastdev.common.wx.WxMpProperties;
import com.fastrise.fastdev.common.wx.common.model.MsgParamModel;
import com.fastrise.fastdev.common.wx.common.service.WxMsgService;
import com.fastrise.fastdev.system.model.WxMessageLog;
import com.fastrise.fastdev.system.service.WxMessageLogService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 船舶每日动态-微信定时推送
 *
 * @author lirong
 * @date 2022/11/10
 */
@Component
public class WorkAreasDynPushTask extends TimerTask {
    private PdjnumService pdjnumService;
    private WxMsgService msgService;
    private WxMessageLogService logService;

    public WorkAreasDynPushTask(PdjnumService pdjnumService, WxMsgService msgService, WxMessageLogService logService) {
        super();
        this.pdjnumService = pdjnumService;
        this.msgService = msgService;
        this.logService = logService;
    }

    private static final Logger logger = Logger.getLogger(WorkAreasDynPushTask.class);

    @Override
    public void run() {
        try {
           //具体业务实现逻辑
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("+++++++++++++WorkAreasDynPushTask++++++++++++++++" + e.getMessage());
        }
    }



}

4.建立监听类,并实现ServletContextListener接口,启动tomcat时初始化、启动定时任务
 

package com.fastrise.fastdev.business.baseinfo.time;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 多线程定时器
 *
 * @author lirong
 * @date 2022-12-21
 */
public class MultiLyzListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //获得Spring容器
//        WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
        //从Spring容器中获得weightEquipService的实例
//        final PdjnumService pdjnumService = ctx.getBean(PdjnumService.class);
//        final WxMsgService msgService = ctx.getBean(WxMsgService.class);
//        final WxMessageLogService logService = ctx.getBean(WxMessageLogService.class);

        //新建一个定时管理器
//        final long oneMinute = 60 * 1000;
//        Thread thread1 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                new MultiThreadTimerManager(FileTask.class, oneMinute);
//            }
//        });
//
//        Thread thread2 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                new MultiThreadTimerManager(WorkAreasDynPushTask.class, oneMinute, pdjnumService, msgService, logService);
//            }
//        });

//        thread1.start();
//        thread2.start();

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值