esjob的作业类型有三种,分别是Simple、Dataflow和Script。本文主要介绍基于springboot的simple类型的作业。以及控制台的使用。作业名称为simpleJobDemo.
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.elasticjob
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
elastic-job-common-core
com.dangdang
2.1.5
elastic-job-lite-core
com.dangdang
2.1.5
elastic-job-lite-spring
com.dangdang
2.1.5
org.projectlombok
lombok
1.16.18
provided
org.springframework.boot
spring-boot-maven-plugin
# 端口
server:
port: 8080
# spring配置相关
spring:
# 应用名称
application:
name: elastic-job-demo
# zk注册中心配置相关
reg-center:
# 连接Zookeeper服务器的列表IP:端口号,多个地址用逗号分隔
server-list: "IP:2181,IP,IP:2183"
# Zookeeper的命名空间
namespace: zk-elastic-job
# 等待重试的间隔时间的初始值 默认1000,单位:毫秒
baseSleepTimeMilliseconds: 1000
# 等待重试的间隔时间的最大值 默认3000,单位:毫秒
maxSleepTimeMilliseconds: 3000
# 最大重试次数 默认3
maxRetries: 3
# 会话超时时间 默认60000,单位:毫秒
sessionTimeoutMilliseconds: 60000
# 连接超时时间 默认15000,单位:毫秒
connectionTimeoutMilliseconds: 15000
# 连接Zookeeper的权限令牌 默认不需要
#digest:
# simpleJobDemo作业配置相关
simpleJobDemo:
# cron表达式,用于控制作业触发时间
cron: 0/30 * * * * ?
# 作业分片总数
sharding-total-count: 3
# 分片序列号和参数用等号分隔,多个键值对用逗号分隔.分片序列号从0开始,不可大于或等于作业分片总数
sharding-item-parameters: 0=上海,1=北京,2=深圳
# 作业自定义参数,可通过传递该参数为作业调度的业务方法传参,用于实现带参数的作业
job-parameter: "simpleJobDemo作业参数"
# 是否开启任务执行失效转移,开启表示如果作业在一次任务执行中途宕机,允许将该次未完成的任务在另一作业节点上补偿执行, 默认为false
failover: true
# 是否开启错过任务重新执行 默认为true
misfire: true
# 作业描述信息
job-description: "simpleJobDemo作业描述"
注:此处配置了zk注册中心,然后配置了一个名为simpleJobDemo的simle类型的作业
package com.elasticjob.demo.config.esjob;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* zk注册中心配置
*/
@Configuration
public class ZkRegistryCenterConfig {
@Value("${reg-center.server-list}")
private String serverList;
@Value("${reg-center.namespace}")
private String namespace;
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter() {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
}
注: 此处配置了zk注册中心。如果zookeeper不会搭建的话可以参考博主的另一篇文章 linux下安装zookeeper集群 。
package com.elasticjob.demo.config.esjob;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 全局作业配置中心
*/
@Configuration
public class JobConfig {
@Autowired
private ZookeeperRegistryCenter regCenter;
@Autowired
private LiteJobConfiguration liteJobConfiguration;
@Autowired
private SimpleJobDemo simpleJobDemo;
@Bean(initMethod = "init")
public JobScheduler simpleJobScheduler() {
return new SpringJobScheduler(simpleJobDemo, regCenter, liteJobConfiguration);
}
}
package com.elasticjob.demo.taskjob.simplejob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* SimpleJobDemo作业配置
*/
@Configuration
public class SimpleJobDemoJobProperties {
@Value("${simpleJobDemo.cron}")
private String cron;
@Value("${simpleJobDemo.sharding-total-count}")
private int shardingTotalCount;
@Value("${simpleJobDemo.sharding-item-parameters}")
private String shardingItemParameters;
@Value("${simpleJobDemo.job-description}")
private String jobDescription;
@Value("${simpleJobDemo.job-parameter}")
private String jobParameter;
@Autowired
private SimpleJobDemo simpleJobDemo;
@Bean
public LiteJobConfiguration liteJobConfiguration() {
JobCoreConfiguration.Builder builder =
JobCoreConfiguration.newBuilder(simpleJobDemo.getClass().getName(), cron, shardingTotalCount);
JobCoreConfiguration jobCoreConfiguration = builder
.shardingItemParameters(shardingItemParameters)
.description(jobDescription)
.jobParameter(jobParameter)
.build();
SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, simpleJobDemo.getClass().getCanonicalName());
return LiteJobConfiguration
.newBuilder(simpleJobConfiguration)
.overwrite(true)
.build();
}
}
package com.elasticjob.demo.taskjob.simplejob;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class SimpleJobDemo implements SimpleJob {
@Override
public void execute(ShardingContext shardingContext) {
log.info("shardingContext的信息为:" + shardingContext);
switch (shardingContext.getShardingItem()) {
case 0:
System.out.println("上海分片执行的任务:求和1到3完成,结果为:6");
break;
case 1:
System.out.println("北京分片执行的任务:求和4到7完成,结果为:22");
break;
case 2:
System.out.println("深圳分片执行的任务:求和8到10完成,结果为:27");
break;
}
}
}
2019-05-23 15:19:30.382 INFO 15492 --- [SimpleJobDemo-1] c.e.d.taskjob.simplejob.SimpleJobDemo : shardingContext的信息为:ShardingContext(jobName=com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo, taskId=com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo@-@0,1,2@-@READY@[email protected]@-@15492, shardingTotalCount=3, jobParameter=simpleJobDemo作业参数, shardingItem=0, shardingParameter=上海)
上海分片执行的任务:求和1到3完成,结果为:6
2019-05-23 15:19:30.383 INFO 15492 --- [SimpleJobDemo-2] c.e.d.taskjob.simplejob.SimpleJobDemo : shardingContext的信息为:ShardingContext(jobName=com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo, taskId=com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo@-@0,1,2@-@READY@[email protected]@-@15492, shardingTotalCount=3, jobParameter=simpleJobDemo作业参数, shardingItem=1, shardingParameter=北京)
北京分片执行的任务:求和4到7完成,结果为:22
2019-05-23 15:19:30.385 INFO 15492 --- [SimpleJobDemo-3] c.e.d.taskjob.simplejob.SimpleJobDemo : shardingContext的信息为:ShardingContext(jobName=com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo, taskId=com.elasticjob.demo.taskjob.simplejob.SimpleJobDemo@-@0,1,2@-@READY@[email protected]@-@15492, shardingTotalCount=3, jobParameter=simpleJobDemo作业参数, shardingItem=2, shardingParameter=深圳)
深圳分片执行的任务:求和8到10完成,结果为:27
提交之后全局配置上面会变为1,0. 如下图:
此时,点击链接,则如下图:检测到了一个作业:
点击作业维度,如下图:
注: 可以看到有好多操作。都可以操作。此时状态是正常,如果服务关停,则过段时间刷新后状态会变味已下线。
查看作业详情页:
注: 在此处,可以看到作业的执行情况。等待运行,运行中等。