package cn.biz.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
/**
* @Description 可动态更改时间的定时任务
* @Author
* @Date 2021/3/2
* @Version 1.0.0
*/
@Component
public class SchedulingTask implements SchedulingConfigurer {
private final Logger logger = LoggerFactory.getLogger(SchedulingTask .class);
// cron表达式,我们动态更改此属性的值即可更改定时任务的执行时间
private String expression = "0 0/2 * * * ?";
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
// 定时任务要执行的方法
Runnable task = () -> {
//1、重试策略:初试时间为1s 重试3次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
//2、通过工厂创建连接
CuratorFramework client = CuratorFrameworkFactory.newClient(zkAddress, retryPolicy);
//3、开启连接
client.start();
//4、获取zk分布式锁
InterProcessMutex mutex = new InterProcessMutex(client, "/curator/lock");
boolean flag = false;
try {
//尝试获取锁,最多等待0秒
flag = mutex.acquire(0, TimeUnit.SECONDS);
Thread currentThread = Thread.currentThread();
if(flag){//锁抢占成功 执行业务逻辑
logger.info(">>> configureTasks-----------【"+expression+"】");
//开始业务逻辑
}
//
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(flag){
//1.先释放锁
if(flag){
try {
mutex.release();
} catch (Exception e) {
e.printStackTrace();
}
}
//2.后关闭zk客户端连接
if (client != null) {
client.close();
}
}
}
};
// 调度实现的时间控制
Trigger trigger = triggerContext -> {
CronTrigger cronTrigger = new CronTrigger(expression);
return cronTrigger.nextExecutionTime(triggerContext);
};
taskRegistrar.addTriggerTask(task, trigger);
}
public String getExpression() {
return expression;
}
public void setExpression(String expression) {
this.expression = expression;
}
}
zookeeper分布式锁解决集群定时任务重复执行问题
最新推荐文章于 2024-09-18 10:57:53 发布