Listing 15-16. Testing Async Task
package com.apress.prospring3.ch15.schedule;
import java.util.concurrent.Future;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.apress.prospring3.ch15.service.AsyncService;
public class AsyncTaskSample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:async-app-context.xml");
ctx.refresh();
AsyncService asyncService = ctx.getBean(
"asyncService", AsyncService.class);
for (int i = 0; i < 5; i++)
asyncService.asyncTask();
Future<String> result1 = asyncService.asyncWithReturn("Clarence");
Future<String> result2 = asyncService.asyncWithReturn("John");
Future<String> result3 = asyncService.asyncWithReturn("Robert");
try {
Thread.sleep(6000);
System.out.println("Result1: " + result1.get());
System.out.println("Result2: " + result2.get());
System.out.println("Result3: " + result3.get());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
From Listing 15-16, we call the asyncTask() method five times and then the asyncWithReturn() three
times with different arguments, and then we retrieve the result after sleeping for six seconds.
Running the program will produce the following output:
2011-11-23 17:46:16,276 INFO [com.apress.prospring3.ch15.service.async.AsyncServiceImpl]
­
<Start execution of async. task>
2011-11-23 17:46:16,277 INFO [com.apress.prospring3.ch15.service.async.AsyncServiceImpl]
­
<Start execution of async. task>
2011-11-23 17:46:16,277 INFO [com.apress.prospring3.ch15.service.async.AsyncServiceImpl]
­
<Start execution of async. task>
2011-11-23 17:46:16,277 INFO [com.apress.prospring3.ch15.service.async.AsyncServiceImpl]
­
<Start execution of async. task>
2011-11-23 17:46:16,277 INFO [com.apress.prospring3.ch15.service.async.AsyncServiceImpl]
­
<Start execution of async. task>
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home