All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wangchenning.autoconfig.SimpleJobAutoConfig Maven / Gradle / Ivy

The newest version!
package com.github.wangchenning.autoconfig;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.github.wangchenning.springbootelaticjob.job.MySimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Map;

@Configuration
@ConditionalOnBean(CoordinatorRegistryCenter.class)
@AutoConfigureAfter(ZookeeperAutoConfig.class)
public class SimpleJobAutoConfig {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private CoordinatorRegistryCenter zkCenter;
    @PostConstruct
    public void initSimpleJob() {
        Map beans = applicationContext.getBeansWithAnnotation(ElasticSimpleJob.class);
        for (Map.Entry entry : beans.entrySet()) {
            Object instance = entry.getValue();
            Class[] interfaces = instance.getClass().getInterfaces();
            for (Class superInterface : interfaces) {
                if (superInterface == SimpleJob.class) {
                    ElasticSimpleJob annotation = instance.getClass().getAnnotation(ElasticSimpleJob.class);
                    String jobName = annotation.jobName();
                    String corn = annotation.corn();
                    int shardingTotalCount = annotation.shardingTotalCount();
                    boolean overwrite = annotation.overwrite();

                    //job核心配置
                    JobCoreConfiguration jcc = JobCoreConfiguration.newBuilder(jobName, corn, shardingTotalCount).build();
                    //job类型配置
                    JobTypeConfiguration jtc = new SimpleJobConfiguration(jcc, instance.getClass().getCanonicalName());
                    //job根的配置(LiteJobConfiguration)
                    LiteJobConfiguration ljc = LiteJobConfiguration.newBuilder(jtc)
                            .overwrite(overwrite).build();
                    new JobScheduler(zkCenter, ljc).init();
                }
            }
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy