且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java EE中的异步执行

更新时间:2023-12-03 13:03:28

Java EE 6中最简单的解决方案是使用 @Asynchronous 您的注释EJB方法(或整个类)。它允许您异步调用业务方法,这意味着将委派一个新线程来执行此方法,您将在调用者方法中获得控制权。



在Java EE前6天,JMS被用于此目的。



作为附注 - 在 Servlets 中,您还可以使用异步执行。


I'm learning Java EE currently (moving from SE) and I am confused about asynchronous execution in Java EE environment.
Basically what I understand creating Thread or Timer is not exactly recommended. One other method what I found so far are use JMS for transfer message to EJB Message Bean and it will be executed asynchronously.

What are some other methods to achieve this behavior? Cause using JMS looks like too much overhead for simple tasks.

The simplest possible solution in Java EE 6 is to use @Asynchronous annotation on your EJB method (or the whole class). It allows you to invoke a business method asynchronously which means that a new thread will be delegated to execute this method and you'll get the control back in caller method.

In pre-Java EE 6 days the JMS was used for this purpose.

As a side note - in Servlets you can also use asynchronous execution.