|
|
|
@ -1,12 +1,15 @@
|
|
|
|
|
package com.example.serive.impl;
|
|
|
|
|
|
|
|
|
|
import com.dangdang.ddframe.job.api.ElasticJob;
|
|
|
|
|
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
|
|
|
|
|
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
|
|
|
|
|
import com.dangdang.ddframe.job.executor.handler.JobProperties;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.api.JobScheduler;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.api.strategy.impl.AverageAllocationJobShardingStrategy;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.internal.config.LiteJobConfigurationGsonFactory;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.internal.schedule.JobRegistry;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.internal.schedule.JobScheduleController;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.internal.storage.JobNodePath;
|
|
|
|
|
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
|
|
|
|
|
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
|
|
|
|
|
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
|
|
|
|
|
import com.example.entity.Job;
|
|
|
|
@ -15,10 +18,10 @@ import com.example.serive.JobService;
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
import com.google.common.base.Strings;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.curator.framework.CuratorFramework;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -32,9 +35,9 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
private CoordinatorRegistryCenter center;
|
|
|
|
|
@Resource
|
|
|
|
|
private ZookeeperRegistryCenter zookeeperRegistryCenter;
|
|
|
|
|
|
|
|
|
|
// @Resource
|
|
|
|
|
// @Resource
|
|
|
|
|
// private DataSource dataSource;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addJob(Job job) {
|
|
|
|
|
boolean overwrite = job.getOverwrite();
|
|
|
|
@ -42,11 +45,8 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
.newBuilder(job.getJobName(), job.getCron(), job.getShardingTotalCount())
|
|
|
|
|
.shardingItemParameters(job.getShardingItemParameters())
|
|
|
|
|
.description(job.getDescription())
|
|
|
|
|
.failover(job.isFailover())
|
|
|
|
|
.jobParameter(job.getJobParameter())
|
|
|
|
|
.misfire(job.isMisfire())
|
|
|
|
|
.jobProperties(JobProperties.JobPropertiesEnum.JOB_EXCEPTION_HANDLER.getKey(), job.getJobProperties().get("JOB_EXCEPTION_HANDLER"))
|
|
|
|
|
.jobProperties(JobProperties.JobPropertiesEnum.EXECUTOR_SERVICE_HANDLER.getKey(), job.getJobProperties().get("EXECUTOR_SERVICE_HANDLER"))
|
|
|
|
|
.misfire(true)
|
|
|
|
|
.build();
|
|
|
|
|
SimpleJobConfiguration sjc = new SimpleJobConfiguration(
|
|
|
|
|
jcc, job.getJobClass()
|
|
|
|
@ -54,26 +54,61 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
LiteJobConfiguration ljc = LiteJobConfiguration
|
|
|
|
|
.newBuilder(sjc)
|
|
|
|
|
.overwrite(overwrite)
|
|
|
|
|
.monitorPort(job.getMonitorPort())
|
|
|
|
|
.monitorExecution(job.isMonitorExecution())
|
|
|
|
|
.maxTimeDiffSeconds(job.getMaxTimeDiffSeconds())
|
|
|
|
|
.jobShardingStrategyClass(job.getJobShardingStrategyClass())
|
|
|
|
|
.reconcileIntervalMinutes(job.getReconcileIntervalMinutes())
|
|
|
|
|
.jobShardingStrategyClass(AverageAllocationJobShardingStrategy.class.getCanonicalName())
|
|
|
|
|
.build();
|
|
|
|
|
// 配置数据源
|
|
|
|
|
// JobEventConfiguration jec = new JobEventRdbConfiguration(dataSource);
|
|
|
|
|
//配置监听
|
|
|
|
|
// MyNormalListener myNormalListener = new MyNormalListener();
|
|
|
|
|
new JobScheduler(center, ljc).init();
|
|
|
|
|
try {
|
|
|
|
|
ElasticJob elasticJob = (ElasticJob) Class.forName(job.getJobClass()).newInstance();
|
|
|
|
|
// 配置数据源
|
|
|
|
|
// if (job.isJobEvent()) {
|
|
|
|
|
// JobEventConfiguration jec = new JobEventRdbConfiguration(dataSource);
|
|
|
|
|
// new SpringJobScheduler(elasticJob, center, ljc, jec);
|
|
|
|
|
// } else {
|
|
|
|
|
// new SpringJobScheduler(elasticJob, center, ljc);
|
|
|
|
|
// }
|
|
|
|
|
new SpringJobScheduler(elasticJob, center, ljc);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
log.error("加载任务失败,任务为 {}", job.getJobName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void removeJob(String jobName) {
|
|
|
|
|
try {
|
|
|
|
|
CuratorFramework client = zookeeperRegistryCenter.getClient();
|
|
|
|
|
client.delete().deletingChildrenIfNeeded().forPath("/" + jobName);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("删除任务:{} 错误 {}", jobName, e.getMessage());
|
|
|
|
|
JobScheduleController jobScheduleController = JobRegistry.getInstance().getJobScheduleController(jobName);
|
|
|
|
|
if (jobScheduleController != null) {
|
|
|
|
|
jobScheduleController.pauseJob();
|
|
|
|
|
jobScheduleController.shutdown();
|
|
|
|
|
zookeeperRegistryCenter.remove("/" + jobName);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
//zk直接删除任务
|
|
|
|
|
// try {
|
|
|
|
|
// CuratorFramework client = zookeeperRegistryCenter.getClient();
|
|
|
|
|
// client.delete().deletingChildrenIfNeeded().forPath("/" + jobName);
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// log.error("删除任务:{} 错误 {}", jobName, e.getMessage());
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 暂停任务
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void pauseJob(String jobName) {
|
|
|
|
|
JobScheduleController jobScheduleController = JobRegistry.getInstance().getJobScheduleController(jobName);
|
|
|
|
|
if (jobScheduleController != null) {
|
|
|
|
|
jobScheduleController.pauseJob();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 立刻启动作业
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void start(String jobName) {
|
|
|
|
|
JobScheduleController jobScheduleController = JobRegistry.getInstance().getJobScheduleController(jobName);
|
|
|
|
|
if (jobScheduleController != null) {
|
|
|
|
|
jobScheduleController.triggerJob();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -86,7 +121,6 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
try {
|
|
|
|
|
JobNodePath jobNodePath = new JobNodePath(jobName);
|
|
|
|
|
LiteJobConfiguration liteJobConfig = LiteJobConfigurationGsonFactory.fromJson(center.get(jobNodePath.getConfigNodePath()));
|
|
|
|
|
job.setJobType(liteJobConfig.getTypeConfig().getJobType().name());
|
|
|
|
|
this.buildSimpleJobSettings(jobName, job, liteJobConfig);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("查询job任务:{} 错误 {}", jobName, e.getMessage());
|
|
|
|
@ -102,7 +136,6 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
public List<JobBriefInfo> getAllJobsDetails() {
|
|
|
|
|
List<String> jobNames = zookeeperRegistryCenter.getChildrenKeys("/");
|
|
|
|
|
List<JobBriefInfo> result = new ArrayList<>(jobNames.size());
|
|
|
|
|
|
|
|
|
|
for (String each : jobNames) {
|
|
|
|
|
JobBriefInfo jobBriefInfo = this.getJobBriefInfo(each);
|
|
|
|
|
if (null != jobBriefInfo) {
|
|
|
|
@ -124,20 +157,12 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
|
|
|
|
|
private void buildSimpleJobSettings(String jobName, Job job, LiteJobConfiguration liteJobConfig) {
|
|
|
|
|
job.setJobName(jobName);
|
|
|
|
|
job.setJobType(liteJobConfig.getTypeConfig().getJobType().name());
|
|
|
|
|
job.setJobClass(liteJobConfig.getTypeConfig().getJobClass());
|
|
|
|
|
job.setShardingTotalCount(liteJobConfig.getTypeConfig().getCoreConfig().getShardingTotalCount());
|
|
|
|
|
job.setCron(liteJobConfig.getTypeConfig().getCoreConfig().getCron());
|
|
|
|
|
job.setShardingItemParameters(liteJobConfig.getTypeConfig().getCoreConfig().getShardingItemParameters());
|
|
|
|
|
job.setJobParameter(liteJobConfig.getTypeConfig().getCoreConfig().getJobParameter());
|
|
|
|
|
job.setMonitorExecution(liteJobConfig.isMonitorExecution());
|
|
|
|
|
job.setMaxTimeDiffSeconds(liteJobConfig.getMaxTimeDiffSeconds());
|
|
|
|
|
job.setMonitorPort(liteJobConfig.getMonitorPort());
|
|
|
|
|
job.setFailover(liteJobConfig.getTypeConfig().getCoreConfig().isFailover());
|
|
|
|
|
job.setMisfire(liteJobConfig.getTypeConfig().getCoreConfig().isMisfire());
|
|
|
|
|
job.setJobShardingStrategyClass(liteJobConfig.getJobShardingStrategyClass());
|
|
|
|
|
job.setDescription(liteJobConfig.getTypeConfig().getCoreConfig().getDescription());
|
|
|
|
|
job.setReconcileIntervalMinutes(liteJobConfig.getReconcileIntervalMinutes());
|
|
|
|
|
job.setOverwrite(liteJobConfig.isOverwrite());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -179,7 +204,6 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
++disabledServerCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return disabledServerCount == serversPath.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -191,7 +215,6 @@ public class JobServiceImpl implements JobService {
|
|
|
|
|
shardingInstances.add(instanceId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !instances.containsAll(shardingInstances) || shardingInstances.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|