elasticJob

文章详细介绍了如何在Java项目中使用ElasticJob进行作业调度,包括简单作业、数据流作业、工作流模式、脚本作业、HTTP作业以及一次性调度的实现。同时,展示了SpringBoot集成ElasticJob的配置方法,并提到了错误处理策略和通知机制,如邮件、企业微信和钉钉通知。
摘要由CSDN通过智能技术生成

pom

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

环境
jdk8及以上版本
maven:3.5.0及以上版本
zookeeper:3.6及以上版本

作业开发

@Slf4j
public class MyJob implements SimpleJob {

    @Override
    public void execute(ShardingContext shardingContext) {
        switch (shardingContext.getShardingItem()) {
            case 0 :
                log.info("0");
                System.out.println("0");
                break;
            case 1 :
                log.info("1");
                System.out.println("1");
                break;
            case 2 :
                log.info("2");
                System.out.println("2");
                break;
        }
    }
}

作业调度

	public static void main(String[] args) {
        new ScheduleJobBootstrap(createRegistryCenter(),
                new MyJob(),
                createJobConfiguration()).schedule();
    }
    private static CoordinatorRegistryCenter createRegistryCenter(){
        CoordinatorRegistryCenter center = new ZookeeperRegistryCenter(
                new ZookeeperConfiguration("127.0.0.1:2181","my-job"));
        center.init();
        return center;
    }
    private static JobConfiguration createJobConfiguration(){
        // 创建作业配置
        JobConfiguration build = JobConfiguration.newBuilder("MyJob", 3)
                .cron("0/5 * * * * ?").build();
        return build;
    }

数据流作业

@Slf4j
public class MyElasticJob implements DataflowJob<String> {
    //获取数据   从数据库中
    @Override
    public List<String> fetchData(ShardingContext context) {
        //TODO 模拟从数据库获取数据
        log.info("开始拉取数据");
        return Arrays.asList("a","b","c","d");
    }
    //具体处理
    @Override
    public void processData(ShardingContext shardingContext, List<String> list) {
        //数据处理
        log.info("开始处理数据");
        for (String str : list) {
            log.info(str);
        }
    }
}

工作流模式的调度类

public static void main(String[] args) {
        new ScheduleJobBootstrap(createRegistryCenter(),
                new MyElasticJob(),
                createJobConfiguration()).schedule();
    }

脚本job

public class ScriptDemo {
    public static void main(String[] args) throws IOException {
        new ScheduleJobBootstrap(createRegistryCenter(),
                "SCRIPT",   //启动类型
                createJobConfiguration())
                .schedule();
    }

    private static CoordinatorRegistryCenter createRegistryCenter(){
        CoordinatorRegistryCenter center = new ZookeeperRegistryCenter(
                new ZookeeperConfiguration("127.0.0.1:2181","my-script-job"));
        center.init();
        return center;
    }

    private static JobConfiguration createJobConfiguration() throws IOException {
        // 创建作业配置
        JobConfiguration configuration = JobConfiguration.newBuilder("scriptElasticJob", 3)
                .cron("0/5 * * * * ?")
                .setProperty(ScriptJobProperties.SCRIPT_KEY,buildScriptCommandLine())
                .build();

        return configuration;
    }

    public static String buildScriptCommandLine() throws IOException {
        if (System.getProperties().getProperty("os.name").contains("window")){
            return Paths.get(ScriptDemo.class.getResource("/script/job.bat")
                    .getPath().substring(1)).toString();
        }

        Path path = Paths.get(ScriptDemo.class.getResource("/script/job.sh").getPath());
        Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x"));
        return path.toString();
    }

}

job.bat

@echo sharding execution context is %*

job.sh

#!/bin/bash
echo sharding execution context is $*

http作业

public static void main(String[] args) {
        new ScheduleJobBootstrap(createRegistryCenter(), "HTTP",createJobConfiguration())
                .schedule();
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
        ZookeeperConfiguration zc = new ZookeeperConfiguration("127.0.0.1:2181", "httpjob");
        zc.setConnectionTimeoutMilliseconds(40000);
        zc.setMaxRetries(5);
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zc);
        regCenter.init();
        return regCenter;
    }

    private static JobConfiguration createJobConfiguration() {
        return JobConfiguration.newBuilder("MyHTTPJob", 3)
                .description("HTTP作业")
                .cron("0/5 * * * * ?")
                .setProperty(HttpJobProperties.URI_KEY,
                        "http://127.0.0.1:8000/execute")
                .setProperty(HttpJobProperties.METHOD_KEY, "GET")
                .setProperty(HttpJobProperties.DATA_KEY, "source=MyHTTPJob")
                .overwrite(true)
                .failover(true)
                .build();
    }

一次性调度

public static void main(String[] args) {
        OneOffJobBootstrap jobBootstrap = new OneOffJobBootstrap(createRegistryCenter(),new MyElasticJob(),createJobConfiguration());
        jobBootstrap.execute();
        jobBootstrap.execute();
        jobBootstrap.execute();
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(new ZookeeperConfiguration("127.0.0.1:2181", "elastic-job-demo"));
        regCenter.init();
        return regCenter;
    }

    private static JobConfiguration createJobConfiguration() {
        return JobConfiguration.newBuilder("MyOneJob", 1)
                .build();
    }

springboot集成elasticjob

@Slf4j
@Component
public class MyJob implements SimpleJob {
    @Override
    public void execute(ShardingContext shardingContext) {
        switch (shardingContext.getShardingItem()) {
            case 0 :
                log.info("任务执行--MyElasticJob - 0 ");
                break;
            case 1 :
                log.info("任务执行--MyElasticJob - 1 ");
                break;
            case 2 :
                log.info("任务执行--MyElasticJob - 2 ");
                break;
        }
    }
}

yaml

# yml中添加注册中心和作业配置
elasticjob:
  #注册中心配置
  reg-center:
    server-lists: 127.0.0.1:2181
    namespace: elasticjob-lite-springboot-demo
  #作业配置
  jobs:
    simpleJob:
      elasticJobClass: com.tuling.boot.MyJob
      cron: 0/5 * * * * ?
      timeZone: GMT+08:00
      shardingTotalCount: 3
      shardingItemParameters: 0=Beijing,1=Shanghai,2=Guangzhou

定时调度
定时调度作业在 Spring Boot 应用程序启动完成后会自动启动,无需其他额外操作。

一次性调度

@RestController
public class OneOffJobController {
    // 通过 "@Resource" 注入
    @Resource(name = "myOneOffJobBean")
    private OneOffJobBootstrap myOneOffJob;

    @GetMapping("/execute")
    public String executeOneOffJob() {
        myOneOffJob.execute();
        return "{\"msg\":\"OK\"}";
    }

     @Autowired
     @Qualifier(name = "myOneOffJobBean")
     private OneOffJobBootstrap myOneOffJob2;
     @GetMapping("/execute")
     public String executeOneOffJob2() {
         myOneOffJob.execute();
         return "{\"msg\":\"OK\"}";
     }
}

yaml

elasticjob:
  jobs:
    myOneOffJob:
      elasticJobClass: com.tuling.boot.MyJob
      jobBootstrapBeanName: myOneOffJobBean
      shardingTotalCount: 3

配置错误处理策略
yaml

elasticjob:
  jobs:
    jobErrorHandlerType: IGNORE #忽略异常策略

controller

@RestController
public class OneOffJobController {
    // 通过 "@Resource" 注入
    @Resource(name = "myOneOffJobBean")
    private OneOffJobBootstrap myOneOffJob;

    @GetMapping("/execute")
    public String executeOneOffJob() {
        myOneOffJob.execute();
        return "{\"msg\":\"OK\"}";
    }

     @Autowired
     @Qualifier(name = "myOneOffJobBean")
     private OneOffJobBootstrap myOneOffJob2;
     @GetMapping("/execute")
     public String executeOneOffJob2() {
         myOneOffJob.execute();
         return "{\"msg\":\"OK\"}";
     }
}

邮件通知策略
pom

<dependency>
   <groupId>org.apache.shardingsphere.elasticjob</groupId>
   <artifactId>elasticjob-error-handler-email</artifactId>
   <version>${latest.release.version}</version>
</dependency>

yaml

elasticjob:
  reg-center:
  ...
  jobs:
   ...
    jobErrorHandlerType: EMAIL 
  props:
    email:
      host: host
      port: 465
      username: username
      password: password
      useSsl: true
      subject: ElasticJob error message
      from: from@xxx.xx
      to: to1@xxx.xx,to2@xxx.xx
      cc: cc@xxx.xx
      bcc: bcc@xxx.xx
      debug: false

可配置属性
在这里插入图片描述
企业微信通知策略

        <dependency>
            <groupId>org.apache.shardingsphere.elasticjob</groupId>
            <artifactId>elasticjob-error-handler-wechat</artifactId>
            <version>${latest.release.version}</version>
        </dependency>

yaml

elasticjob:
  reg-center:
  ...
  jobs:
   ...
    jobErrorHandlerType: EMAIL 
  props:
    wechat:
      webhook: you_webhook
      connectTimeout: 3000
      readTimeout: 5000

可配置属性
在这里插入图片描述
钉钉通知策略
pom

        <dependency>
            <groupId>org.apache.shardingsphere.elasticjob</groupId>
            <artifactId>elasticjob-error-handler-dingtalk</artifactId>
            <version>${latest.release.version}</version>
        </dependency>

yml配置

elasticjob:
  reg-center:
  ...
  jobs:
   ...
    jobErrorHandlerType: DINGTALK 
  props:
    dingtalk:
      webhook: you_webhook
      keyword: you_keyword
      secret: you_secret
      connectTimeout: 3000
      readTimeout: 5000

可配置策略
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值