"Fossies" - the Fresh Open Source Software Archive

Member "spring-framework-6.0.4/spring-aspects/src/main/java/org/springframework/scheduling/aspectj/AbstractAsyncExecutionAspect.aj" (11 Jan 2023, 3145 Bytes) of package /linux/misc/spring-framework-6.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright 2002-2016 the original author or authors.
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *      https://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 package org.springframework.scheduling.aspectj;
   18 
   19 import java.util.concurrent.Callable;
   20 import java.util.concurrent.Future;
   21 
   22 import org.aspectj.lang.annotation.SuppressAjWarnings;
   23 import org.aspectj.lang.reflect.MethodSignature;
   24 
   25 import org.springframework.aop.interceptor.AsyncExecutionAspectSupport;
   26 import org.springframework.core.task.AsyncTaskExecutor;
   27 
   28 /**
   29  * Abstract aspect that routes selected methods asynchronously.
   30  *
   31  * <p>This aspect needs to be injected with an implementation of a task-oriented
   32  * {@link java.util.concurrent.Executor} to activate it for a specific thread pool,
   33  * or with a {@link org.springframework.beans.factory.BeanFactory} for default
   34  * executor lookup. Otherwise it will simply delegate all calls synchronously.
   35  *
   36  * @author Ramnivas Laddad
   37  * @author Juergen Hoeller
   38  * @author Chris Beams
   39  * @author Stephane Nicoll
   40  * @since 3.0.5
   41  * @see #setExecutor
   42  * @see #setBeanFactory
   43  * @see #getDefaultExecutor
   44  */
   45 public abstract aspect AbstractAsyncExecutionAspect extends AsyncExecutionAspectSupport {
   46 
   47 	/**
   48 	 * Create an {@code AnnotationAsyncExecutionAspect} with a {@code null}
   49 	 * default executor, which should instead be set via {@code #aspectOf} and
   50 	 * {@link #setExecutor}. The same applies for {@link #setExceptionHandler}.
   51 	 */
   52 	public AbstractAsyncExecutionAspect() {
   53 		super(null);
   54 	}
   55 
   56 
   57 	/**
   58 	 * Apply around advice to methods matching the {@link #asyncMethod()} pointcut,
   59 	 * submit the actual calling of the method to the correct task executor and return
   60 	 * immediately to the caller.
   61 	 * @return {@link Future} if the original method returns {@code Future};
   62 	 * {@code null} otherwise
   63 	 */
   64 	@SuppressAjWarnings("adviceDidNotMatch")
   65 	Object around() : asyncMethod() {
   66 		final MethodSignature methodSignature = (MethodSignature) thisJoinPointStaticPart.getSignature();
   67 
   68 		AsyncTaskExecutor executor = determineAsyncExecutor(methodSignature.getMethod());
   69 		if (executor == null) {
   70 			return proceed();
   71 		}
   72 
   73 		Callable<Object> task = new Callable<Object>() {
   74 			public Object call() throws Exception {
   75 				try {
   76 					Object result = proceed();
   77 					if (result instanceof Future) {
   78 						return ((Future<?>) result).get();
   79 					}
   80 				}
   81 				catch (Throwable ex) {
   82 					handleError(ex, methodSignature.getMethod(), thisJoinPoint.getArgs());
   83 				}
   84 				return null;
   85 			}};
   86 
   87 		return doSubmit(task, executor, methodSignature.getReturnType());
   88 	}
   89 
   90 	/**
   91 	 * Return the set of joinpoints at which async advice should be applied.
   92 	 */
   93 	public abstract pointcut asyncMethod();
   94 
   95 }