2台机器同时启动的时候第一次会同时进入定时任务,这是为什么 cron表达式时 10秒进行一次

package com.example.demo.jobdetail;


import com.example.demo.config.zookeeper.ZKClient;
import com.example.demo.utils.RedisUtils;
import com.example.demo.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
public abstract class ZkQuartzJobBean extends QuartzJobBean {


    protected void executeInternal(JobExecutionContext jobexecutioncontext) throws JobExecutionException {
        CuratorFramework zkClient= SpringUtil.getBean(CuratorFramework.class);
        ZKClient client = SpringUtil.getBean(ZKClient.class);
        String node="";
        try {
            node= zkClient.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.EPHEMERAL) //零时顺时节点
                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)//只有创建者才有ACL权限
                    .forPath(client.getLockPath());
            if(StringUtils.isNotEmpty(node)){
                run(jobexecutioncontext);
            }
        }
        catch (Exception e){
            log.error("拒绝执行任务");
        }
        finally {
            try {
                if(StringUtils.isNotEmpty(node)){
                    zkClient.delete().forPath(node);
                }
            }
            catch (Exception e){log.error("删除节点失败:{}",e); }
        }
    }
    abstract void run(JobExecutionContext jobexecutioncontext);
}

 

 

2021-06-28 14:52:40.036  INFO 14608 --- [lerThreadPool-1] com.example.demo.jobdetail.HelloWold     : start task:2021-06-28 14:52:40.000032

2021-06-28 14:52:40.014  INFO 9944 --- [lerThreadPool-9] com.example.demo.jobdetail.HelloWold     : start task:2021-06-28 14:52:40.000014
 

每10秒执行一次.就是逢10s,20s,30s...60s,当然就会一起进咯.有这种定时任务时,要做好幂等操作,最次也要做个排他操作