perf:动态调度schedule优化

This commit is contained in:
xiang
2025-07-27 14:11:15 +08:00
parent c59007745a
commit d96ee51b29
7 changed files with 69 additions and 13 deletions

View File

@@ -8,6 +8,7 @@
<artifactId>xservice-basic</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<version>1.1-SNAPSHOT</version>
<artifactId>xservice-schedule-starter</artifactId>

View File

@@ -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 {
}

View File

@@ -10,7 +10,9 @@ public class DynamicTaskSchedulerConfig {
public ThreadPoolTaskScheduler globalTaskScheduler() {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(10);
scheduler.setThreadNamePrefix("task-scheduler-");
scheduler.setThreadNamePrefix("dynamic-task-");
scheduler.setWaitForTasksToCompleteOnShutdown(true);
scheduler.setAwaitTerminationSeconds(30);
scheduler.initialize();
return scheduler;
}

View File

@@ -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);
}
}

View File

@@ -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;
}

View File

@@ -1,11 +1,12 @@
package com.xiang.xservice.schedule.service;
import com.xiang.xservice.schedule.entity.TaskConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -17,19 +18,22 @@ public class DynamicTaskSchedulerServiceImpl implements IDynamicTaskSchedulerSer
private final ThreadPoolTaskScheduler taskScheduler;
private final Map<String, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();
private final Map<Long, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();
@Override
public void schedule(String taskId, LocalDateTime runTime, Runnable task) {
long delay = Duration.between(LocalDateTime.now(), runTime).toMillis();
if (delay < 0) {
throw new IllegalArgumentException("时间已过,无法调度");
}
public void schedule(TaskConfig config, Runnable task) {
LocalDateTime time = config.getExecutionTime();
if (time.isBefore(LocalDateTime.now())) return;
ScheduledFuture<?> future = taskScheduler.schedule(
task, new Date(System.currentTimeMillis() + delay)
);
taskMap.put(taskId, future);
ScheduledFuture<?> future = taskScheduler.schedule(() -> {
try {
task.run();
} finally {
taskMap.remove(config.getTaskId());
}
}, Date.from(time.atZone(ZoneId.systemDefault()).toInstant()));
taskMap.put(config.getTaskId(), future);
}
@Override

View File

@@ -1,9 +1,11 @@
package com.xiang.xservice.schedule.service;
import com.xiang.xservice.schedule.entity.TaskConfig;
import java.time.LocalDateTime;
public interface IDynamicTaskSchedulerService {
void schedule(String taskId, LocalDateTime runTime, Runnable task);
void schedule(TaskConfig config, Runnable task);
void cancel(String taskId);
Boolean contains(String taskId);
}