Spring Task Scheduler versus Java ScheduledExecutorService

I have a requirement to create a task scheduler with 10 threads that we need to run at the same time, and each thread returns with a status of complete or unsuccessful. Based on the result of the stream, we will make a call to db and extract the data from db. The application is already configured using the Spring Framework. I understand that Spring provides a task scheduler, but not sure how to use it, Spring beginners need help. What about java ScheduledExecutorService, can we use this? What advantage will we get over another? Is there a better alternative to Spring and Java ScheduledExecutorService Task Scheduler?

+5
source share
2 answers

Spring TaskExecutor is actually identical to the java Executor interface. After adding Spring 2.0 TaskExecutor to add abstraction to Java Executor so that it hides implementation details between different versions of Java SE and EE environments.

Since you already have a Spring environment, I highly recommend using Spring Schedulers. Later, if necessary, you can provide the other Spring components with an abstraction for combining threads, etc.

There are also some off-the-shelf TaskExecutor implementations that are perfect, since you don't need to worry about the details and implementations on your own.

+1
source

The easiest way is to use the provided task tags in your spring configuration. Note 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 "> 

as soon as you do this 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 current scheduled task is a bean with a method that is being called. You can schedule it with a fixed delay or on cron, etc.

you can also declare artists in config as follows:

 <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 perform a pool of parallel tasks (enter a bean in your bean and see what it provides).

+1
source

Source: https://habr.com/ru/post/1213145/


All Articles