use of com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob 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;
}
use of com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob in project LinkAgent by shulieTech.
the class JobExecutorFactoryGetJobExecutorInterceptor method processWithoutSpring.
private boolean processWithoutSpring(ShadowJob shadowJob) {
boolean ok = false;
Map<String, SimpleJob> map = PradarSpringUtil.getBeanFactory().getBeansOfType(SimpleJob.class);
for (Map.Entry<String, SimpleJob> entry : map.entrySet()) {
SimpleJob simpleJob = entry.getValue();
if (!simpleJob.getClass().getName().equals(shadowJob.getClassName())) {
continue;
}
ZookeeperRegistryCenter regCenter = getRegisterConter();
Object originJob = simpleJob;
ElasticSimpleJob elasticSimpleJobAnnotation = simpleJob.getClass().getAnnotation(ElasticSimpleJob.class);
Class ptJobClass = PtElasticJobSimpleJob.class;
String cron = StringUtils.defaultIfBlank(elasticSimpleJobAnnotation.cron(), elasticSimpleJobAnnotation.value());
SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(JobCoreConfiguration.newBuilder(ptJobClass.getName(), cron, elasticSimpleJobAnnotation.shardingTotalCount()).shardingItemParameters(elasticSimpleJobAnnotation.shardingItemParameters()).build(), simpleJob.getClass().getCanonicalName());
LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build();
DefaultListableBeanFactory defaultListableBeanFactory = PradarSpringUtil.getBeanFactory();
BeanDefinitionBuilder beanSimple = BeanDefinitionBuilder.rootBeanDefinition(ptJobClass);
defaultListableBeanFactory.registerBeanDefinition(ptJobClass.getSimpleName() + originJob.getClass().getName(), beanSimple.getBeanDefinition());
ElasticJob ptJob = (ElasticJob) PradarSpringUtil.getBeanFactory().getBean(ptJobClass.getSimpleName() + originJob.getClass().getName());
String dataSourceRef = elasticSimpleJobAnnotation.dataSource();
if (StringUtils.isNotBlank(dataSourceRef)) {
if (!PradarSpringUtil.getBeanFactory().containsBean(dataSourceRef)) {
throw new RuntimeException("not exist datasource [" + dataSourceRef + "] !");
}
DataSource dataSource = (DataSource) PradarSpringUtil.getBeanFactory().getBean(dataSourceRef);
JobEventRdbConfiguration jobEventRdbConfiguration = new JobEventRdbConfiguration(dataSource);
SpringJobScheduler jobScheduler = new SpringJobScheduler(ptJob, regCenter, liteJobConfiguration, jobEventRdbConfiguration);
jobScheduler.init();
if (originJob instanceof SimpleJob) {
((PtElasticJobSimpleJob) Reflect.on(jobScheduler).get("elasticJob")).setSimpleJob((SimpleJob) originJob);
}
ok = true;
break;
} else {
SpringJobScheduler jobScheduler = new SpringJobScheduler(ptJob, regCenter, liteJobConfiguration);
jobScheduler.init();
if (originJob instanceof SimpleJob) {
((PtElasticJobSimpleJob) Reflect.on(jobScheduler).get("elasticJob")).setSimpleJob((SimpleJob) originJob);
}
ok = true;
break;
}
}
return ok;
}
Aggregations