1、引入jar
com.dangdang
elastic-job-lite-core
2.1.5
2、任务逻辑
public class ArchivieJob implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
//1、从原表中查询出1条记录(未归档)
String selectSql = "select * from resume where state = '未归档' limit 1";
List
public class JDBCUtil {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://101.132.167.18:3306/mydatabase";
private static String username = "root";
private static String password = "root";
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static List
建表,初始化数据
create table resume(id long,name varchar(10),education varchar(10),state varchar(10));
insert into resume values(1,'张三','博士','未归档');
insert into resume values(2,'张二','博士','未归档');
insert into resume values(3,'李四','硕士','未归档');
insert into resume values(4,'李六','硕士','未归档');
insert into resume values(5,'王六','硕士','未归档');
insert into resume values(6,'王无','硕士','未归档');
insert into resume values(7,'赵六','本科','未归档');
insert into resume values(8,'赵八','本科','未归档');
insert into resume values(9,'小刚','本科','未归档');
insert into resume values(10,'小亮','本科','未归档');
insert into resume values(11,'小明','本科','未归档');
insert into resume values(12,'小花','专科','未归档');
insert into resume values(13,'龙龙','专科','未归档');
insert into resume values(14,'蓓蓓','博科','未归档');
insert into resume values(15,'阿珍','博科','未归档');
insert into resume values(16,'阿强','博士、','未归档');
insert into resume values(17,'杰克','博士、','未归档');
insert into resume values(18,'汤姆','博士、','未归档');
insert into resume values(19,'山姆','博士、','未归档');
insert into resume values(20,'山羊','博士、','未归档');
3、执行器
public class ElasticJobMain {
public static void main(String[] args) {
//配置分布式协调服务中心(zookeeper)
ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration("101.132.167.18:2181","data-archive-job");
CoordinatorRegistryCenter coordinatorRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
coordinatorRegistryCenter.init();
//配置任务(时间事件、定时任务业务逻辑、调度器)
JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder("archive-job","*/2 * * * * ?",1).build();
SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration,ArchivieJob.class.getName());
//创建LiteJobConfiguration
LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration).overwrite(true).build();
new JobScheduler(coordinatorRegistryCenter,liteJobConfiguration).init();
}
}
备注:可以使用 zooinspector 工具查看 zookeeper ,解压 zooinspector 安装包,执行
java -jar zookeeper-dev-ZooInspector.jar