use of com.pamirs.attach.plugin.shadowjob.obj.PtDataflowJob in project LinkAgent by shulieTech.
the class JobExecutorFactoryGetJobExecutorInterceptor method registerShadowJob.
public boolean registerShadowJob(ShadowJob shadowJob) throws Throwable {
if (null == PradarSpringUtil.getBeanFactory()) {
logger.info("[spring-context] is null, 无法注册影子 ElasticJob");
return false;
}
try {
if (checkRegistered(shadowJob)) {
return true;
}
boolean found = false;
Map<String, JobScheduler> schedulerMap = PradarSpringUtil.getBeanFactory().getBeansOfType(JobScheduler.class);
for (Map.Entry<String, JobScheduler> s : schedulerMap.entrySet()) {
JobScheduler jobScheduler = s.getValue();
LiteJobConfiguration liteJobConfiguration = Reflect.on(jobScheduler).get("liteJobConfig");
JobTypeConfiguration jobTypeConfiguration = liteJobConfiguration.getTypeConfig();
String OriginJobName = jobTypeConfiguration.getJobClass();
if (!OriginJobName.equals(shadowJob.getClassName())) {
continue;
}
found = true;
JobEventConfiguration jobEventConfig = null;
try {
DataSource dataSource = PradarSpringUtil.getBeanFactory().getBean(DataSource.class);
jobEventConfig = new JobEventRdbConfiguration(dataSource);
} catch (Exception e) {
// ignore
}
ElasticJob originJob = (ElasticJob) findJobInstance(jobTypeConfiguration.getJobClass());
if (originJob == null) {
originJob = (ElasticJob) Class.forName(jobTypeConfiguration.getJobClass()).newInstance();
}
Field liteJobConfigField = JobScheduler.class.getDeclaredField("liteJobConfig");
liteJobConfigField.setAccessible(true);
Field regCenterField = JobScheduler.class.getDeclaredField("regCenter");
regCenterField.setAccessible(true);
final LiteJobConfiguration liteJobConfig = (LiteJobConfiguration) liteJobConfigField.get(jobScheduler);
final CoordinatorRegistryCenter registryCenter = (CoordinatorRegistryCenter) regCenterField.get(jobScheduler);
Class ptJobClass = getPtJobClass(jobTypeConfiguration.getJobType());
LOGGER.info("shadow job registering spring className:{}, jobName:{},elasticjob.......", ptJobClass.getName(), originJob.getClass().getName());
DefaultListableBeanFactory defaultListableBeanFactory = PradarSpringUtil.getBeanFactory();
BeanDefinitionBuilder beanSimple = BeanDefinitionBuilder.rootBeanDefinition(ptJobClass);
defaultListableBeanFactory.registerBeanDefinition(ptJobClass.getSimpleName(), beanSimple.getBeanDefinition());
LiteJobConfiguration jobConfiguration = null;
if (ShaDowJobConstant.SIMPLE.equals(shadowJob.getJobDataType())) {
jobConfiguration = ElasticJobConfig.createJobConfiguration(ptJobClass, liteJobConfig.getTypeConfig().getCoreConfig().getCron(), liteJobConfig.getTypeConfig().getCoreConfig().getShardingTotalCount(), liteJobConfig.getTypeConfig().getCoreConfig().getShardingItemParameters());
} else if (ShaDowJobConstant.DATAFLOW.equals(shadowJob.getJobDataType())) {
jobConfiguration = ElasticJobConfig.createFlowJobConfiguration(ptJobClass, liteJobConfig.getTypeConfig().getCoreConfig().getCron(), liteJobConfig.getTypeConfig().getCoreConfig().getShardingTotalCount(), liteJobConfig.getTypeConfig().getCoreConfig().getShardingItemParameters());
} else {
throw new IllegalAccessException("【Elastic-Job】" + shadowJob.getJobDataType() + "JobDataType类型错误,未知类型【simple、dataFlow】");
}
Field jobNameField = jobConfiguration.getTypeConfig().getCoreConfig().getClass().getDeclaredField("jobName");
jobNameField.setAccessible(true);
jobNameField.set(jobConfiguration.getTypeConfig().getCoreConfig(), jobConfiguration.getTypeConfig().getCoreConfig().getJobName() + originJob.getClass().getName());
ElasticJobListener[] elasticJobListeners = null;
if (!StringUtil.isEmpty(shadowJob.getListenerName())) {
Class<?> listenerClass = Class.forName(shadowJob.getListenerName());
elasticJobListeners = new ElasticJobListener[] { (ElasticJobListener) listenerClass.newInstance() };
} else {
elasticJobListeners = new ElasticJobListener[0];
}
BeanDefinitionBuilder beanDefinitionBuilder = null;
// 通过BeanDefinitionBuilder创建bean定义
if (jobEventConfig != null) {
beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(SpringJobScheduler.class).setInitMethodName("init").addConstructorArgValue(PradarSpringUtil.getBeanFactory().getBean(ptJobClass.getSimpleName())).addConstructorArgValue(registryCenter).addConstructorArgValue(jobConfiguration).addConstructorArgValue(jobEventConfig).addConstructorArgValue(elasticJobListeners);
} else {
beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(SpringJobScheduler.class).setInitMethodName("init").addConstructorArgValue(PradarSpringUtil.getBeanFactory().getBean(ptJobClass.getSimpleName())).addConstructorArgValue(registryCenter).addConstructorArgValue(jobConfiguration).addConstructorArgValue(elasticJobListeners);
}
String jobBeanName = Pradar.addClusterTestPrefix(originJob.getClass().getSimpleName()) + JobScheduler.class.getSimpleName();
// 注册bean
defaultListableBeanFactory.registerBeanDefinition(jobBeanName, beanDefinitionBuilder.getBeanDefinition());
JobScheduler ptSpringJobScheduler = (JobScheduler) PradarSpringUtil.getBeanFactory().getBean(jobBeanName);
Field ptElasticJobField = ptSpringJobScheduler.getClass().getDeclaredField("elasticJob");
ptElasticJobField.setAccessible(true);
if (originJob instanceof SimpleJob) {
PtElasticJobSimpleJob ptJob = (PtElasticJobSimpleJob) ptElasticJobField.get(ptSpringJobScheduler);
ptJob.setSimpleJob((SimpleJob) originJob);
} else {
PtDataflowJob ptJob = (PtDataflowJob) ptElasticJobField.get(ptSpringJobScheduler);
ptJob.setDataflowJob((DataflowJob) originJob);
}
if (!found) {
boolean ok = false;
try {
ok = processWithoutSpring(shadowJob);
} catch (Throwable t) {
logger.info("[elastic-job] register failed. jobName is {},{}", shadowJob.getClassName(), ThrowableUtils.toString(t));
}
if (ok) {
logger.info("[elastic-job] register success. jobName is {}", shadowJob.getClassName());
registered.add(shadowJob.getClassName());
return true;
}
logger.error("[elastic-job] 未找到相关Class信息 className is {} ", shadowJob.getClassName());
shadowJob.setErrorMessage("【Elastic-Job】未找到相关Class信息,error:" + shadowJob.getClassName());
return false;
} else {
break;
}
}
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
return false;
}
registered.add(shadowJob.getClassName());
logger.info("[elastic-job] register success. jobName is {}", shadowJob.getClassName());
return true;
}
Aggregations