Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any good Spring threading with a TaskExecutor examples? [closed]

I'm trying to get a handle on how to implement threading in a Java application that uses Spring for transaction management. I've found the TaskExecutor section in the Spring documentation, and ThreadPoolTaskExecutor looks like it would fit my needs;

ThreadPoolTaskExecutor

This implementation can only be used in a Java 5 environment but is also the most commonly used one in that environment. It exposes bean properties for configuring a java.util.concurrent.ThreadPoolExecutor and wraps it in a TaskExecutor. If you need something advanced such as a ScheduledThreadPoolExecutor, it is recommended that you use a ConcurrentTaskExecutor instead.

However I have no idea how to go about using it. I've been searching for good examples for awhile now with no luck. If anyone can help me out I would appreciate it.

like image 487
James McMahon Avatar asked May 12 '09 13:05

James McMahon


People also ask

What is TaskExecutor spring?

The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster , JMS's AbstractMessageListenerContainer , and Quartz integration all use the TaskExecutor abstraction to pool threads.

What is TaskExecutor?

Interface TaskExecutorSimple task executor interface that abstracts the execution of a Runnable . Implementations can use all sorts of different execution strategies, such as: synchronous, asynchronous, using a thread pool, and more.

Does spring use thread pool?

Spring also features implementations of those interfaces that support thread pools or delegation to CommonJ within an application server environment.


1 Answers

It's pretty simple. The idea is that you have an executor object that's a bean, which is passed into whatever object wants to fire the new task (in a new thread). The nice thing is that you can modify what type of task executor to use just by changing the Spring config. In the example below I'm taking some example class (ClassWithMethodToFire) and wrapping it in a Runnable object to do the fire; you could also actually implement Runnable in a class of your own, and then in the execute method you'd just call classWithMethodToFire.run().

Here's a very simple example.

public class SomethingThatShouldHappenInAThread {      private TaskExecutor taskExecutor;      private ClassWithMethodToFire classWithMethodToFire;       public SomethingThatShouldHappenInAThread(TaskExecutor taskExecutor,                                                ClassWithMethodToFire classWithMethodToFire) {           this.taskExecutor = taskExecutor;           this.classWithMethodToFire = classWithMethodToFire;      }       public void fire(final SomeParameterClass parameter) {           taskExecutor.execute( new Runnable() {                public void run() {                     classWithMethodToFire.doSomething( parameter );                }           });      } } 

And here are the Spring beans:

<bean name="somethingThatShouldHappenInAThread" class="package.name.SomethingThatShouldHappenInAThread">      <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />      <constructor-arg type="package.name.ClassWithMethodToFire" ref="classWithMethodToFireBean"/> </bean>  <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">      <property name="corePoolSize" value="5" />      <property name="maxPoolSize" value="10" />      <property name="queueCapacity" value="25" /> </bean> 
like image 186
Jacob Mattison Avatar answered Oct 06 '22 18:10

Jacob Mattison