Java Spring Boot中的多线程cron作业

Java Spring Boot中的多线程cron作业,java,multithreading,spring-boot,cron,Java,Multithreading,Spring Boot,Cron,我正在开发一个Spring Boot应用程序,它在给定的网站中查找给定的关键字,如果找到匹配项,就会删除网页。我正在编写一个cron作业,每5分钟刷新一次结果,如下所示: @Scheduled(cron = "* */5 * * * *") public void fetchLatestResults() throws Exception { LOG.debug("Fetching latest results >>>"); List<Keyword>

我正在开发一个Spring Boot应用程序,它在给定的网站中查找给定的
关键字
,如果找到匹配项,就会删除网页。我正在编写一个cron作业,每5分钟刷新一次结果,如下所示:

@Scheduled(cron = "* */5 * * * *")
public void fetchLatestResults() throws Exception {
    LOG.debug("Fetching latest results >>>");
    List<Keyword> keywords = keywordService.findOldestSearched10();
    keywordService.updateLastSearchDate(keywords);
    searchResultService.fetchLatestResults(keywords);
    LOG.debug("<<< Latest results fetched");
}
@Scheduled(cron=“***/5****”)
public void fetchLatestResults()引发异常{
LOG.debug(“获取最新结果>>”;
List keywords=keywordService.findOldestSearched10();
关键字服务.updateLastSearchDate(关键字);
searchResultService.fetchLatestResults(关键字);

debug(“我建议您使cron作业的执行异步化

创建将创建新线程以运行cron作业的
executor
类:

@Component
public class YourCronJobExecutor {

    private int threadsNumber = 10;
    private ExecutorService executorService;

    @PostConstruct
    private void init() {
        executorService = Executors.newFixedThreadPool(threadsNumber);
    }

    /**
     * Start.
     * @param runnable - runnable instance.
     */
    public void start(Runnable runnable) {
        try {
            executorService.execute(runnable);
        } catch (RejectedExecutionException e) {
            init();
            executorService.execute(runnable);
        }
    }
}
@Component
public class CronJobProcessor {

    //logger
    //autowired beans

    public void executeYouCronJob() {
        LOG.debug("Fetching latest results >>>");
        List<Keyword> keywords = keywordService.findOldestSearched10();
        keywordService.updateLastSearchDate(keywords);
        searchResultService.fetchLatestResults(keywords);
        LOG.debug("<<< Latest results fetched");
    }
}
创建一个
处理器
类,该类将包含cron作业的逻辑:

@Component
public class YourCronJobExecutor {

    private int threadsNumber = 10;
    private ExecutorService executorService;

    @PostConstruct
    private void init() {
        executorService = Executors.newFixedThreadPool(threadsNumber);
    }

    /**
     * Start.
     * @param runnable - runnable instance.
     */
    public void start(Runnable runnable) {
        try {
            executorService.execute(runnable);
        } catch (RejectedExecutionException e) {
            init();
            executorService.execute(runnable);
        }
    }
}
@Component
public class CronJobProcessor {

    //logger
    //autowired beans

    public void executeYouCronJob() {
        LOG.debug("Fetching latest results >>>");
        List<Keyword> keywords = keywordService.findOldestSearched10();
        keywordService.updateLastSearchDate(keywords);
        searchResultService.fetchLatestResults(keywords);
        LOG.debug("<<< Latest results fetched");
    }
}
这样,cron作业的执行将需要几毫秒的时间,一个单独的线程将实际执行该作业,它将根据需要运行多长时间


但是,您可能希望在一个单独的线程中执行每个关键字的搜索,但情况有点不同。

这非常有效,并提高了性能。谢谢。