|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Extensions
Examining the Programming Model Extensions
Within IBM WebSphere Application Server version 6
By: Roland Barcia
Digg This!
Back in version 4 of IBM WebSphere Application Server (WAS), IBM released the Enterprise Edition of WAS. In version 5, IBM continued to release the Enterprise Edition (later renamed IBM WebSphere Business Integration Server Foundations in v5.1) with even more functionality. In version 5, the heart of the Enterprise Edition functionality was process choreographer, a Java 2 Enterprise Edition (J2EE)-based workflow engine. However, the Enterprise Edition also contains a slew of other technology sometimes referred to as the WAS Programming Model Extensions (PME). The goal of these programming model extensions is to provide solutions to problems that are difficult and sometimes impossible to solve using existing J2EE technology. In WAS v5, customers needed to upgrade to the Enterprise Edition to benefit from WAS PMEs, leaving a large portion of WAS developers out of luck. However, in version 6 most of the PMEs have now been included in all editions of WAS. This means that all WebSphere version 6 users can benefit from these extensions without upgrading to a higher end workflow engine. In this article, I will go over some of these WAS PMEs. I will discuss each PME in terms of the problem it solves. In addition, I'll talk about the work being done to add some of these extensions to future versions of the J2EE specification. Overview of ExtensionsIn order to put some structure around this article, I am going to categorize the extensions by their problem space:
Multi-threading and SchedulingIn this section, I am going to discuss some of the WAS PME features in the multi-threading and scheduling space.Asynchronous Beans and CommonJ Work Manager WAS developers can create asynchronous beans instead of Java threads. Asynchronous beans allow a developer to code the work to be done on a separate thread. (It is important to note that because of work being done to standardize asynchronous beans, the asynchronous bean framework is also referred to as CommonJ WorkManager. This means that there are two APIs, one in com.ibm package referred to as asynchronous bean API, and the other in the commonj package referred to as CommonJ WorkManager API. For the purpose of this article we will refer to asynchronous beans and CommonJ WorkManager interchangeably; they are both configured the same way.) There are three types of asynchronous beans:
public class StockQuoteTimerListener implements TimerListener {
String context;
String url;
public StockQuoteTimerListener(String context, String url){
this.context = context;
This.url = url;
}
public void timerExpired(Timer timer) {
System.out.println("Timer fired. Context ="+
((StockQuoteTimerListener)timer.getTimerListener()).getContext());
}
public String getContext() {
return context;
}
}
Timers are not persistent in WAS. If you need persistent services, the WAS scheduler or EJB timer service should be used. (In version 5, the Enterprise Edition provided alarms instead of timers. Timers are a replacement for alarms but the alarms are still supported.) As I mentioned, the J2EE context can flow from one J2EE component to another as long as they are called on the same thread. The asynchronous bean framework makes it possible to transfer all or some of the context to separate threads. The administrator can also configure which parts of the context are going to be transferred when a WorkManager is created. The following contexts can optionally be configured to propagate:
Due to the asynchronous nature of this programming model, transactional contexts are not passed to the asynchronous beans. The asynchronous beans get their own local transaction, or they can start global transactions if XA resources are involved. Asynchronous beans should stay away from caching connections. They can cache connection factories and then get a connection, use it, and release it at the end of the asynchronous execution. That way connection pooling is used correctly. The WorkManager is the heart of the asynchronous bean framework. A WorkManager is essentially a thread pool. It runs Work instances asynchronously and controls the transfer of J2EE contexts to those threads. The WorkManager is the anchor point that allows developers to create asynchronous beans. Applications will look up the WorkManagers using JNDI. Once an application has obtained a WorkManager, it can use it, for example, to submit Work instances. The WorkManager returns a WorkItem to the application, which can use it to monitor the progress of the work. Applications can define resource references to work managers. For timers, timer managers are configured on the server much the same way as work managers (see Figure 1). The asynchronous bean framework is the basis for two new JSRs which have been submitted jointly by IBM and BEA. These are JSR 236 (Timers for Application Servers) and JSR 237 (Work Managers for Application Servers). For further information on asynchronous beans, see the following link: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.nd.doc/info/ae/ae/welc6tech_asb.html. Scheduler Service Applications can then treat the scheduler as a resource and create tasks as shown:
//lookup Scheduler as a Resource
context = new InitialContext();
Object schedulerObj =
context.lookup("java:comp/
env/scheduler/MyScheduler");
scheduler = (Scheduler) schedulerObj;
//Lookup Task Handler as an EJB
Object taskHandlerObj = context.lookup("java:comp/
env/ejb/StockTickerPublisher");
taskHandlerHome =
(TaskHandlerHome) PortableRemoteObject.narrow(
taskHandlerObj,
TaskHandlerHome.class);
//Create Task
taskInfo = scheduler.createBeanTaskInfo();
//Associate TaskHandler with Task
taskInfo.setTaskHandler(taskHandlerHome);
taskInfo.setStartTimeInterval(startInterval
+ "minutes");
taskInfo.setRepeatInterval(repeatInterval + "minutes");
taskInfo.setNumberOfRepeats(repeats);
taskInfo.setName(taskName);
//Create Task in Scheduler
TaskStatus status = scheduler.create(taskInfo);
WAS provides the home and remote interface for the task handler object. Developers only need to provide a bean class with a special process method:
//Example task handler
public void process(TaskStatus taskStatus)
{
try
{
publisherBO.publishStockTicker();
}
catch (StockException e)
{
throw new EJBException(e.getLocalizedMessage(),e);
}
}
In J2EE 1.4, the EJB timer service is introduced to the specification. The EJB timer service allows any EJB component that implements the TimerObject interface to be called by some scheduling component. Any EJB type can be used (session bean, entity bean, or message driven bean) as a timed object. The timer service also defines a standard interface called TimerService for scheduling events. In version 6, the EJB timed objects is a special type of WAS TaskHandler. So the EJB timed components build upon the same scheduling infrastructure provided in version 6. By default, an internal scheduler instance is used to manage those tasks, and they are persisted to a Cloudscape database associated with the server process. The EJB timer service can be configured on the desired server (see Figure 3). You can choose to use the default pre-configured scheduler or your own (see Figure 4). Any EJB type can implement the TimedObject interface and implement the ejbTimeout() method:
import javax.ejb.Timer;
import javax.ejb.TimedObject;
import javax.ejb.TimerService;
public class MyBean implements SessionBean, TimedObject {
// This method is called by the EJB container upon Timer expiration.
public void ejbTimeout(Timer theTimer) {
// Any code typically placed in an EJB method can be placed here.
String whyWasICalled = (String) Timer.getInfo():
System.out.println("I was called because of"+ whyWasICalled);
} // end of method ejbTimeout
Keep in mind that you cannot use the scheduler API to schedule tasks for EJB-based timers or use the TimerService API to configure WAS TaskHandlers. For further information on the scheduler service, see the following URL: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.express.doc/info/exp/ae/welc6tech_sch.html. Transactional Enhancements and EJB Entity Bean ExtensionIn this section I will discuss some extensions that deal with transactions, such as single phase resources inside an XA transaction. I will also discuss some extensions to entity beans, such as dynamic query and application profiles.Last Participant Support Ordinarily, all participants in a two-phase commit process must be two-phase commit-capable. With the last participant support (LPS), you can use a single one-phase commit (1PC)-capable resource with any number of two-phase commit (2PC)-capable resources in the same global transaction. The global transaction commit processing still takes place in two phases. In phase one, all the two-phase commit resources are prepared using the two-phase commit protocol. During phase two, the one-phase commit resource is called to commit first if all the two-phase resources successfully prepared. This way, if the 1PC resource fails, WAS can simply roll back the other resources. On the other hand, if the 1PC commit resource succeeds, WAS can commit the remaining resources. Figure 5 illustrates the process. Keep in mind that LPS is not a complete substitute for using a resource that fully supports 2PC. It should only be used when you want to commit a one-phase capable resource (that has no 2PC support) with other 2PC resources and you want to get as close to 2PC as possible. LPS introduces a hazard of a mixed commit result because 1PC resources can't completely participate in the 2PC protocol. For example, if the WAS doesn't get a response from the 1PC resource, it has no way of knowing the status. Thus, WAS can't know whether the 1PC resource committed or not and thus does not know how to proceed. In this case, manual intervention will be required to resolve the state of the data in the 1PC resource. No special programming is needed to use LPS; it only needs to be enabled using WebSphere Application Server administration. For more information on last participant support, see http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.express.doc/info/exp/lao/ref/rla_rlinks.html. Activity Sessions 1. Client-side demarcation of and coordination of multiple one-phase units of work: The activity session service captures the commit operations of each local and global transaction and upholds them. At the end of the activity session, the container will go back and commit (or roll back) each individual transaction. Keep in mind that the activity session, like LPS, is no substitute for two-phase commitment control, when it comes to data integrity. An activity session may result in a mixed outcome, if some of the single phase resources successfully get committed before another resource fails to commit. In that case, the activity session service will allow the programmer to retrieve the list of resources that were committed and those whose state is uncertain. There are two approaches to implementing activity sessions in your applications: the declarative (or container-managed) approach and the programmatic (or bean managed) approach. The programming model for activity sessions and the terminology which relates to the activity session service closely resemble the standard definitions that apply to transaction management, making it simple for a J2EE programmer to become familiar with the activity session semantics. Activity session scopes can be defined either through declarations that apply to EJB methods invocations (in a very similar way as for declarative demarcation of transactions) or servlets - or programmatically, using the UserActivitySession APIs - which are semantically very similar to the standard UserTransaction APIs. Figure 6 shows an example of a scenario. Here you have two different entity beans each mapped to a different 1PC resource. The EJB methods are demarcated much like you would regular transactions only using ActivityService demarcations. Activity sessions are not just bound to local resources. They can even be used to group multiple 2PC. Activity sessions are being worked on for future versions of J2EE as JSR 95: J2EE TM Activity Service for Extended Transactions (URL). For more information on the activity service, see: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.express.doc/info/exp/ae/welc6tech_as.html. Other Entity Bean Enhancements 1. Dynamic query support: The current EJB 2.1 specification only allows you to define EJB QL statically in the EJB deployment descriptor. However, there are times when an application needs to construct a query dynamically. The dynamic query service provides an API you can use to pass EJB QL at runtime. The dynamic query service is automatically deployed on WAS as a stateless session bean with a local and remote interface. Dynamic queries is being worked on as part of EJB 3.0. For more information on dynamic query, see: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.express.doc/info/exp/ae/welc6tech_que.html. For more information on application profiles, see: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.express.doc/info/exp/ae/welc6tech_appprof.html. Other PMEsWe will briefly review some more WAS 6 PMEs and provide links where you can find more information.WorkArea Service WAS version 6 provides this function through the WorkArea service. By using a simple API, components can pass context information from component to component without passing it directly through programmatic interface. The WorkArea service will propagate to local components using thread local storage and it will pass data to remote components through IIOP. Work areas can be nested as well to allow flexibility of layering your code correctly. A client component can create a UserWork area:
//Lookup UserWorkArea
UserWorkArea workArea = (UserWorkArea) ctx.lookup("java:comp/websphere/UserWorkArea");
//create workArea
workArea.begin("Profile");
workArea.set("Name","Luke Skywalker");
workArea.set("Age",new Integer(28));
//call component
myEJB.callService(params);
//complete workArea
workArea.complete();
The target component can then access the UserWorkarea to obtain the information.
void callService() throws SomeException
{
InitialContext ctx = new InitialContext();
//lookup WorkArea
UserWorkArea workArea =
(UserWorkArea) ctx.lookup("java:comp/websphere/UserWorkArea");
//Access Data
System.out.println("Work Area");
System.out.println("Name : " + workArea.get("Name"));
System.out.println("Age : " + workArea.get("Age"));
}
IBM is leading JSR 149 for adding WorkArea support to the J2EE specification. You can find more information about the WorkArea service at: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.base.doc/info/aes/ae/welc6tech_wa.html. Internationalization Service The i18n service in WAS 6 allows applications to access the i18n context of the caller:
UserInternationalization i18nService = (UserInternationalization) ctx.lookup("java:comp/websphere/UserInternationalization");
Internationalization callerI18n = i18nService.getCallerInternationalization();
The i18n service also allows applications to access the i18n context of the running environment: UserInternationalization i18nService = (UserInternationalization) ctx.lookup( "java:comp/websphere/UserInternationalization"); InvocationInternationalization invI18n = i18nService.getInvocationInternationalization(); IBM is working on standardizing the i18n service through JSR 150. More information can be found at: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.base.doc/info/aes/ae/welc6tech_in.html. Object Pools More information about object pools can be found at: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.base.doc/info/aes/ae/welc6tech_objp.html. Startup Beans More information can be found about startup beans at: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.base.doc/info/aes/ae/welc6tech_sub.html. Dynamic Cache More information on dynamic cache service can be found at: http://publib.boulder.ibm.com/infocenter/ws60help/index.jsp?topic=/ com.ibm.websphere.base.doc/info/aes/ae/welc6tech_dyn.html. ConclusionThis article provided an overview of many of the WAS programming model extensions. There are certainly other extensions not addressed in this article. For example, there are more extensions in the area of SOA, such as the Web Service Gateway or Service Data Objects (SDO). This article tries to provide the PMEs that were previously only available in the Enterprise Edition (WBI SF) of WAS. Now, all WebSphere Application Server customers can use these enterprise class PMEs to fulfill their enterprise requirements.AcknowledgmentsThanks to Billy Newport, Chris Johnson, and Bruce Clay for reviewing this document.References
WEBSPHERE LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING WEBSPHERE NEWS
|
|||||||||||||||||||||||||||||||