Service ActivatorContextEnterprise beans and other business services need a way to be activated asynchronously without user intervention. ProblemEnterprise beans implemented prior to the EJB 2.0 specification do not support asynchronous invocation. EJB 2.0 introduces integration with JMS by providing a Message Driven Bean, which is a new type of stateless session bean. When a client needs to access the services offered by an enterprise bean, it does so by first looking up the bean's home interface, then calling create/find/remove on the home. All interactions between the client and the enterprise beans components are remote and synchronous. The EJB specification permits the container to passivate an enterprise bean to secondary storage. As a result, the EJB container has no mechanism by which it can provide a process-like service where an enterprise bean is always in activated and ready state. Because the client must interact with the bean using the bean's remote interface, even if the bean is always activated in the container, the client still needs to obtain its remote interface via the lookup process and still interacts with the bean in a synchronous manner. If application needs are synchronous processing for server-side business components, then enterprise beans are the appropriate choice. Some application clients may require asynchronous processing for the server-side business objects because the clients do not need to wait or do not have the time to wait for the processing to complete. In such cases, the bean functionality provided by pre-EJB 2.0 specification implementations fails to offer the required solution. In EJB 2.0, the newly introduced message-driven bean is a special type of stateless session bean. However, the new specification does not offer asynchronous invocation for other types of enterprise beans such as stateful or entity beans. In general, any business service that provides only synchronous processing may be a limitation to implement asynchronous processing. Forces
SolutionUse a Service Activator to receive asynchronous client requests and messages. On receiving a message, the Service Activator locates and invokes the necessary business methods on the business service components to fulfill the request asynchronously. The ServiceActivator is a JMS Listener and delegation service that requires implementing the JMS message listener-making it a JMS listener object that can listen to JMS messages- and it can be implemented as a standalone service. Clients act as the message generator, generating events based on their activity. Any client that needs to asynchronously invoke a business service, such as an enterprise bean, may create and send a message to the Service Activator. The Service Activator receives the message and parses it to interpret the client's request. Once the client's request is identified, the Service Activator locates the necessary business service components and invokes the business services to complete processing the client's request asynchronously. The Service Activator may optionally send an acknowledgement to the client after successfully completing the request processing. The Service Activator may also notify the client or other services on failure events if it fails to complete the asynchronous request processing. The Service Activator may use the services of a Service Locator to locate a business component (see Service Locator pattern). StructureThe following class diagram represents the relationships for the Service Activator pattern.
Participants & ResponsibilitiesThe following sequence diagram shows the interaction between the various participants in this pattern.
click to enlarge Client Request ServiceActivator BusinessObject StrategiesStrategies for BusinessObjectSession Bean vs. Entity Bean: Both session and entity beans can fulfill the role of a BusinessObject. When J2EE applications implement a Session Façade pattern to provide coarse-grained access to entity beans and to encapsulate the workflow, then the session bean from the Session Façade fulfills the BusinessObject role.In simple applications with minimal workflow, an entity bean may fulfill the BusinessObject role. However, it is not recommended that the Service Activator deal directly with multiple entity beans. Such workflow is usually encapsulated in a Session Façade instead. Stateful vs. Stateless Sesson Bean as the BusinessObject: When a Session Bean fulfills the role of the BusinessObject, the business requirements determine whether the bean should be stateful or stateless. Since the client for the BusinessObject is a ServiceActivator that activates the BusinessObject on receiving a new message, the workflow to process the message can define whether the bean should be stateful or not. In most cases, a message delivery simply activates a single method in the BusinessObject that delegates the processing of the message within. This is typical when the BusinessObject is a Session Façade that provides coarse-grained business services in an EJB application. A stateless session bean can be used in these cases. If the ServiceActivator needs to invoke multiple methods in the BusinessObject or to work with more than one BusinessObject to fulfill the processing requirements for a message, it may be useful to consider a stateful session bean to retain state between multiple invocations. Strategies for ServiceActivatorThe most straightforward strategy for the listener or ServiceActivator is to implement it as a standalone JMS application that listens and processes JMS messages. An alternative is to implement the ServiceActivator as a service of the application server. This may make it easier to manage the ServiceActivator, because it uses the application server features to monitor the ServiceActivator state, and to start, restart, and stop the ServiceActivator as needed, either manually or automatically. Strategies for ClientsThe Client can be type of any client, including another enterprise bean, that requires asynchronous processing from the enterprise bean. When integrating legacy applications to the J2EE platform, Java application clients are a logical choice to act as the message generators based on the activity in the legacy system. The ServiceActivator can receive messages and perform the necessary enterprise bean invocations to process the request from the legacy system. Consequences
Prior to the EJB 2.0 specification, there was no integration between enterprise bean and JMS components. This pattern provides a means to integrate JMS into an EJB application and enable asynchronous processing. The EJB 2.0 specification defines a new type of session bean, called a message-driven bean, to integrate JMS and enterprise beans. This special bean implements the JMS Message Listener interface and it receives asynchronous messages. In this case, the application server plays the role of the ServiceActivator. This pattern makes it possible to run applications in EJB 2.0 implementations as well as pre-EJB 2.0 implementations. In EJB 2.0, the message-driven bean is a stateless session bean. Using the Service Activator pattern, it is possible to provide asynchronous invocation on all types of entity beans such as stateless session bean, stateful session bean and entity bean. As previously explained, since the ServiceActivator is implemented in its own right, without any limitations of the message driven bean, the Service Activator can perform asynchronous invocations on any type of business service. Thus, this pattern provides a way to enable asynchronous processing for applications that either have no need to wait for the results or do not want to spend time waiting for processing to complete. The processing can be deferred and performed at a later time, enabling the client to complete the service in less time. The ServiceActivator can be run as a standalone process. However, in a critical application, ServiceActivator needs to be monitored to ensure availability. The additional management and maintenance of this process can add to application support overhead. Related Patterns
The Session Facade pattern encapsulates the complexity of the system and provides coarse-grained access to business objects. This Service Activator pattern may access a Session Façade as the primary business object to invoke business service methods in the Session Façade asynchronously on behalf of the client. The Service Activator pattern employs the delegate pattern because it delegates the received message's work to the ServiceActivator object. The ServiceActivator acts as a listener. On receiving a new message, the Service Activator fulfills the role of a delegate to get the work done for the message. The client can use the Service Locator pattern to look up and create JMS-related service objects. The ServiceActivator can use the Service Locator pattern to look up and create enterprise bean components. (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. Version 1.0 Beta | |||
|
| ||||||||||||