Spring Task Scheduler vs. Java's ScheduledExecutorService Spring Task Scheduler vs. Java's ScheduledExecutorService multithreading multithreading

Spring Task Scheduler vs. Java's ScheduledExecutorService


Spring TaskExecutor is actually identical to java Executor interface. After Spring 2.0 TaskExecutor has been introduced to add abstraction to the Java's Executor, so that it will hide implementation details between Java SE different versions and EE environments.

As you have already Spring environment, I'd strongly recommend to use spring schedulers. Later on if there will be need you can give other Spring components an abstraction for thread pooling, etc.

Also there are some pre-built implementations of TaskExecutor, which is ideal, as you don't have to care about the details and implementation by your own.


The simplest way is to use the provided task tags in the spring config.Notice the 'task' namespace below

<?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">

once you've done that you can use

<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>

etc.your actual scheduled task is a bean with a method on it that gets called. You can schedule it on a fixed delay or on a cron etc.

you can also declare executors in the config like this :

<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>

You can use an executor to execute a pool of concurrent tasks (inject that bean into your bean and look at what it provides).