mirror of
https://gitee.com/devlive-community/datacap.git
synced 2024-11-29 18:48:23 +08:00
[Core] [Lib] Add schedule lib
This commit is contained in:
parent
98833006e5
commit
33a49f0157
31
lib/datacap-schedule/pom.xml
Normal file
31
lib/datacap-schedule/pom.xml
Normal file
@ -0,0 +1,31 @@
|
||||
<?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>
|
||||
<parent>
|
||||
<groupId>io.edurt.datacap</groupId>
|
||||
<artifactId>datacap</artifactId>
|
||||
<version>1.8.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>datacap-schedule</artifactId>
|
||||
<description>DataCap - Schedule</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.edurt.datacap</groupId>
|
||||
<artifactId>datacap-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,78 @@
|
||||
package io.edurt.datacap.schedule;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.config.CronTask;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Component
|
||||
public class ScheduledCronRegistrar
|
||||
implements DisposableBean
|
||||
{
|
||||
private final Map<Runnable, ScheduledTask> scheduledTasks = new ConcurrentHashMap<>(16);
|
||||
|
||||
private final TaskScheduler taskScheduler;
|
||||
|
||||
public ScheduledCronRegistrar(TaskScheduler taskScheduler)
|
||||
{
|
||||
this.taskScheduler = taskScheduler;
|
||||
}
|
||||
|
||||
public TaskScheduler getScheduler()
|
||||
{
|
||||
return this.taskScheduler;
|
||||
}
|
||||
|
||||
public void addCronTask(Runnable task, String cronExpression)
|
||||
{
|
||||
addCronTask(new CronTask(task, cronExpression));
|
||||
}
|
||||
|
||||
public void addCronTask(CronTask cronTask)
|
||||
{
|
||||
if (ObjectUtils.isNotEmpty(cronTask)) {
|
||||
Runnable task = cronTask.getRunnable();
|
||||
if (this.scheduledTasks.containsKey(task)) {
|
||||
removeCronTask(task);
|
||||
}
|
||||
this.scheduledTasks.put(task, scheduleCronTask(cronTask));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeCronTask(Runnable task)
|
||||
{
|
||||
ScheduledRunnable sourceScheduledRunnable = (ScheduledRunnable) task;
|
||||
Optional<Map.Entry<Runnable, ScheduledTask>> findScheduledRunnable = this.scheduledTasks.entrySet()
|
||||
.stream()
|
||||
.filter(v -> {
|
||||
ScheduledRunnable targetScheduledRunnable = (ScheduledRunnable) v.getKey();
|
||||
return targetScheduledRunnable.getName().equals(sourceScheduledRunnable.getName());
|
||||
})
|
||||
.findFirst();
|
||||
if (findScheduledRunnable.isPresent()) {
|
||||
findScheduledRunnable.get().getValue().cancel();
|
||||
this.scheduledTasks.remove(findScheduledRunnable.get().getKey());
|
||||
}
|
||||
}
|
||||
|
||||
public ScheduledTask scheduleCronTask(CronTask cronTask)
|
||||
{
|
||||
ScheduledTask scheduledTask = new ScheduledTask();
|
||||
scheduledTask.future = this.taskScheduler.schedule(cronTask.getRunnable(), cronTask.getTrigger());
|
||||
return scheduledTask;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy()
|
||||
{
|
||||
for (ScheduledTask task : this.scheduledTasks.values()) {
|
||||
task.cancel();
|
||||
}
|
||||
this.scheduledTasks.clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package io.edurt.datacap.schedule;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class ScheduledRunnable
|
||||
implements Runnable
|
||||
{
|
||||
@Getter
|
||||
private final String name;
|
||||
|
||||
public ScheduledRunnable(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
log.info("Scheduled created");
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package io.edurt.datacap.schedule;
|
||||
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
public final class ScheduledTask
|
||||
{
|
||||
volatile ScheduledFuture<?> future;
|
||||
|
||||
/**
|
||||
* cancel the current task
|
||||
*/
|
||||
public void cancel()
|
||||
{
|
||||
ScheduledFuture<?> future = this.future;
|
||||
if (ObjectUtils.isNotEmpty(future)) {
|
||||
future.cancel(true);
|
||||
}
|
||||
}
|
||||
}
|
1
pom.xml
1
pom.xml
@ -15,6 +15,7 @@
|
||||
<module>lib/datacap-http</module>
|
||||
<module>lib/datacap-logger</module>
|
||||
<module>lib/datacap-shell</module>
|
||||
<module>lib/datacap-schedule</module>
|
||||
<module>driver/datacap-driver-redis</module>
|
||||
<module>plugin/datacap-native-alioss</module>
|
||||
<module>plugin/datacap-native-zookeeper</module>
|
||||
|
Loading…
Reference in New Issue
Block a user