WAS v8.5 > End-to-end paths > Asynchronous beans

Use asynchronous beans

Asynchronous beans enable Java EE applications to run asynchronously inside an Integration Server.

  1. Configure work managers
  2. Configure timer managers
  3. Assemble applications that use work managers and timer managers
  4. Develop work objects to run code in parallel
  5. Develop event listeners
  6. Develop asynchronous scopes


Example

An asynchronous bean method can use the connections created by the Java EE component 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 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
Interoperate with asynchronous beans
Java theory and practice: Thread pools and work queues


+

Search Tips   |   Advanced Search