Search in sources :

Example 1 with PtElasticJobSimpleJob

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;
}
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)

Example 2 with PtElasticJobSimpleJob

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;
}
Also used : PtElasticJobSimpleJob(com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob) JobEventRdbConfiguration(com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) SimpleJobConfiguration(com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration) DataSource(javax.sql.DataSource) ZookeeperRegistryCenter(com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) LiteJobConfiguration(com.dangdang.ddframe.job.lite.config.LiteJobConfiguration) ElasticJob(com.dangdang.ddframe.job.api.ElasticJob) SpringJobScheduler(com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler) PtElasticJobSimpleJob(com.pamirs.attach.plugin.shadowjob.obj.PtElasticJobSimpleJob) SimpleJob(com.dangdang.ddframe.job.api.simple.SimpleJob) ElasticSimpleJob(com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob) ElasticSimpleJob(com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob) Map(java.util.Map)

Aggregations

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