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