Developing custom services

To define a hook point to be run when a server starts and shuts down, you develop a custom service class and then use the administrative console to configure a custom service instance for an appserver. When the appserver starts, the custom service starts and initializes.

 

  1. Develop a custom service class that implements the com.ibm.websphere.runtime.CustomService interface.

    The properties passed by the appserver run-time to the initialize method can include one for an external file containing configuration information for the service (retrieved with externalConfigURLKey). In addition, the properties can contain any name-value pairs that are stored for the service, along with the other system administration configuration data for the service. The properties are passed to the initialize method of the service as a Properties object.

    There is a shutdown method for the interface as well. Both methods of the interface declare that they may throw an exception, although no specific exception subclass is defined. If an exception is thrown, the run-time logs it, disables the custom service, and proceeds with starting the server.

  2. On the Custom Service page of the administrative console, click New. Then, on the settings page for a custom service instance, create a custom service configuration for an existing appserver, supplying the name of the class implemented. If your custom services class requires a configuration file, specify the fully-qualified path name to that configuration file in the externalConfigURL field. This file name is passed into your custom service class.

  3. Stop the appserver and then restart the server.

  4. Check the appserver to ensure that the initialize method of the custom service ran as intended. Also ensure that the shutdown method performs as intended when the server stops.

 

Usage scenario

As mentioned above, your custom services class must implement the CustomService interface. In addition, your class must implement the initialize and shutdown methods. Suppose the name of the class that implements your custom service is ServerInit, your code would declare this class as shown below. The code below assumes that your custom services class needs a configuration file. It shows how to process the input parameter in order to get the configuration file. If your class does not require a configuration file, the code that processes configProperties is not needed.

public class ServerInit implements CustomService
{
/**
* The initialize method is called by the appserver run-time when the
* server starts. The Properties object passed to this method must contain all
* configuration information necessary for this service to initialize properly.
*
* @param configProperties java.util.Properties
*/
    static final java.lang.String externalConfigURLKey =
       "com.ibm.websphere.runtime.CustomService.externalConfigURLKey";
          
    static String ConfigFileName="";

    public void initialize java.util.Properties(configProperties) throws Exception
    {
        if (configProperties.getProperty(externalConfigURLKey) != null)
        {
           ConfigFileName = configProperties.getProperty(externalConfigURLKey);
        }

       // Implement rest of initialize method
    }

/**
* The shutdown method is called by the appserver run-time when the
* server begins its shutdown processing.
*
* @param configProperties java.util.Properties
*/
    public void shutdown() throws Exception
    {
        // Implement shutdown method
    }


Custom services
Custom service collection