写在前面的话:在工作中遇见了几次这个需要定时进行特定操作的需求,就做个记录方便参考。本文仅作参考。
SpringBoot
导入包
<!-- spring boot - quartz -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
启动类添加扫描注释
@EnableScheduling
job代码
/**
* <b>创建人:</b>surfingCat<br>
* <b>类描述:</b><br>
* <b>创建时间:</b>2021/3/01 16:10<br>
*/
@Component
public class PunchCardJob implements SchedulingConfigurer, Runnable, Trigger {
private final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private PunchCardJobService punchCardJobService;
// 读取配置
@Value("${job.interval}")
public String cronString;
@Override
public void run() {
try{
// 执行的任务逻辑
punchCardJobService.upDatePunchCardTaskStatus();
}catch (Exception e){
log.error("定时job任务时出错",e);
}
}
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
log.debug("定时job任务CronTrigger设定={}", cronString);
return new CronTrigger(cronString).nextExecutionTime(triggerContext);
}
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(this, this);
}
}
附Spring配置
导入包
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
spring-mvc配置
<!-- job配置 -->
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
job(多实例部署时提供一个任务抢占模式)
/**
* <b>创建人:</b>surfingCat<br>
* <b>类描述:</b>Job<br>
* <b>创建时间:</b>2020-03-30 15:24<br>
*/
@Service("com.web.job.DeleteLogsJob")
@DisallowConcurrentExecution
public class DeleteLogsJob implements SchedulingConfigurer, Runnable, Trigger {
private static final String DELETE_JOB_KEY = "asjcxvywjehq81etjs!@#e7dae1k2i(*&23qkj9";
private final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate<String, Object> jedisTemplate;
@Resource
private SysFlatDictBmo sysFlatDictBmo;
@Resource
private DeleteLogsJobBmoImpl deleteLogsJobBmo;
@Override
public void run() {
boolean monopolyMode = false;
try {
// 抢占模式,并标记为true=已独占
Boolean b = (Boolean) jedisTemplate.opsForValue().getAndSet(DELETE_JOB_KEY, true);
monopolyMode = b != null && b.booleanValue();
} catch (Exception ignore) {
// ignore
}
// 若原值为true,则抢占失败,表示已被其他人独占
if (monopolyMode) {
log.debug("已被其他任务独占");
return;
}
try {
log.debug("系统定时-开始");
// deleteLogsJobBmo.deleteOverdueLogs();
log.debug("系统定时-结束");
} catch (Exception e) {
log.error("系统定时-异常", e);
}
try {
// 独占模式释放,让其他人可以抢占
jedisTemplate.opsForValue().getAndSet(DELETE_JOB_KEY, false);
} catch (Exception ignore) {
// ignore
}
}
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
String cronString = sysFlatDictBmo.getter().DeleteLogsJobTime();
log.debug("job任务CronTrigger设定={}", cronString);
return new CronTrigger(cronString).nextExecutionTime(triggerContext);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(this, this);
}
评论区