java - 为方法调用的异步执行创建自定义 "Asynch"注释是个好主意吗?

标签 java multithreading asynchronous message-queue multitasking

以下代码创建该注释。

正如此处所述, http://engineering.webengage.com/2012/03/12/a-peek-into-webengages-security-layer-super-cool-use-of-java-annotations

/**
 * 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();
  }
}

它与不返回任何内容( void )的方法调用一起使用。

示例,

/**
 * So, earlier we had a simple method in our interface which we later
 * annotated with the Asynch @interface. Bang! The caller doesn't need
 * to worry about it now. This method (no matter who the caller is)
 * gets executed asynchronously. Ain't that awesome? 
 */
@Asynch
public void refreshSurveyStatusOnResponse(String licenseCode, Integer surveyId);

优点和缺点是什么?如果我们使用消息队列和工作线程池来解决而不是异步方法调用呢? 可以使用标准 java 中的什么解决方案来代替这种自行开发的解决方案? 上面的方法似乎有一个积压,异步方法调用不会返回任何值,在这种情况下,上面的代码将中断。 ,期望异步方法调用返回值是否符合逻辑?

最佳答案

What are the pros and cons ?

您应该能够找出专业人士。如果你想不出任何一个;你有一个解决方案正在寻找一个需要解决的问题。通常的想法是提高不等待调用结果的线程的性能。

缺点,如果你不小心,它可能会更慢而不是更快。取决于 voerhead 与节省的时间。

What if we used a message queue and worker thread pool to solve instead of asynchronous method call ?

唯一知道的方法就是尝试一下。

What solution could have been used from standard Java instead of such homegrown solution?

您可以使用返回 Future 对象的 ExecutoreService。您可以使用 get()

获取结果或异常

Above method seems to have one backlog that Asynch method calls do not return any value in such a case above code will break. ,Is it logical to expect a return value on asynchronous method call ?

ExecutorServices 返回一个可以有返回值的 Future 或抛出的 Exception/Throwable(另一个可能的结果;)

关于java - 为方法调用的异步执行创建自定义 "Asynch"注释是个好主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9748316/

相关文章:

javascript - 如何用 Promise.all() 重构 for-loop async/await?

javascript - 数据库查询回调函数不返回任何内容

java - 在 Java SE 6 中 move 文件并进行错误处理

java - 更改 JFrame 的外观和感觉?

java - 在构造函数中设置 ThreadLocal 变量是否有效?

Java 内存模型和并发

multithreading - 在一个内核中的Spark worker上启动多个处理器线程

javascript - javascript 中的 await 关键字会降低应用程序的速度吗?

java - 在子类中隐藏静态方法时的签名差异

java - 在 Java 中传递类 ("Country.class") 作为参数