解决某些定时任务中不能使用dao或者是service中对象值的问题

1.监听器类(监听器类需要在web.xml中注册)

package cn.com.topnetwork.rms.videodeploy.scan.TimeTask;

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

/**
 * @author wmx
 * @date 2021/5/8 15:36
 * @version: V1.0
 * <p>
 * <p>
 * (需求文档版本---    )
 */
//监听器类
public class TimerDataTaskListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        new TimerTask().collectData();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

注册代码:

<!-- TimerDataTaskListener 监听器 -->
	<listener>
		<listener-class>cn.com.topnetwork.rms.videodeploy.scan.TimeTask.TimerDataTaskListener</listener-class>
	</listener>

2.定时器代码

package cn.com.topnetwork.rms.videodeploy.scan.TimeTask;

import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author wmx
 * @date 2021/5/8 15:15
 * @version: V1.0
 * <p>
 * <p>
 * (需求文档版本---    )
 */
@Component
public class TimerTask {
    public void collectData(){
        long oneDay = 24 * 60 * 60 * 1000;
        long initDelay = getTimeMillis("00:00:00") - System.currentTimeMillis();
        long initDelay2 = getTimeMillis("06:00:00") - System.currentTimeMillis();
        long initDelay3 = getTimeMillis("17:23:00") - System.currentTimeMillis();
        long initDelay4 = getTimeMillis("18:00:00") - System.currentTimeMillis();
        if(initDelay < 0){
            if(initDelay2 > 0){
                initDelay =  initDelay2;
            }else{
                if(initDelay3 > 0){
                    initDelay =  initDelay3;
                }else{
                    if(initDelay4 > 0){
                        initDelay =  initDelay4;
                    }else{
                        initDelay = oneDay + initDelay;
                    }
                }
            }
        }
        // 创建任务队列
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);// 10 为线程数量
        // 执行任务
        scheduledExecutorService.scheduleAtFixedRate(() -> {
//            执行生成检测采集库
            new RunCode().run();
        }, initDelay, 6 * 60 * 60 * 1000, TimeUnit.MILLISECONDS); // 00:00:00 开始执行,每 6小时 执行一次
    }
    /**
     * 获取指定时间对应的毫秒数
     *
     * @param time "HH:mm:ss"
     * @return
     */
    private static long getTimeMillis(String time){
        try {
            DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
            Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
            return curDate.getTime();
        } catch (Exception e) {
            System.out.println("getTimeMillis catch exception.");
        }
        return 0;
    }
}

3.使用工具类激活spring对dao的管理

package cn.com.topnetwork.rms.videodeploy.scan.TimeTask;

/**
 * @author wmx
 * @date 2021/5/8 17:02
 * @version: V1.0
 * <p>
 * <p>
 * (需求文档版本---    )
 */

import cn.com.topnetwork.rms.videodeploy.scan.dao.ResourceScanDao;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @version : V1.0
 * @ClassName: FileUtils
 * @Description:
 * @Auther: Anakki
 * @Date: 2019/4/29 14:35
 */
@Component
public class FileUtils {

    //用于对数据库操作的Dao接口,博主用的是
    //Mybatis,由Mybatis调用接口返回数据库数据。
    @Resource
    public ResourceScanDao resourceScanDao;


    //静态化工具类变量
    public static FileUtils fileUtils;

    //注解用于告诉次代码在Spring加载之前就运行
    @PostConstruct
    public void init(){
        fileUtils = this;//工具类的实例赋值给fileUtils
        fileUtils.resourceScanDao=this.resourceScanDao;//会激活Spring对Dao的管理并赋给此类
        System.out.println("工具类已经初始化了,被纳入spring管理");
    }

}

4.执行的测试方法

package cn.com.topnetwork.rms.videodeploy.scan.TimeTask;


import org.springframework.stereotype.Component;

/**
 * @author wmx
 * @date 2021/5/8 15:42
 * @version: V1.0
 * <p>
 * <p>
 * (需求文档版本---    )
 */
@Component
public class RunCode {

    public void run(){
        FileUtils.fileUtils.init();
        System.out.println("这里是打印数据:"+FileUtils.fileUtils.resourceScanDao);
    }
}

主要的解决方式就是第三条内容,将dao重新纳入spring管理这样就可以调用工具类,以用于实现定时器中调用dao的目的

注解实现定时器并且调用dao的实现方式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值