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的目的