Java Reference
In-Depth Information
in Java 1.5, although its interface was compatible. And any class implementing TaskExecutor could also
implement the Executor interface because it defines the exact same method signature . This interface
exists even in Spring 3.0 for backward compatibility with JDK 1.4 in Spring 2.x. This meant that people
stuck on older JDKs could build applications with this sophisticated functionality without JDK 5. In
Spring 3.0, with Java 5 the baseline, the TaskExecutor interface now extends Executor , which means that
all the support provided by Spring now works with the core JDK support, too.
The TaskExecutor interface is used quite a bit internally in the Spring framework. For example, the
Quartz integration (which has threading, of course) and the message-driven POJO container support
make use of TaskExecutor :
// the Spring abstraction
package org.springframework.core.task;
import java.util.concurrent.Executor;
public interface TaskExecutor extends Executor {
void execute(Runnable task);
}
In some places, the various solutions mirror the functionality provided by the core JDK options. In
others, they're quite unique and provide integrations with other frameworks such as with a CommonJ
WorkManager. These integrations usually take the form of a class that can exist in the target framework
but that you can manipulate just like any other TaskExecutor abstraction. Although there is support for
adapting an existing Java SE Executor or ExecutorService as a TaskExecutor , this isn't so important in
Spring 3.0 because the base class for TaskExecutor is Executor , anyway. In this way, the TaskExecutor
in Spring bridges the gap between various solutions on Java EE and Java SE.
Let's see some of the simple support for the TaskExecutor first, using the same Runnable defined
previously. The client for the code is a simple Spring bean, into which you've injected various instances
of TaskExecutor with the sole aim of submitting the Runnable :
package com.apress.springenterpriserecipes.spring3.executors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.support.TaskExecutorAdapter;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.timer.TimerTaskExecutor;
public class SpringExecutorsDemo {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("context2.xml");
SpringExecutorsDemo demo = ctx.getBean(
"springExecutorsDemo", SpringExecutorsDemo.class);
demo.submitJobs();
}
@Autowired
private SimpleAsyncTaskExecutor asyncTaskExecutor;
Search WWH ::




Custom Search