Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In Java EE 6, you can put the <code>@Asynchronous</code> annotation on an EJB method to have the work in that method be handled by a special thread-pool:</p> <pre><code>@Stateless public class SomeBean { @Asynchronous public void doSomething() { // ... } } </code></pre> <p>Then somewhere else, inject this bean and call the <code>doSomething()</code> method. Control will return immediately.</p> <pre><code>@WebServlet("/test") public class SyncServlet extends HttpServlet { @EJB private SomeBean someBean; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { someBean.doSomething(); } } </code></pre> <p>Note that you might <strong>not</strong> need something like <code>AsyncContext asyncContext = request.startAsync()</code> here. You would use this only if the asynchronous code would still need to send something to the response.</p> <p>If you need something akin to a period timer though, and not do something in response to a request, you can use the timer service in a singleton:</p> <pre><code>@Startup @Singleton public class SomeTimerBean { @Schedule(hour = "*", minute = "*/30", second = "0", persistent = false) public void doStuff() { // ... } } </code></pre> <p>Instead of running periodically, you can also run something like this once using the <code>@TimeOut</code> annotation:</p> <pre><code>@Startup @Singleton public class SomeTimerBean { @Resource private TimerService timerService; @Postconstruct public void init() { timerService.createSingleActionTimer(0, new TimerConfig(null, false)); } @Timeout public void doStuff(Timer timer) { // ... } } </code></pre> <p>You can have variants on this, e.g. <code>SomeTimerBean</code> can also call an <code>@Asynchronous</code> method in an <em>other</em> bean as well to basically have the same effect.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload