+

Search Tips   |   Advanced Search

Develop an enterprise bean or enterprise application client to manage ActivitySessions

Use this task to write the code needed by a session EJB or enterprise application client to manage an ActivitySession, based on the example code extract provided.

In most situations, an enterprise bean can depend on the EJB container to manage ActivitySessions within the bean. In these situations, all we need to do is set the appropriate ActivitySession attributes in the EJB module deployment descriptor, as described in the topic about configuring EJB module ActivitySession deployment attributes.. Further, in general, it is practical to design your enterprise beans so that all ActivitySession management is handled at the enterprise bean level.

However, in some cases you may need to have a session bean or enterprise application client participate directly in ActivitySessions. We then need to write the code needed by the session bean or enterprise application client to manage its own ActivitySessions.

Session beans that use BMT and have an Activate at setting of Activity session can manage ActivitySessions. Entity beans cannot manage ActivitySessions; the EJB container always manages ActivitySessions within entity beans.

When preparing to write code needed by a session bean or enterprise application client to manage ActivitySessions, consider the points described in the topic about ActivitySession and transaction contexts.

To write the code needed by a session EJB or enterprise application client to manage an ActivitySession, complete the following steps, based on the following example code extract.


Tasks

  1. Get an initial context for the ActivitySession.

  2. Get an implementation of the UserActivitySession interface, by a JNDI lookup of the URL java:comp/websphere/UserActivitySession. The UserActivitySession interface is used to begin and end ActivitySessions and to query various attributes of the active ActivitySession associated with the thread.

  3. Set the timeout, in seconds, after which any subsequently started ActivitySessions are automatically completed by the ActivitySession service. If the session bean or enterprise application client does not specifically set this value, the default timeout (300 seconds) is used.

    The default timeout can also be overridden for each application server, on the server-> Activity Session Service panel of the administrative console.

  4. Start the ActivitySession, by calling the beginSession() method of the UserActivitySession.

  5. Within the ActivitySession, call business methods to do the work needed. We can also call other methods of UserActivitySession to manage the ActivitySession; for example, to get the status of the ActivitySession or to checkpoint all the ActivitySession resources involved in the ActivitySession.
  6. End the ActivitySession, by calling the endSession() method of the UserActivitySession.


Example

The following code extract provides a basic example of using the UserActivitySession interface:

// Get initial context
  InitialContext ic = new InitialContext();
// Lookup UserActivitySession
  UserActivitySession uas = 
 (UserActivitySession)ic.lookup("java:comp/websphere/UserActivitySession");

// Set the ActivitySession timeout to 60 seconds
  uas.setSessionTimeout(60);
// Start a new ActivitySession context
  uas.beginSession();
// Do some work under this context
  MyBeanA beanA.doSomething();
  ...
  MyBeanB beanB.doSomethingElse();
// End the context
  uas.endSession(EndModeCheckpoint);


Related:

  • The ActivitySession service
  • ActivitySession and transaction contexts
  • Set EJB module ActivitySession deployment attributes