The StartUp Service


There are many scenarios in which you want to know when your application has been started and when it has been stopped by the container. That is to say, after an application is deployed to run in a particular container, the application can be administratively started and stopped – the administrator can determine whether the application is allowed to accept requests from its clients (and end users). If an application is stopped by the administrator, the container will not dispatch work to it – it is as if the application is not there. As soon as the administrator starts the application, the container will begin dispatching work to it again.

Knowing when the application is started and stopped gives you the chance to initialize services that you may depend on in your application, and release those resources when the application has been stopped. One common use of this service is to initiate asynchronous work that will execute in the background of your application. We will discuss this further in The Deferred Execution Service section of this chapter.

The startup service is a straightforward way of being notified when the application has been started or stopped.

About the StartUp Example

We have included a example startup bean in Chapter11Sample application called SampleStartUp. It does not do much other than to put a message out to the sysout log whenever the application started or stopped, but should give you a template to work from for your own startup beans.

The StartUp Service Interfaces

The startup service is composed of two interfaces:

  • com.ibm.websphere.startupservice.AppStartUp – is the interface your StartUp bean must implement. This interface consists of two methods:

    • start() – will be called when your application is started. You should implement this method to do whatever you want to do during application startup, and you should return a Boolean value from this operation, where true indicates that your application started fine and you want the startup of your application to be completed, and false indicates that you encountered an error and do not want your application to start. If you return false from this operation, your application will terminate and be put in "stopped" state – an administrator will have to manually start your application again.

    • stop() – will be called when your application is stopped. Again, you should implement this operation to do whatever you want when your application is stopped.

  • com.ibm.websphere.startupservice.AppStartUpHome – is the interface your home StartUp bean must implement.

The start() method is invoked by the WebSphere container when your application is started – including just after a container restart if the application is already in start mode.

The stop() method is invoked by the WebSphere container when your application is stopped – including just before the container is shut down if your application is still in start mode.

Neither of these methods will be invoked while the application is in stop mode.

Creating a StartUp Listener

Creating a startup listener is simple. You provide an implementation of the com.ibm.websphere.startupservice.AppStartUp interface and deploy that as a stateful session bean in your application. You can implement and deploy multiple startup beans in your application. Simply follow the standard EJB programming model for differentiating these – as you would for differentiating any other session bean.

The following depicts the creation of a StartUp bean using WSAD-IE:

click to expand

Notice that the bean name and class are customized, while the remote interface and remote home interface are com.ibm.websphere.startupservice.AppStartUp and com.ibm.websphere.startupservice.AppStartUpHome, respectively. WebSphere will look for this, and the fact that it has been deployed as a stateful session bean as a marker of it being a startup bean.

One other thing worth noting is that WSAD-IE will want to give your bean an EJB binding name that is derived from the package and class name of your remote interface. You will have to go back to the EJB binding name entry field and adjust it to the name you would prefer. This will be important to help avoid colliding with other startup beans you may have included in your application (WebSphere will be able to differentiate between the startup beans included in your application and those included in other applications regardless of what you set this value – just not between multiple startup beans within the same module).

The WebSphere container takes care of the rest. More specifically, WebSphere will detect the presence of session beans in your application that are deployed with the AppStartUpHome interface supplied with this service and will automatically treat these as startup beans.

Obviously, you will want to implement the start() and stop() methods as appropriate to your application.

Startup Beans are EJBs

A key feature of the startup service is that the startup bean is an EJB. This is a great benefit to EJB developers, as it ensures that the startup bean will be executed within an execution environment defined by its deployment policies, such as security. However, since the startup bean is invoked by the container (not on behalf of some other application client), you should ensure you deploy with a full set of deployment policies. In particular, you should specify a RunAsSpecified security policy to ensure that you execute with the credentials and roles that are appropriate for your application. In other words, if you are going to invoke a method on another EJB during your start() method, then you will need the required authority for accessing that bean (however that was configured for that bean). You therefore will need to execute with the credentials of a principal that has that authority – something that you can achieve using the RunAsSpecified security policy for your application (see Chapter 13 – Securing your Enterprise, for more information on the RunAsSpecified security policy.




Professional IBM WebSphere 5. 0 Applicationa Server
Professional IBM WebSphere 5. 0 Applicationa Server
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 135

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net