ElasticJob单点使用

任务类

public class BackupJob implements SimpleJob {public void execute(ShardingContext shardingContext) {String selectSql = "select * from resume where state='未归档' limit 1";List<Map<String, Object>> list =JdbcUtil.executeQuery(selectSql);if(list == null || list.size() == 0) {return;}Map<String, Object> stringObjectMap = list.get(0);long id = (long) stringObjectMap.get("id");String name = (String) stringObjectMap.get("name");String education = (String)stringObjectMap.get("education");
// 打印出这条记录System.out.println("======>>>id:" + id + " name:" +name + " education:" + education);
// 更改状态String updateSql = "update resume set state='已归档' where id=?";JdbcUtil.executeUpdate(updateSql,id);
// 归档这条记录String insertSql = "insert into resume_bak select * from resume where id=?";JdbcUtil.executeUpdate(insertSql,id);}}

主要的任务就是将未归档的数据整理到归档的表中,表结构一样 执行类

public class JobMain {public static void main(String[] args) {//初始化注册中心ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("127.0.0.1:2181","data-job");CoordinatorRegistryCenter coordinatorRegistryCenter= new ZookeeperRegistryCenter(zookeeperConfiguration);coordinatorRegistryCenter.init();//创建任务JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("data-job","*/2 * * * * ?",1).build();SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration,BackupJob.class.getName());//执行任务new JobScheduler(coordinatorRegistryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).build()).init();}
}

这种情况下,启动两个任务类只会有一个在执行任务。但是当一个任务停止之后,另一个任务会立马开始接着执行任务,相当于其他中间件中的主备切换。但是这里的主备切换是依托zk进行的

多节点分布式任务调度

修改执行类的代码为

public class JobMain {public static void main(String[] args) {//初始化注册中心ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("127.0.0.1:2181","data-job");CoordinatorRegistryCenter coordinatorRegistryCenter= new ZookeeperRegistryCenter(zookeeperConfiguration);coordinatorRegistryCenter.init();//创建任务JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("data-job","*/2 * * * * ?",3).build();SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration,BackupJob.class.getName());//执行任务new JobScheduler(coordinatorRegistryCenter, LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build()).init();}
}

除了修改分片数还需要在执行任务的类中执行相应的分片参数,另外需要注意的是仅仅增加分票策略是不生效的,必须同时配置分片参数。另外如果使用同一个job来做执行的话。需要增加overwrite为true 执行器代码为 ``` public class BackupJob implements SimpleJob { public void execute(ShardingContext shardingContext) { int shardingitem = shardingContext.getShardingItem(); System.out.println("当前分片"+shardingitem); String shardingParamter = shardingContext.getShardingParameter(); System.out.println(shardingParamter); String selectSql = "select * from resume where state='未归档' and name='"+shardingParamter+"' limit 1"; List<Map<String, Object>> list = JdbcUtil.executeQuery(selectSql); if(list == null || list.size() == 0) { return; } Map<String, Object> stringObjectMap = list.get(0); long id = (long) stringObjectMap.get("id"); String name = (String) stringObjectMap.get("name"); String education = (String) stringObjectMap.get("education"); // 打印出这条记录 System.out.println("======>>>id:" + id + " name:" + name + " education:" + education); // 更改状态 String updateSql = "update resume set state='已归档' where id=?"; JdbcUtil.executeUpdate(updateSql,id); // 归档这条记录 String insertSql = "insert into resume_bak select * from resume where id=?"; JdbcUtil.executeUpdate(insertSql,id); }

} ``` 测试结果为,当执行器未全部启动时,所有分片在一个执行器上运行。当三个执行器都启动时,会平均分配到三个执行器。

demo代码地址为https://github.com/zhendiao/deme-code/tree/main/schedule_job

欢迎搜索关注本人的公众号【微瞰技术】,以及总结的分类面试题https://github.com/zhendiao/JavaInterview

ElasticJob简单使用相关推荐

  1. elastic-job 的简单使用

    说明:这个是使用2.1.5版本 elastic-job是当当开源的的的定时任务,使用也是很简单的,可以解决数据量的大的时候可以分片执行,多应用节点部署时候不会重复执行. 是通过zookeeper作为控 ...

  2. 【elastic-job】elastic-job部署以及简单例子

    一.elastic-job是什么 elastic-job是当当开发的基于qutarz以及zookeeper封装的作业调度工具,主要有两个大框架,一个是elastic-job lite另外一个是elas ...

  3. xxl-job Vs ElasticJob,谁牛?

    欢迎关注方志朋的博客,回复"666"获面试宝典 1. xxl-job 2. 运行 xxl-job 3. 开发定时任务 3.1 项目创建及配置 3.2 定时任务开发方式 4. 小结 ...

  4. 王者归来:分布式调度解决方案 ElasticJob 重启!

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 你会误认为 ElasticJob 只是作业管控平台么?创 ...

  5. elastic-job详解(一):数据分片

    数据分片的目的在于把一个任务分散到不同的机器上运行,既可以解决单机计算能力上限的问题,也能降低部分任务失败对整体系统的影响.elastic-job并不直接提供数据处理的功能,框架只会将分片项分配至各个 ...

  6. springboot整合elasticJob实战(纯代码开发三种任务类型用法)以及分片系统,事件追踪详解...

    一 springboot整合 介绍就不多说了,只有这个框架是当当网开源的,支持分布式调度,分布式系统中非常合适(两个服务同时跑不会重复,并且可灵活配置分开分批处理数据,贼方便)! 这里主要还是用到zo ...

  7. elastic-job的原理简介和使用

    elastic-job是当当开源的一款非常好用的作业框架,在这之前,我们开发定时任务一般都是使用quartz或者spring-task(ScheduledExecutorService),无论是使用q ...

  8. java定时任务框架elasticjob详解

    这篇文章主要介绍了java定时任务框架elasticjob详解,Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.该项目基于成熟的开源产品Quartz和Zo ...

  9. elastic-job 和 xxl-job 的对比

    综合对比 许雪里 软件作者 05/23 18:47 #xxl-job# 即将发布的新版本,将会自研触发组件,移除quartz依赖,并精简掉11张表,大大降低线程和内存开销.在第三方依赖上不断做减法,在 ...

  10. Elastic-Job简介

    针对分布式任务调度的需求,市场上出现了很多的产品: 1) TBSchedule:淘宝推出的一款非常优秀的高性能分布式调度框架,目前被应用于阿里.京东.支付宝.国美等很多互联网企业的流程调度系统中.但是 ...

最新文章

  1. oracle 工具:tkprof
  2. Python地信专题 | 基于geopandas的空间数据分析-文件IO篇
  3. AI基础:Python开发环境设置和小技巧
  4. 红外摄像机的功率究竟有多大
  5. java用if判断输入字符_java怎么用if判断输入的是不是数字
  6. c#获取当前时间 毫秒_《Linux设备驱动程序》(十二)——时间操作(一)
  7. 前端开发 css样式的简写
  8. emui内核支持kvm吗_KVM虚拟化详解
  9. 多核Cache一致性 伪共享 atomic的实现和cache相关的部分
  10. 将Matlab程序打包成.exe独立可执行程序
  11. 台达变频器485通讯接线图_台达变频器RS485通讯设置
  12. Android系统架构
  13. 将表中一整列数据都生成五笔和拼音
  14. android gridview textview,gogo体育官方网站-gogo体育官方网站
  15. 空格键除了敲空格外的多种用途
  16. 设计评审CheckList
  17. 一种留存分析的方案:Cohort Analysis
  18. NR基础篇上——均值滤波、高斯滤波、双边滤波、NLM
  19. Python的大数据之旅(1)---Anaconda与WingIDE安装
  20. EOS星球秒杀所有的区块链游戏

热门文章

  1. 最近整理电脑硬盘,分享几个小巧实用的软件下载,持续更新
  2. 反向传播神经网络(Back propagation neural network ,BPNN)
  3. jsp余jspx的区别
  4. stc单片机id加密c语言,STC单片机使用加密芯片SMEC98SP的加密实例源码
  5. Webshell 管理工具
  6. python 典型相关分析_典型关联分析(CCA)原理
  7. 人脸识别技术软件测试测什么,人脸识别这么火,你知道它是什么吗?
  8. 双态运维联盟首个“共研基地”落户云南电网信息中心
  9. Directx9下载安装
  10. 电力拖动计算机系统考试,电力拖动自动控制系统__考试复习题.doc