2021-06-23

ElasticJob简单使用

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 = ......

原文转载:http://www.shaoqun.com/a/823678.html

跨境电商:https://www.ikjzd.com/

芒果店长:https://www.ikjzd.com/w/1533

1号团:https://www.ikjzd.com/w/2263

杨颜:https://www.ikjzd.com/w/1820


ElasticJob单点使用任务类publicclassBackupJobimplementsSimpleJob{publicvoidexecute(ShardingContextshardingContext){StringselectSql="select*fromresumewherestate='未归档'limit1";List<Map<St
houzz:https://www.ikjzd.com/w/236
2019 亚马逊美国站黑五网一优惠券以及Prime专享折扣提报指南:https://www.ikjzd.com/tl/107617
最近CPC效果是有多差,大家有体会,看这十招能不能帮上你!:https://www.ikjzd.com/tl/107618
男友拉我的手进他裤子 腿张开给我看看你下面:http://lady.shaoqun.com/a/247453.html
学长大人上课和我做 学长的巨大在我体内进进出出:http://lady.shaoqun.com/m/a/247506.html
亚马逊CPC 广告到底要用哪个:https://www.ikjzd.com/tl/107619
【春运提示】—防盗防骗要小心 - :http://www.30bags.com/a/408807.html
口述:男友喜欢在阳台亲热 我受不了男友阳台爱爱:http://lady.shaoqun.com/a/29809.html
男友让我穿着闺蜜的睡衣爱爱:http://lady.shaoqun.com/a/271241.html
美国夫妇在光天化日之下在公共汽车上发生性行为,男性逃脱了精神分裂症的指控:http://lady.shaoqun.com/a/384287.html
美国夫妇在光天化日之下在公共汽车上发生性行为,男性逃脱了精神分裂症的指控:http://www.30bags.com/a/451914.html
有图有真相:是博士生导师吴春明的错,他引诱女学生在学校性交易中获得成功:http://www.30bags.com/a/451915.html

No comments:

Post a Comment