如果它们上存在“异步”注释,我有这段代码允许在单独的线程中执行函数。一切正常,除了我意识到我还必须处理我刚刚添加的一些新函数的返回值的那一天。我可以为此使用处理程序和消息传递,但是,由于已经构建了项目结构(它很大并且工作正常),我无法更改现有函数来处理消息传递。
这是代码:
/**
* Defining the Asynch interface
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Asynch {}
/**
* Implementation of the Asynch interface. Every method in our controllers
* goes through this interceptor. If the Asynch annotation is present,
* this implementation invokes a new Thread to execute the method. Simple!
*/
public class AsynchInterceptor implements MethodInterceptor {
public Object invoke(final MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
if(declaredAnnotations != null && declaredAnnotations.length > 0) {
for (Annotation annotation : declaredAnnotations) {
if(annotation instanceof Asynch) {
//start the requested task in a new thread and immediately
//return back control to the caller
new Thread(invocation.getMethod().getName()) {
public void execute() {
invocation.proceed();
}
}.start();
return null;
}
}
}
return invocation.proceed();
}
}
现在,我该如何转换它,如果它是这样的:
@Asynch
public MyClass getFeedback(int clientId){
}
MyClass mResult = getFeedback(12345);
“mResult”会用返回的值更新吗?
提前谢谢...