Java Reference
In-Depth Information
Any beans interested in subscribing to this event would normally need only to implement
ApplicationListener :
public interface ApplicationListener<E extends ApplicationEvent>
extends EventListener {
void onApplicationEvent(E event);
}
The revised Java 5 support allows you to parameterize the type of the interface in Spring 3.0.
BookInventoryManagerServiceImpl , whose job it is to respond to inventory outages, would look like this:
import org.springframework.context.ApplicationListener;
import com.apress.springenterpriserecipes.spring3.events.
CopiesExhaustedApplicationEvent;
public class BookInventoryManagerServiceImpl implements
BookInventoryManagerService,
ApplicationListener<CopiesExhaustedApplicationEvent> {
public void onApplicationEvent(CopiesExhaustedApplicationEvent event) {
System.out.printf("Received a CopiesExhausted"+
"ApplicationEvent for book %s\n",
event.getBook().getTitle());
this.purchaseMoreCopiesOfABook(event.getBook());
}
}
2-3. Achieving Concurrency with TaskExecutors
Problem
Options for building threaded, concurrent programs are myriad, but there's no standard approach.
What's more, building such programs tends to involve creating lots of utility classes to support common
use cases.
Solution
Use Spring's TaskExecutor abstraction. This abstraction provides numerous implementations
for many environments, including basic Java SE Executor implementations, Java EE WorkManager
implementations, and custom implementations. In Spring 3.0, all the implementations are unified
and can be cast to Java SE's Executor interface, too.
How It Works
Threading is a difficult issue, and several difficult use cases remain unapproachable without a sizable
amount of effort; others are at least very tedious to implement using standard threading in the Java SE
environment. Concurrency is an important aspect of architectures when implementing server-side
components and enjoys no standardization in the Java EE space. In fact, it's quite the contrary: some
parts of the Java EE specs forbid the explicit creation and manipulation of threads!
 
Search WWH ::




Custom Search