针对elasticJob,在服务未完全启动的情况,通过拒绝执行方法来避免因为依赖service等组件未初始化完成,导致定时任务业务逻辑执行异常。
public class ElasticJobState {public static Boolean JOB_STATE = Boolean.FALSE;public static void enable() {ElasticJobState.JOB_STATE = Boolean.TRUE;}public static void disable() {ElasticJobState.JOB_STATE = Boolean.FALSE;}}
public class ElasticJobApplicationStartListener implements ApplicationListener<ApplicationStartedEvent> {@Overridepublic void onApplicationEvent(ApplicationStartedEvent event) {log.info("elastic job start..................");ElasticJobState.enable();}}
public class ElasticJobApplicationClosedListener implements ApplicationListener<ContextClosedEvent> {@Overridepublic void onApplicationEvent(ContextClosedEvent event) {log.info("elastic job closed..................");ElasticJobState.disable();}}
@Slf4jpublic abstract class XySimpleJob implements SimpleJob {@Overridepublic void execute(ShardingContext shardingContext) {String jobName = shardingContext.getJobName();if (!ElasticJobState.JOB_STATE) {log.warn("The service is not ready and the scheduled task is rejected.............jobName:{}", jobName);return;}JobDescInfo jobDescInfo = getJobDescInfo();int shardingTotalCount = shardingContext.getShardingTotalCount();int shardingItem = shardingContext.getShardingItem();String taskId = shardingContext.getTaskId();log.info("jobName:{},shardingTotalCount:{},shardingItem:{},taskId:{}", jobName, shardingTotalCount, shardingItem, taskId);//调用执行目标方法executeJob(shardingContext);}protected abstract void executeJob(ShardingContext shardingContext);protected abstract JobDescInfo getJobDescInfo();}
cat监控同理
