添加依赖(​​3.0.1​​​是目前最新的​​Releases​​版本):

<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-core</artifactId>
<version>3.0.1</version>
</dependency>

​ElasticJob​​​提供了事件追踪功能,可通过事件订阅的方式处理调度过程的重要事件,用于查询、统计和监控。目前提供了基于关系型数据库的事件订阅方式记录事件,开发者也可以通过​​SPI​​​自行扩展,博主在下面这篇博客中介绍了如何通过​​SPI​​自行扩展作业类型,方式是类似的。

  • ​​ElasticJob‐Lite:扩展作业类型​​

​pom.xml​​:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kaven</groupId>
<artifactId>job</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.shardingsphere.elasticjob</groupId>
<artifactId>elasticjob-lite-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

作业定义(以​​Simple​​作业为例):

package com.kaven.job;

import lombok.SneakyThrows;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Author: ITKaven
* @Date: 2021/11/20 17:02
* @Leetcode: https://leetcode-cn.com/u/kavenit
* @Notes:
*/
public class MySimpleJob implements SimpleJob {
private static final SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@SneakyThrows
@Override
public void execute(ShardingContext shardingContext) {
switch (shardingContext.getShardingItem()) {
case 0:
System.out.println(formatter.format(new Date()) + " : ShardingItem[0]");
Thread.sleep(2000);
break;
case 1:
System.out.println(formatter.format(new Date()) + " : ShardingItem[1]");
Thread.sleep(2000);
break;
case 2:
System.out.println(formatter.format(new Date()) + " : ShardingItem[2]");
Thread.sleep(2000);
break;
default:
System.out.println(formatter.format(new Date()) + " : Unknown ShardingItem");
}
}
}

启动类​​Application​​ :

package com.kaven.job;

import com.mysql.cj.jdbc.MysqlDataSource;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.ScheduleJobBootstrap;
import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;

import javax.sql.DataSource;

/**
* @Author: ITKaven
* @Date: 2021/11/20 17:05
* @Leetcode: https://leetcode-cn.com/u/kavenit
* @Notes:
*/
public class Application {

public static void main(String[] args) {
new ScheduleJobBootstrap(createRegistryCenter(), new MySimpleJob(),
createJobConfiguration()).schedule();
}

// 注册中心
private static CoordinatorRegistryCenter createRegistryCenter() {
ZookeeperConfiguration zc = new ZookeeperConfiguration("192.168.31.172:9000", "my-job");
zc.setConnectionTimeoutMilliseconds(40000);
zc.setMaxRetries(5);
CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zc);
regCenter.init();
return regCenter;
}

// 数据源
private static DataSource getDataSource() {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("root");
dataSource.setPassword("ITkaven@666.com");
dataSource.setURL("jdbc:mysql://localhost:3306/trace?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
return dataSource;
}

// 作业配置
private static JobConfiguration createJobConfiguration() {
TracingConfiguration tracingConfig = new TracingConfiguration<>("RDB", getDataSource());
return JobConfiguration.newBuilder("MySimpleJob", 3)
.description("该作业有三个分片,每隔一分钟执行一次")
.cron("30 * * * * ?")
.addExtraConfigurations(tracingConfig)
.overwrite(true)
.failover(true)
.build();
}
}

执行作业后,数据库就会有作业的执行记录。

ElasticJob‐Lite:事件追踪_apache


​ElasticJob‐Lite​​的事件追踪就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。