Activity, in the SLEE API, refers to a sequence of related events from a resource.
For example, SIP dialogs and Diameter sessions are activities. An RA creates activities when certain initial events occur. A SIP resource adaptor might create a new dialog activity when an initial INVITE arrives. When the RA determines that no more events will occur on the activity, the activity is ended. In SIP this might be when a BYE
request is processed.
Each activity has an activity object. This is a Java object, managed by the RA, that applications can use to interact with the activity, for example to query state or send messages. The RA type defines what types of activity object may be created by the RA.
The SLEE manages an activity context (AC) for each activity. The AC is a logical handle for the activity. Applications can "attach" to activity contexts, which means they will automatically receive any events that the RA fires on that activity. Activity contexts are presented to applications as instances of the type javax.slee.ActivityContextInterface (ACI). Applications can use the RA type’s ACI factory interface to obtain an ActivityContextInterface
instance for an activity.
Activities that SIP Servlet applications support
SIP Servlet applications already support two types of "activity", SipSession
and SipApplicationSession
. These are built into the SIP Servlet API, and servlet applications don’t need to do anything special to handle events from them. But when using other SLEE RAs, the servlet application needs to be aware of activities and activity contexts in order to use those RAs effectively.
Many RAs operate asynchronously when communicating with external systems. Typically a SLEE application calls the RA’s interface to send a request, but instead of waiting for the response to arrive (which blocks the calling thread), most RAs can send the request and then return an activity, which will fire the response event(s), in the future. The application attaches to the new activities' activity context, and the SLEE ensures that future events on that activity will be delivered to the attached application instance.
In a SIP Servlet application, the "application instance" corresponds to the SipApplicationSession
. Servlets themselves are stateless; any state related to a particular call will be stored in the SipApplicationSession
(and its child SipSessions
) that the container created to process that call. The SIP Servlet container ensures that all SIP messages and other events related to a particular application instance are processed within the same SipApplicationSession
context. This makes it easy for developers to reason about what should happen on a call, because all the relevant state is immediately available from the SipApplicationSession
.
In the same way, to ensure that events arriving from other SLEE RAs are handled by the correct SipApplicationSession
, it is necessary to logically attach a SLEE RA activity context to a SipApplicationSession
. Servlet applications can do this using the com.opencloud.sipservlet.slee.SleeAdaptor
interface in sipservlet-slee.jar
.
Like other container-managed interfaces, applications may use resource injection to obtain a
|
Getting the ACI for an activity
When a new activity is created, the application obtains the ACI for the activity, and then calls SleeAdaptor.attach()
with that ACI. This will attach the current SipApplicationSession
to the activity context. Now when events arrive on the activity, they will be handled by the same SipApplicationSession
. See Event Handling for details on handling SLEE events in servlet applications.
Here is a small example, again using the HTTP RA. The HTTP RA Type defines the interfaces HttpProvider
, for sending HTTP requests, and HttpActivityContextInterfaceFactory
, for obtaining the ACI for an HTTP activity.
@Resource
private SleeAdaptor sleeAdaptor;
@Resource(name = "slee/resources/http/provider")
private HttpProvider httpProvider;
@Resource(name = "slee/resources/http/acifactory")
private HttpActivityContextInterfaceFactory httpACIFactory;
...
// Create a request
HttpRequest req = httpProvider.createRequest("GET", new URL("http://example.com"));
// Send request, returns the OutgoingHttpRequestActivity activity object
OutgoingHttpRequestActivity activity = httpProvider.sendRequest(req);
// Obtain ACI for the new activity
ActivityContextInterface aci = httpACIFactory.getActivityContextInterface(activity);
// Attach our SipApplicationSession to the activity.
sleeAdaptor.attach(aci);
Applications can also detach from activities, using SleeAdaptor.detach()
. Both attach()
and detach()
implicitly operate on the current SipApplicationSession
. An application may attach an activity to a different SipApplicationSession
as well, for example an application session that was looked up via SipSessionsUtil
.
Attaching an activity to a different application session effectively "hands off" the activity to that application session. The current application session is automatically detached and won’t receive future events from the activity. See the SleeAdaptor javadoc for details.
|