• Elastic Job3.0


    demo

    添加依赖

            <dependency>
                <groupId>org.apache.shardingsphere.elasticjob</groupId>
                <artifactId>elasticjob-lite-core</artifactId>
                <version>3.0.0-alpha</version>
            </dependency>

    配置文件添加

    elasticJob:
      regCenter:
        serverLists: localhost:2181
        namespace: live-task
    
    elastic-job:
      jobs:
        - jobName: testJob
          cron: '*/5 * * * * ?'

    添加ElasticJob配置类:

    注册中心

    package com.wjc.server.config;
    
    import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
    import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
    import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @author wjc
     * @description
     * @date 2020/10/23
     */
    @Configuration
    public class ElasticRegCenterConfig {
        @Bean(initMethod = "init")
        public CoordinatorRegistryCenter regCenter(
                @Value("${elasticJob.regCenter.serverLists}") final String serverList,
                @Value("${elasticJob.regCenter.namespace}") final String namespace) {
            return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
        }
    }

    配置对应实体类

    package com.wjc.server.config;
    
    import lombok.Data;
    
    /**
     * @author wjc
     * @description
     * @date 2020/10/23
     */
    @Data
    public class JobEntity {
    
        /**
         * 任务名称
         */
        private String jobName;
        /**
         * 分片数量
         */
        private int shardingTotalCount = 1;
        /**
         * cron表达式
         */
        private String cron;
        /**
         * 当前分片参数
         */
        private String shardingItemParameters;
        /**
         * 当前任务参数
         */
        private String jobParameter;
        /**
         * 任务类型 0.SimpleJob 1.DataflowJob 2.ScriptJob
         */
        private Integer type = 0;
        /**
         * 任务说明
         */
        private String description;
    
    }

    获取任务配置

    package com.wjc.server.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author wjc
     * @description
     * @date 2020/10/23
     */
    @Data
    @Component
    @ConfigurationProperties(prefix = "elastic-job")
    public class JobEntityConfig {
        private List<JobEntity> jobs = new ArrayList<>();
    }

    最终配置类

    package com.wjc.server.config;
    
    import org.apache.shardingsphere.elasticjob.api.ElasticJob;
    import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
    import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
    import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.PostConstruct;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author wjc
     * @description
     * @date 2020/10/23
     */
    @Configuration
    public class ElasticJobConfig {
    
        @Autowired
        private CoordinatorRegistryCenter regCenter;
        @Autowired
        private ApplicationContext applicationContext;
        @Autowired
        private JobEntityConfig jobEntityConfig;
    
        @PostConstruct
        public void executor(){
            List<JobConfiguration> list = getJobConfiguration();
            list.forEach(job -> new ScheduleJobBootstrap(regCenter, (ElasticJob)applicationContext.getBean(job.getJobName()), job).schedule());
        }
    
        public List<JobConfiguration> getJobConfiguration(){
            List<JobEntity> jobEntityList = jobEntityConfig.getJobs();
            List<JobConfiguration> list = new ArrayList<>(jobEntityList.size());
            jobEntityList.forEach(jobEntity -> list.add(JobConfiguration.newBuilder(jobEntity.getJobName(), jobEntity.getShardingTotalCount()).cron(jobEntity.getCron()).description(jobEntity.getDescription()).shardingItemParameters(jobEntity.getShardingItemParameters()).build()));
            return list;
        }
    }
    博客链接:https://www.cnblogs.com/gqymy/
  • 相关阅读:
    ubuntu12.04.2上利用cmake安装opencv2.4.6
    微软无线鼠标3500滚轮问题
    Linux 安装 Tomcat
    Linux 安装 MySQL
    Linux 安装 JDK
    Linux 安装 Redis 及踩坑
    IDEA Git 撤销push(回退到指定版本)
    【Java】Ajax实现级联城市
    node.js更换镜像源
    【Java】数据库连接池的简单使用
  • 原文地址:https://www.cnblogs.com/gqymy/p/14665260.html
Copyright © 2020-2023  润新知