(一)quartz
java 中的方法,我的意图是在每月的月末执行
package com.modules.kaoqin.workyears.service;
import java.util.Date;
import java.util.List;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;
import com.common.utils.DateUtils;
import com.modules.kaoqin.workyears.dao.TscUserYearDao;
import com.modules.kaoqin.workyears.entity.TscUserYear;
/**
* 轮询时间设置工龄信息
*/
@Service
@Lazy(false)
public class WorkYearsTimerTask extends QuartzJobBean{
private static long DATETIME = 1000 * 60 * 60 * 24 ;
private static Logger logger = LoggerFactory.getLogger(WorkYearsTimerTask.class);
@Autowired
private TscUserYearDao userYearsDao;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//可以获取到xml中定义的值
JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
}
// @Scheduled(cron = "1 10 6 28-31 * ?") //每月的最后一日的6点十分执行
public void run(){
List<TscUserYear> list= userYearsDao.findList(new TscUserYear());
Date currDate = new Date();
for (TscUserYear userYear : list) {
Date enterDate = userYear.getEnterCompnyDate();
long workTime = DateUtils.dateToLong(currDate) - DateUtils.dateToLong(enterDate);
userYear.setWorkYears(workYear(workTime));
userYearsDao.update(userYear);
logger.debug(userYear.getUser().getName() + "已经工作了" + workYear(workTime) + "年了,数据更新成功!");
}
}
private int workYear(long workTime){
long date = workTime/(DATETIME);
int work = (int) (date / 365);
return work;
}
@Scheduled(fixedDelay = 5000)
public void testTask() {
// run();
// logger.info("测试定时任务");
}
public static void main(String[] args) {
WorkYearsTimerTask w = new WorkYearsTimerTask();
// System.out.println(w.workYear(739 * DATETIME));
w.testTask();
}
}
XML中的配置。执行run 方法
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
default-lazy-init="true">
<!-- job的定义 -->
<!-- 要执行任务的任务类 -->
<bean id="workYears" class="com.thinkgem.jeesite.modules.kaoqin.workyears.service.WorkYearsTimerTask"></bean>
<!-- 将需要执行的定时任务注入JOB中 -->
<bean name="myJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="workYears"></property>
<!-- 任务类中需要执行的方法 -->
<property name="targetMethod" value="run"></property>
<!-- 是否并发执行 -->
<property name="concurrent" value="false"></property>
</bean>
<!-- SimpleTrigger 触发器 -->
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="myJob" />
<property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>
<!-- CronTriggerBean 触发器 -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="myJob" />
<!-- 每3秒执行一次 -->
<!-- <property name="cronExpression" value="0/3 * * * * ?" /> -->
<property name="cronExpression" value="1 10 6 L * ?" />
</bean>
<!-- 配置调度工厂 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- <ref bean="simpleTrigger" /> -->
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
https://blog.csdn.net/zbw18297786698/article/details/73469505