Search in sources :

Example 1 with PtDataflowJob

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;
}
Also used : JobEventRdbConfiguration(com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration) Field(java.lang.reflect.Field) ElasticJob(com.dangdang.ddframe.job.api.ElasticJob) SpringJobScheduler(com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler) JobEventConfiguration(com.dangdang.ddframe.job.event.JobEventConfiguration) PtElasticJobSimpleJob(com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob) SimpleJob(com.dangdang.ddframe.job.api.simple.SimpleJob) ElasticSimpleJob(com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob) SpringJobScheduler(com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler) JobScheduler(com.dangdang.ddframe.job.lite.api.JobScheduler) PtElasticJobSimpleJob(com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob) ElasticJobListener(com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) DataSource(javax.sql.DataSource) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) PtDataflowJob(com.pamirs.attach.plugin.shadowjob.obj.PtDataflowJob) LiteJobConfiguration(com.dangdang.ddframe.job.lite.config.LiteJobConfiguration) Map(java.util.Map) JobTypeConfiguration(com.dangdang.ddframe.job.config.JobTypeConfiguration) CoordinatorRegistryCenter(com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter)

Aggregations

ElasticJob (com.dangdang.ddframe.job.api.ElasticJob)1 SimpleJob (com.dangdang.ddframe.job.api.simple.SimpleJob)1 JobTypeConfiguration (com.dangdang.ddframe.job.config.JobTypeConfiguration)1 JobEventConfiguration (com.dangdang.ddframe.job.event.JobEventConfiguration)1 JobEventRdbConfiguration (com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration)1 JobScheduler (com.dangdang.ddframe.job.lite.api.JobScheduler)1 ElasticJobListener (com.dangdang.ddframe.job.lite.api.listener.ElasticJobListener)1 LiteJobConfiguration (com.dangdang.ddframe.job.lite.config.LiteJobConfiguration)1 SpringJobScheduler (com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler)1 CoordinatorRegistryCenter (com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter)1 ElasticSimpleJob (com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob)1 PtDataflowJob (com.pamirs.attach.plugin.shadowjob.obj.PtDataflowJob)1 PtElasticJobSimpleJob (com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 DataSource (javax.sql.DataSource)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1