当前位置: 代码迷 >> 综合 >> ElasticJob‐Lite:一次性调度
  详细解决方案

ElasticJob‐Lite:一次性调度

热度:34   发布时间:2023-12-01 17:48:53.0

添加依赖(3.0.1是目前最新的Releases版本):

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

本篇博客介绍一次性调度,之前的调度都是定时调度(通过ScheduleJobBootstrap类),现在来介绍一次性调用。实现一次性调用其实非常简单,只需要将ScheduleJobBootstrap类换成OneOffJobBootstrap类即可,并且作业配置不能配置cron时间表达式。

package com.kaven.job;import com.kaven.job.my.MyKavenJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;/*** @Author: ITKaven* @Date: 2021/11/20 17:05* @Blog: https://kaven.blog.csdn.net* @Leetcode: https://leetcode-cn.com/u/kavenit* @Notes:*/
public class Application {
    public static void main(String[] args) {
    OneOffJobBootstrap oneOffJobBootstrap = new OneOffJobBootstrap(createRegistryCenter(), new MyKavenJob(),createJobConfiguration());oneOffJobBootstrap.execute();}private static CoordinatorRegistryCenter createRegistryCenter() {
    ZookeeperConfiguration zc = new ZookeeperConfiguration("192.168.1.200:9999", "my-job");zc.setConnectionTimeoutMilliseconds(40000);zc.setMaxRetries(5);CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zc);regCenter.init();return regCenter;}private static JobConfiguration createJobConfiguration() {
    String jobs = "0=看论文,1=做实验,2=打比赛,3=开组会,4=看书,5=其他";return JobConfiguration.newBuilder("MyOneOffJob", 6).shardingItemParameters(jobs).overwrite(true).build();}
}

输出如下图所示:
在这里插入图片描述

多次调度可以多次执行oneOffJobBootstrap.execute()ElasticJob的一次性调度就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。

  相关解决方案