moved async aspect to aspectj sub-package
This commit is contained in:
parent
d9d7fb6f9a
commit
a6569a2930
|
|
@ -1,58 +0,0 @@
|
||||||
package org.springframework.scheduling;
|
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
import org.aspectj.lang.reflect.MethodSignature;
|
|
||||||
import org.springframework.core.task.AsyncTaskExecutor;
|
|
||||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
|
||||||
import org.springframework.core.task.support.TaskExecutorAdapter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract aspect that routes selected methods asynchronously.
|
|
||||||
* <p>
|
|
||||||
* This aspect, by default, uses {@link SimpleAsyncTaskExecutor} to route method
|
|
||||||
* execution. However, you may inject it with any implementation of
|
|
||||||
* {@link Executor} to override the default.
|
|
||||||
*
|
|
||||||
* @author Ramnivas Laddad
|
|
||||||
*/
|
|
||||||
public abstract aspect AbstractAsynchronousExecutionAspect {
|
|
||||||
private AsyncTaskExecutor asyncExecutor;
|
|
||||||
|
|
||||||
public AbstractAsynchronousExecutionAspect() {
|
|
||||||
// Set default executor, which may be replaced by calling setExecutor(Executor)
|
|
||||||
setExecutor(new SimpleAsyncTaskExecutor());
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract pointcut asyncMethod();
|
|
||||||
|
|
||||||
Object around() : asyncMethod() {
|
|
||||||
Callable<Object> callable = new Callable<Object>() {
|
|
||||||
public Object call() throws Exception {
|
|
||||||
Object result = proceed();
|
|
||||||
if (result instanceof Future) {
|
|
||||||
return ((Future<?>) result).get();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}};
|
|
||||||
|
|
||||||
Future<?> result = asyncExecutor.submit(callable);
|
|
||||||
|
|
||||||
if (Future.class.isAssignableFrom(((MethodSignature)thisJoinPointStaticPart.getSignature()).getReturnType())) {
|
|
||||||
return result;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setExecutor(Executor executor) {
|
|
||||||
if (executor instanceof AsyncTaskExecutor) {
|
|
||||||
this.asyncExecutor = (AsyncTaskExecutor) executor;
|
|
||||||
} else {
|
|
||||||
this.asyncExecutor = new TaskExecutorAdapter(asyncExecutor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2010 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.aspectj;
|
||||||
|
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
import org.springframework.core.task.AsyncTaskExecutor;
|
||||||
|
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||||
|
import org.springframework.core.task.support.TaskExecutorAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract aspect that routes selected methods asynchronously.
|
||||||
|
*
|
||||||
|
* <p>This aspect, by default, uses {@link SimpleAsyncTaskExecutor} to route
|
||||||
|
* method execution. However, you may inject it with any implementation of
|
||||||
|
* {@link Executor} to override the default.
|
||||||
|
*
|
||||||
|
* @author Ramnivas Laddad
|
||||||
|
* @since 3.0.5
|
||||||
|
*/
|
||||||
|
public abstract aspect AbstractAsynchronousExecutionAspect {
|
||||||
|
|
||||||
|
private AsyncTaskExecutor asyncExecutor;
|
||||||
|
|
||||||
|
public AbstractAsynchronousExecutionAspect() {
|
||||||
|
// Set default executor, which may be replaced by calling setExecutor.
|
||||||
|
setExecutor(new SimpleAsyncTaskExecutor());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExecutor(Executor executor) {
|
||||||
|
if (executor instanceof AsyncTaskExecutor) {
|
||||||
|
this.asyncExecutor = (AsyncTaskExecutor) executor;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.asyncExecutor = new TaskExecutorAdapter(asyncExecutor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object around() : asyncMethod() {
|
||||||
|
Callable<Object> callable = new Callable<Object>() {
|
||||||
|
public Object call() throws Exception {
|
||||||
|
Object result = proceed();
|
||||||
|
if (result instanceof Future) {
|
||||||
|
return ((Future<?>) result).get();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}};
|
||||||
|
Future<?> result = asyncExecutor.submit(callable);
|
||||||
|
if (Future.class.isAssignableFrom(((MethodSignature) thisJoinPointStaticPart.getSignature()).getReturnType())) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract pointcut asyncMethod();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,28 @@
|
||||||
package org.springframework.scheduling;
|
/*
|
||||||
|
* Copyright 2002-2010 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.scheduling.aspectj;
|
||||||
|
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Aspect to route methods based on the {@link Async} annotation.
|
* Aspect to route methods based on the {@link Async} annotation.
|
||||||
* <p>
|
*
|
||||||
* This aspect routes methods marked with the {@link Async} annotation
|
* <p>This aspect routes methods marked with the {@link Async} annotation
|
||||||
* as well as methods in classes marked with the same. Any method expected
|
* as well as methods in classes marked with the same. Any method expected
|
||||||
* to be routed asynchronously must return either void, {@link Future},
|
* to be routed asynchronously must return either void, {@link Future},
|
||||||
* or a subtype of {@link Future}. This aspect, therefore, will produce
|
* or a subtype of {@link Future}. This aspect, therefore, will produce
|
||||||
|
|
@ -15,11 +31,13 @@ import org.springframework.scheduling.annotation.Async;
|
||||||
* violates this constraint, it produces only a warning.
|
* violates this constraint, it produces only a warning.
|
||||||
*
|
*
|
||||||
* @author Ramnivas Laddad
|
* @author Ramnivas Laddad
|
||||||
*
|
* @since 3.0.5
|
||||||
*/
|
*/
|
||||||
public aspect AnnotationDrivenAsynchronousExecutionAspect extends AbstractAsynchronousExecutionAspect {
|
public aspect AnnotationDrivenAsynchronousExecutionAspect extends AbstractAsynchronousExecutionAspect {
|
||||||
|
|
||||||
private pointcut asyncMarkedMethod()
|
private pointcut asyncMarkedMethod()
|
||||||
: execution(@Async (void || Future+) *(..));
|
: execution(@Async (void || Future+) *(..));
|
||||||
|
|
||||||
private pointcut asyncTypeMarkedMethod()
|
private pointcut asyncTypeMarkedMethod()
|
||||||
: execution((void || Future+) (@Async *).*(..));
|
: execution((void || Future+) (@Async *).*(..));
|
||||||
|
|
||||||
|
|
@ -27,9 +45,10 @@ public aspect AnnotationDrivenAsynchronousExecutionAspect extends AbstractAsynch
|
||||||
|
|
||||||
declare error:
|
declare error:
|
||||||
execution(@Async !(void||Future) *(..)):
|
execution(@Async !(void||Future) *(..)):
|
||||||
"Only method that return void or Future may have @Async annotation";
|
"Only methods that return void or Future may have an @Async annotation";
|
||||||
|
|
||||||
declare warning:
|
declare warning:
|
||||||
execution(!(void||Future) (@Async *).*(..)):
|
execution(!(void||Future) (@Async *).*(..)):
|
||||||
"Method in class marked with @Async that do not return void or Future will be routed synchronously";
|
"Methods in a class marked with @Async that do not return void or Future will be routed synchronously";
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue