added @Async annotation, AsyncExecutionInterceptor, AsyncAnnotationAdvisor

This commit is contained in:
Juergen Hoeller
2009-02-10 11:24:05 +00:00
parent 21a442b253
commit 777a104d48
15 changed files with 752 additions and 48 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,13 +16,16 @@
package example.scannable;
import java.util.concurrent.Future;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.AsyncResult;
/**
* @author Mark Fisher
* @author Juergen Hoeller
*/
public class AutowiredQualifierFooService implements FooService {
@@ -44,6 +47,10 @@ public class AutowiredQualifierFooService implements FooService {
return this.fooDao.findFoo(id);
}
public Future<String> asyncFoo(int id) {
return new AsyncResult<String>(this.fooDao.findFoo(id));
}
public boolean isInitCalled() {
return this.initCalled;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,10 @@
package example.scannable;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
/**
* @author Mark Fisher
* @author Juergen Hoeller
@@ -23,7 +27,10 @@ package example.scannable;
public interface FooService {
String foo(int id);
@Async
Future<String> asyncFoo(int id);
boolean isInitCalled();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
package example.scannable;
import java.util.List;
import java.util.concurrent.Future;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.BeanFactory;
@@ -30,7 +30,9 @@ import org.springframework.context.MessageSource;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
/**
* @author Mark Fisher
@@ -73,6 +75,12 @@ public class FooServiceImpl implements FooService {
return this.fooDao.findFoo(id);
}
public Future<String> asyncFoo(int id) {
System.out.println(Thread.currentThread().getName());
Assert.state(ServiceInvocationCounter.getThreadLocalCount() != null, "Thread-local counter not exposed");
return new AsyncResult<String>(this.fooDao.findFoo(id));
}
public boolean isInitCalled() {
return this.initCalled;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
package example.scannable;
import java.util.concurrent.Future;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.AsyncResult;
/**
* @author Mark Fisher
@@ -29,6 +32,10 @@ public class ScopedProxyTestBean implements FooService {
return "bar";
}
public Future<String> asyncFoo(int id) {
return new AsyncResult<String>("bar");
}
public boolean isInitCalled() {
return false;
}

View File

@@ -31,16 +31,25 @@ public class ServiceInvocationCounter {
private int useCount;
private static final ThreadLocal<Integer> threadLocalCount = new ThreadLocal<Integer>();
@Pointcut("execution(* example.scannable.FooService+.*(..))")
public void serviceExecution() {}
@Before("serviceExecution()")
public void countUse() {
this.useCount++;
this.threadLocalCount.set(this.useCount);
System.out.println("");
}
public int getCount() {
return this.useCount;
}
public static Integer getThreadLocalCount() {
return threadLocalCount.get();
}
}