Use asynchronous beans
The asynchronous beans feature adds a new set of APIs that enable JEE applications to run asynchronously inside an Integration Server.
For a more detailed description of the asynchronous beans model, refer to the Asynchronous beans conceptual topic. For detailed information on the programming model for supported asynchronous beans interfaces refer to the Work managers topic.
- Configure work managers.
- Configure timer managers.
- Assemble applications that use asynchronous beans work managers.
- Develop work objects to run code in parallel.
- Develop event listeners.
- Develop asynchronous scopes.
Example
An asynchronous bean method can use the connections that its creating JEE component obtained using java:comp resource references.
Use connections with asynchronous beans: The following code examples illustrates how to use connections correctly and incorrectly.This code example illustrates an asynchronous bean that uses connections correctly:
class GoodAsynchBean { DataSource ds; public GoodAsynchBean() throws NamingException { // ok to cache a connection factory or datasource // as class instance data. InitialContext ic = new InitialContext(); // it is assumed that the created Java EE component has this // resource reference defined in its deployment descriptor. ds = (DataSource)ic.lookup("java:comp/env/jdbc/myDataSource"); } // When the asynchronous bean method is called, get a connection, // use it, then close it. void anEventListener() { Connection c = null; try { c = ds.getConnection(); // use the connection now... } finally { if(c != null) c.close(); } } }The following example illustrates an asynchronous bean that uses connections incorrectly:
class BadAsynchBean { DataSource ds; // Do not do this. We cannot cache connections across asynch method calls. Connection c; public BadAsynchBean() throws NamingException { // ok to cache a connection factory or datasource as // class instance data. InitialContext ic = new InitialContext(); ds = (DataSource)ic.lookup("java:comp/env/jdbc/myDataSource"); // here, you broke the rules... c = ds.getConnection(); } // Now when the asynch method is called, illegally use the cached connection // and you likely see J2C related exceptions at run time. // close it. void someAsynchMethod() { // use the connection now... } }
Subtopics
- Asynchronous beans
An asynchronous bean is a Java object or enterprise bean that can run asynchronously by a JEE application, using the Java EE context of the asynchronous bean creator.
- Configure timer managers
A timer manager acts as a thread pool for application components that use asynchronous beans. Use the administrative console to configure timer managers. The timer manager service is enabled by default.
- Configure work managers
A work manager acts as a thread pool for application components that use asynchronous beans. Use the administrative console to configure work managers.
- Assemble applications that use work managers and timer managers
The work manager and timer manager objects are both supported for assembling applications that implement the asynchronous bean technology. We can assemble either work managers or time managers.
- Develop work objects to run code in parallel
We can run work objects in parallel, or in a different JEE context, by wrapping the code in a work object.
- Develop event listeners
Application components that listen for events can use the EventSource.addListener() method to register an event listener object (a type of asynchronous bean) with the event source to which the events will be published. An event source also can fire events in a type-safe manner using any interface.
- Develop asynchronous scopes
Asynchronous scopes are units of scoping that comprise a set of alarms, subsystem monitors, and child asynchronous scopes. We can create asynchronous scopes, starting with the parent.
- Interoperating with asynchronous beans
Asynchronous beans support Serialized WorkWithExecutionContext interoperability with objects that are serialized in 5.0.2 or later.
Related information:
Java theory and practice: Thread pools and work queues