perf:动态调度schedule优化
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
<artifactId>xservice-basic</artifactId>
|
<artifactId>xservice-basic</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
<version>1.1-SNAPSHOT</version>
|
||||||
|
|
||||||
<artifactId>xservice-schedule-starter</artifactId>
|
<artifactId>xservice-schedule-starter</artifactId>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.xiang.xservice.schedule.config;
|
||||||
|
|
||||||
|
import com.xiang.xservice.schedule.service.IDynamicTaskSchedulerService;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
@AutoConfiguration
|
||||||
|
@Import({DynamicTaskSchedulerConfig.class, IDynamicTaskSchedulerService.class, TaskRegistry.class})
|
||||||
|
public class DynamicSchedulerAutoConfiguration {
|
||||||
|
}
|
||||||
@@ -10,7 +10,9 @@ public class DynamicTaskSchedulerConfig {
|
|||||||
public ThreadPoolTaskScheduler globalTaskScheduler() {
|
public ThreadPoolTaskScheduler globalTaskScheduler() {
|
||||||
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
||||||
scheduler.setPoolSize(10);
|
scheduler.setPoolSize(10);
|
||||||
scheduler.setThreadNamePrefix("task-scheduler-");
|
scheduler.setThreadNamePrefix("dynamic-task-");
|
||||||
|
scheduler.setWaitForTasksToCompleteOnShutdown(true);
|
||||||
|
scheduler.setAwaitTerminationSeconds(30);
|
||||||
scheduler.initialize();
|
scheduler.initialize();
|
||||||
return scheduler;
|
return scheduler;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.xiang.xservice.schedule.config;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public class TaskRegistry {
|
||||||
|
private final ConcurrentHashMap<Long, Consumer<Long>> handlers = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
public void register(Long taskId, Consumer<Long> consumer) {
|
||||||
|
handlers.put(taskId, consumer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run(Long taskId) {
|
||||||
|
Consumer<Long> consumer = handlers.get(taskId);
|
||||||
|
if (consumer != null) consumer.accept(taskId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregister(Long taskId) {
|
||||||
|
handlers.remove(taskId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.xiang.xservice.schedule.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class TaskConfig {
|
||||||
|
private Long taskId;
|
||||||
|
private String taskName;
|
||||||
|
private LocalDateTime executionTime;
|
||||||
|
}
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
package com.xiang.xservice.schedule.service;
|
package com.xiang.xservice.schedule.service;
|
||||||
|
|
||||||
|
import com.xiang.xservice.schedule.entity.TaskConfig;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@@ -17,19 +18,22 @@ public class DynamicTaskSchedulerServiceImpl implements IDynamicTaskSchedulerSer
|
|||||||
|
|
||||||
private final ThreadPoolTaskScheduler taskScheduler;
|
private final ThreadPoolTaskScheduler taskScheduler;
|
||||||
|
|
||||||
private final Map<String, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();
|
private final Map<Long, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void schedule(String taskId, LocalDateTime runTime, Runnable task) {
|
public void schedule(TaskConfig config, Runnable task) {
|
||||||
long delay = Duration.between(LocalDateTime.now(), runTime).toMillis();
|
LocalDateTime time = config.getExecutionTime();
|
||||||
if (delay < 0) {
|
if (time.isBefore(LocalDateTime.now())) return;
|
||||||
throw new IllegalArgumentException("时间已过,无法调度");
|
|
||||||
}
|
|
||||||
|
|
||||||
ScheduledFuture<?> future = taskScheduler.schedule(
|
ScheduledFuture<?> future = taskScheduler.schedule(() -> {
|
||||||
task, new Date(System.currentTimeMillis() + delay)
|
try {
|
||||||
);
|
task.run();
|
||||||
taskMap.put(taskId, future);
|
} finally {
|
||||||
|
taskMap.remove(config.getTaskId());
|
||||||
|
}
|
||||||
|
}, Date.from(time.atZone(ZoneId.systemDefault()).toInstant()));
|
||||||
|
|
||||||
|
taskMap.put(config.getTaskId(), future);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package com.xiang.xservice.schedule.service;
|
package com.xiang.xservice.schedule.service;
|
||||||
|
|
||||||
|
import com.xiang.xservice.schedule.entity.TaskConfig;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
public interface IDynamicTaskSchedulerService {
|
public interface IDynamicTaskSchedulerService {
|
||||||
void schedule(String taskId, LocalDateTime runTime, Runnable task);
|
void schedule(TaskConfig config, Runnable task);
|
||||||
void cancel(String taskId);
|
void cancel(String taskId);
|
||||||
Boolean contains(String taskId);
|
Boolean contains(String taskId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user