J2EE Design Pattern : Integration Tier Patterns : Service Activator Design Pattern
Service Activator Design Pattern? |
Service Activator facilitates asynchronous processing for EJB components.
Problem
A client invokes a business service and waits until the business service returns from processing. However, the business processing in some use cases takes considerable time and resources. The business processing might even span applications, possibly integrating with applications inside and outside the enterprise. For these long-lived processes, it is not feasible for application clients to wait until the business processing completes.
Context
You want to invoke services asynchronously.
Solution
Use a Service Activator to receive asynchronous requests and invoke one or more business services.
The Service Activator is implemented as a JMS Listener and delegation service that can listen to and receive JMS messages. If your application uses EJB components and your EJB container implements EJB version 2.0 or later, then you can use a message-driven bean (MDB) to receive asynchronous requests. If your application does not use EJB technology, or uses a pre-EJB 2.0 compliant container, then you must implement your own custom solution using Java Message Service (JMS).
Participants and Collaborations
Client
The Client is any client in the application that needs to invoke a business service in an asynchronous manner. The client can be any type of application component, such as a POJO or an EJB component that can create and send JMS messages.
The client can also be an EJB component that needs to invoke another business tier component in an asynchronous manner.
Request
The Request is the message object created by the Client and sent to the ServiceActivator using the message-oriented middleware (MOM). According to the JMS specification, the Request object must implement the javax.jms.Message interface. The JMS API provides several message types, such as TextMessage, ObjectMessage, and so on, that can be used as request objects depending on what type of message you want to send.
ServiceActivator
The ServiceActivator is the main class of the pattern. It implements the javax.jms.MessageListener interface, which is defined by the JMS specification. The ServiceActivator implements an onMessage () method that is invoked when a new message arrives. The ServiceActivator parses (unmarshals) the message (request) to determine what needs to be done. The ServiceActivator might use a Service Locator to look up or create BusinessService components.
BusinessService
The BusinessService is the target object that the client asks to do asynchronous processing. The BusinessService role is typically a Session Façade or an Application Services. In applications with trivial business logic, the BusinessService can be a Business Object. The BusinessService can be any other service in the business tier that can process the Client’s request.
- Business Object Target – In simple applications with minimal business logic, a Business Object might be the target component processing a request.
- Session Façade Target – When you have applications with business logic involving multiple Business Objects, the Service Activator typically interacts with a Session Façade or an Application Service, which encapsulate business logic.
- POJO Target – In applications that do not use EJB components, you can implement business logic components as POJOs, such as Application Services. In this case, the Service Activator can invoke business methods on one or more POJOs to perform the asynchronous processing.
Response
The Response is a message object created and sent either by the ServiceActivator or a BusinessService. Response can be an acknowledgement to let the Client know that the Request was received. The Response can also be the results of the asynchronous processing.
Implementation Strategies
- POJO Service Activator Strategy
- MDB Service Activator Strategy
- Command Request Strategy
- Service Activator Aggregator Strategy
Example for Service Activator |
Recent Comments