Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring任务调度器与Java';s ScheduledExecutorService_Java_Multithreading_Spring_Spring Mvc - Fatal编程技术网

Spring任务调度器与Java';s ScheduledExecutorService

Spring任务调度器与Java';s ScheduledExecutorService,java,multithreading,spring,spring-mvc,Java,Multithreading,Spring,Spring Mvc,我需要创建一个包含10个线程的任务调度程序,我们需要同时启动这些线程,每个线程都会返回一个状态为“完成”或“失败”。根据线程的结果,我们将进行db调用并从db中获取数据。应用程序已经配置了Spring框架。我知道spring提供了任务调度器,但不知道如何使用它,spring新手需要帮助。java的ScheduledExecutorService怎么样,我们可以使用它吗?我们一个比另一个有什么优势?Spring任务调度器和Java的ScheduledExecutorService还有更好的替代方案

我需要创建一个包含10个线程的任务调度程序,我们需要同时启动这些线程,每个线程都会返回一个状态为“完成”或“失败”。根据线程的结果,我们将进行db调用并从db中获取数据。应用程序已经配置了Spring框架。我知道spring提供了任务调度器,但不知道如何使用它,spring新手需要帮助。java的ScheduledExecutorService怎么样,我们可以使用它吗?我们一个比另一个有什么优势?Spring任务调度器和Java的ScheduledExecutorService还有更好的替代方案吗?

Spring
TaskExecutor
实际上与Java
Executor
接口相同。Spring2.0之后,引入了
TaskExecutor
,将抽象添加到Java的
Executor
,这样它将隐藏Java SE不同版本和EE环境之间的实现细节

由于您已经有了Spring环境,我强烈建议您使用Spring。稍后,如果需要,您可以为其他Spring组件提供线程池等的抽象


此外,还有一些TaskExecutor的预构建实现,这非常理想,因为您不必自己关心细节和实现。

最简单的方法是在spring配置中使用提供的任务标记。 请注意下面的“任务”命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:ctx="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
">

一旦你做到了,你就可以使用

<task:scheduler id="taskScheduler" pool-size="4"/>
<task:scheduled-tasks scheduler="taskScheduler">
   <task:scheduled ref="someBean" method="someMethod" fixed-rate="21600000" initial-delay="60000"/>
</task:scheduled-tasks>

等等。 您的实际计划任务是一个bean,其上有一个被调用的方法。您可以将其安排在固定的延迟或cron等上

您还可以在配置中声明执行器,如下所示:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <description>A task pool for general use</description>
        <property name="corePoolSize" value="150" />
        <property name="maxPoolSize" value="200" />
        <property name="queueCapacity" value="10" />
        <property name="keepAliveSeconds" value="0"/>
        <property name="waitForTasksToCompleteOnShutdown" value="false"/>
    </bean>

通用的任务池
您可以使用执行器来执行并发任务池(将该bean注入到您的bean中,并查看它提供了什么)