||
Skip to end of metadata
Go to start of metadata

The server restart handler is an extension point provided by the Kernel to perform some tasks while restarting the server. This function will be called by the server, just before the restarting process begins. This can be used for housekeeping tasks of your custom components, such as persisting in memory data etc. To make use of this extension, a user has to implement the ServerRestartHandler interface with the invoke() method.

Implementing the ServerRestartHandler API

Sample implementation code of the ServerRestartHandler. 

package org.wso2.carbon.restarthandler;

import org.wso2.carbon.core.ServerRestartHandler;

public class SampleServerRestartHandler implements ServerRestartHandler{
   @Override
   public void invoke() {
       // Some important restart task
       System.out.println("Sample Restart task...........");
   }
}

After implementing the ServerRestartHandler interface, the implementation class must be registered as an OSGi service so that the server can identify the implementation and perform the invoke method.

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.wso2.carbon.restarthandler.SampleServerRestartHandler;
import org.wso2.carbon.core.ServerRestartHandler;

public class SampleActivator implements BundleActivator{

   @Override
   public void start(BundleContext bundleContext) throws Exception {
       bundleContext.registerService(ServerRestartHandler.class.getName(), new SampleServerRestartHandler() , null);
   }

   @Override
   public void stop(BundleContext bundleContext) throws Exception {

   }
}

The POM file for the above project is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>org.wso2.carbon</groupId>
   <artifactId>ServerRestartHandler</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>bundle</packaging>

   <dependencies>
       <dependency>
           <groupId>org.wso2.carbon</groupId>
           <artifactId>org.wso2.carbon.core</artifactId>
           <version>4.4.1</version>
       </dependency>
   </dependencies>

   <repositories>
       <repository>
           <id>wso2-nexus</id>
           <name>WSO2 internal Repository</name>
           <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
           <releases>
               <enabled>true</enabled>
               <updatePolicy>daily</updatePolicy>
               <checksumPolicy>ignore</checksumPolicy>
           </releases>
       </repository>
   </repositories>

   <build>
       <plugins>
           <plugin>
               <groupId>org.apache.felix</groupId>
               <artifactId>maven-bundle-plugin</artifactId>
               <extensions>true</extensions>
               <configuration>
                   <instructions>
                       <Bundle-Vendor>WSO2 Inc</Bundle-Vendor>
                       <Bundle-SymbolicName>org.wso2.carbon.clustering.test</Bundle-SymbolicName>
                       <Bundle-Version>1.0.0</Bundle-Version>
                       <Bundle-Activator>org.wso2.carbon.restarthandler.internal.SampleActivator</Bundle-Activator>
                       <Import-Package>
                           org.osgi.framework, org.wso2.carbon.core.* ; version="4.4.1",
                           *;resolution:=optional
                       </Import-Package>
                       <Export-Package>
                           !org.wso2.carbon.restarthandler.internal,
                           org.wso2.carbon.restarthandler
                       </Export-Package>
                       <DynamicImport-Package>*</DynamicImport-Package>
                   </instructions>
               </configuration>
           </plugin>
       </plugins>
   </build>
</project>
  • No labels