cloudroam
2025-07-05 18f59ad508af9d49705b579b5094251687a9e3dd
add:动态注册定时任务
已添加2个文件
131 ■■■■■ 文件已修改
src/main/java/com/mzl/flower/schedule/DynamicTaskManager.java 78 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mzl/flower/schedule/TaskExecutor.java 53 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mzl/flower/schedule/DynamicTaskManager.java
对比新文件
@@ -0,0 +1,78 @@
package com.mzl.flower.schedule;
import com.mzl.flower.entity.film.AiContentTaskConfig;
import com.mzl.flower.service.film.AiContentTaskConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.stream.Collectors;
@Component
public class DynamicTaskManager {
    private final Map<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();
    @Autowired
    private ThreadPoolTaskScheduler taskScheduler;
    @Autowired
    private AiContentTaskConfigService configService;
    @Autowired
    private TaskExecutor taskExecutor;
    @PostConstruct
    public void init() {
        refreshTasks();
    }
    public synchronized void refreshTasks() {
        List<AiContentTaskConfig> latestConfigs = configService.getEnabledConfigs();
        Set<Long> latestIds = latestConfigs.stream()
                .map(AiContentTaskConfig::getId)
                .collect(Collectors.toSet());
        // 移除无效任务
        new HashSet<>(scheduledTasks.keySet()).forEach(id -> {
            if (!latestIds.contains(id)) {
                cancelAndRemoveTask(id);
            }
        });
        // 更新/新增任务
        latestConfigs.forEach(config -> {
            ScheduledFuture<?> existingTask = scheduledTasks.get(config.getId());
            if (existingTask == null) {
                registerNewTask(config);
            } else if (isConfigModified(config)) {
                cancelAndRemoveTask(config.getId());
                registerNewTask(config);
            }
        });
    }
    private void registerNewTask(AiContentTaskConfig config) {
        ScheduledFuture<?> future = taskScheduler.schedule(
                () -> taskExecutor.executeTask(config),
                new CronTrigger(config.getCron())
        );
        scheduledTasks.put(config.getId(), future);
    }
    private void cancelAndRemoveTask(Long taskId) {
        ScheduledFuture<?> task = scheduledTasks.get(taskId);
        if (task != null) {
            task.cancel(false); // 不中断正在执行的任务
            scheduledTasks.remove(taskId);
        }
    }
    private boolean isConfigModified(AiContentTaskConfig newConfig) {
        // 实现配置变更检查逻辑(比较字段或版本号)
        return true; // 简化示例
    }
}
src/main/java/com/mzl/flower/schedule/TaskExecutor.java
对比新文件
@@ -0,0 +1,53 @@
package com.mzl.flower.schedule;
import com.mzl.flower.entity.film.AiContentTaskConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.Date;
import org.springframework.stereotype.Component;
@Component
public class TaskExecutor {
    private final RestTemplate restTemplate;
    @Autowired
    public TaskExecutor(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }
    public void executeTask(AiContentTaskConfig config) {
        String paramsJson = config.getAiParams();
        System.out.println("发送的 JSON 参数: " + paramsJson); // 添加这行
        System.out.println("定时任务执行 - 时间: " + new Date());
        try {
//            String aiResult = callPythonAiService(paramsJson);
//            System.out.println("AI 服务返回结果: " + aiResult);
            System.out.println("开始执行调用AI服务: ");
            // 这里处理结果入库逻辑...
        } catch (Exception e) {
            System.err.println("调用 AI 服务失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    public String callPythonAiService(String paramsJson) {
        String url = "http://127.0.0.1:5000/crawl-douban";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(paramsJson, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        return response.getBody();
    }
}