Java中实现分布式定时任务的方法有很多种,下面我为您介绍一下常见的三种实现方式:
1. 使用Quartz实现分布式定时任务
Quartz是一个功能强大的定时任务框架,它可以支持分布式部署。下面展示Quartz实现分布式定时任务的步骤:
- 引入Quartz的依赖包,可以通过maven进行引入:
xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
- 创建Quartz的配置文件,这里主要是设置Quartz运行时的一些参数,如线程池大小、调度器实例等:
properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.dataSource = myDS
org.quartz.jobStore.nonManagedTXDataSource = myDS
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/quartz
org.quartz.dataSource.myDS.user = root
org.quartz.dataSource.myDS.password = root
org.quartz.dataSource.myDS.maxConnections = 5
这里需要配置数据库连接信息。
- 创建Job类,表示要执行的任务:
```java
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello, Quartz!");
}
}
```
- 创建Trigger类,表示触发任务的时间:
```java
public class MyTrigger {
public Trigger getTrigger() {
return TriggerBuilder.newTrigger()
.withIdentity("myTrigger", "myGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
}
}
```
这里使用的是Cron表达式,表示每间隔5秒执行一次任务。
- 启动Quartz定时任务调度器:
```java
public class MyScheduler {
public static void main(String[] args) throws SchedulerException {
JobDetail job = JobBuilder.newJob(MyJob.class)
.withIdentity("myJob", "myGroup")
.build();
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
scheduler.scheduleJob(job, new MyTrigger().getTrigger());
}
}
```
- 最后,我们需要在多个服务器上依次启动步骤5的程序,即可实现Quartz的分布式定时任务。
2. 使用Spring集成Quartz实现分布式定时任务
- 依赖包引入,这里介绍通过maven进行引入:
xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
-
创建Job类,表示要执行的任务。
-
创建Schedule类,配置定时任务的触发器,以及任务绑定的Job:
```java
public class MyScheduler {
@Autowired
private JobDetail myJobDetail;
@Autowired
private Trigger myTrigger;
@Autowired
private SchedulerFactory schedulerFactory;
@PostConstruct
public void init() throws SchedulerException {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.scheduleJob(myJobDetail, myTrigger);
scheduler.start();
}
}
```
- 最后,在多台服务器上添加相同的配置文件和代码,依次启动定时任务即可。
3. 使用Zookeeper +Dubbo +Quartz实现分布式定时任务
-
使用Dubbo搭建Zookeeper集群。
-
引入Dubbo、Zookeeper、Quartz等相关依赖:
xml
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>x.y.z</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>x.y.z</version>
</dependency>
- 创建Zookeeper连接、Dubbo注册等相关配置文件:
```properties
# Dubbo
dubbo.application.name=quartz-task
dubbo.registry.address=zk://192.168.1.100:2181,192.168.1.101:2181,192.168.1.102:2181
# Quartz
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource = quartzDataSource
org.quartz.jobStore.tablePrefix = qrtz_
org.quartz.dataSource.quartzDataSource.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.quartzDataSource.URL = jdbc:mysql://localhost:3306/quartz
org.quartz.dataSource.quartzDataSource.user = root
org.quartz.dataSource.quartzDataSource.password = root
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5
# Zookeeper
zookeeper.hosts=127.0.0.1:2181
zookeeper.timeout=5000
zookeeper.namespace=quartz-task
```
- 创建Task类,继承Quartz的Job类:
```java
public class MyTask implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Hello, Quartz!");
}
}
```
- 创建TaskManager类,使用Zookeeper获得可用的节点,为定时任务分配节点等:
```java
public class TaskManager {
private static final String ROOT_PATH = "/tasks";
private CuratorFramework client;
public void init() throws Exception {
InterProcessMutex mutex = new InterProcessMutex(client, ROOT_PATH);
if (mutex.acquire(10, TimeUnit.SECONDS)) {
// 获得可用节点,为定时任务分配节点
// ...
mutex.release();
}
}
}
```
- 创建启动器,启动我们的分布式定时任务系统:
```java
public class LauncherServer {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
applicationContext.start();
// 结束后,关闭Spring上下文
applicationContext.close();
}
}
```
- 最终,在控制台输入
mvn exec:java -Dexec.mainClass="com.xxx.LauncherServer"
执行程序,在Zookeeper中创建/leader节点,等待程序执行。
以上只是几种实现分布式定时任务的方式,注重的点也不尽相同,可以根据实际情况进行选择。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java中实现分布式定时任务的方法 - Python技术站