Skip to Main Content
Enterprise JavaBeans 3.1, 6th Edition
book

Enterprise JavaBeans 3.1, 6th Edition

by Andrew Lee Rubinger, Bill Burke
September 2010
Intermediate to advanced content levelIntermediate to advanced
766 pages
18h 35m
English
O'Reilly Media, Inc.
Content preview from Enterprise JavaBeans 3.1, 6th Edition

Asynchronous Methods

New in the EJB 3.1 Specification is the feature of fire-and-forget invocations upon session beans. Often we’ll have requirements where a request may take some time to be fully processed, and the client may not need to wait for the result or confirmation of completion. In these cases, we can take advantage of the new @javax.ejb.Asynchronous annotation to return control to the client before EJB is invoked. When the client does need access to the return value, it can do so via the facilities provided by java.util.concurrent.Future.

In the case of our EncryptionEJB, perhaps we have a very intensive hashing function that takes some time to complete:

    /**
     * Returns a one-way hash of the specified argument, calculated
asynchronously.
     * Useful for safely storing passwords.
     *
     * @param input
     * @return
     * @throws IllegalArgumentException
     * @throws EncryptionException
     */
    Future<String> hashAsync(String input) throws
IllegalArgumentException, EncryptionException;

During implementation, the bean provider may mark this method (either on the business interface or the bean class) as @Asynchronous, wrapping the real return value in a convenience implementation of java.util.concurrent.Future called javax.ejb.AsyncResult:

    @Asynchronous
    @Override
    public Future<String> hashAsync(final String input) throws
IllegalArgumentException, EncryptionException
    {
       // Get the real hash
       final String hash = this.hash(input);

       // Wrap and return
       return new AsyncResult<String>(hash);
    }

Client usage therefore ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick
JavaServer Faces

JavaServer Faces

Hans Bergsten
EJB 3 Developer Guide

EJB 3 Developer Guide

Michael Sikora

Publisher Resources

ISBN: 9781449399139Errata Page