JPPF startup classes
From JPPF 6.2 Documentation
|
Main Page > Extending and Customizing JPPF > JPPF startup classes |
Startup classes allow a piece of code to be executed at startup time of a node or server. They can be used for many purposes, including initialization of resources such as database connections, JMS queues, cache frameworks, authentication, etc ... They permit the creation of any object within the same JVM as the JPPF component they run in.
Startup classes are defined using the Service Provider Interface. The general workflow to create a custom startup class is as follows:
- step 1: create a class implementing the startup class provider interface
- step 2: add or update the corresponding service definition file in the META-INF/services folder
- step 3: create a jar file containing the above elements and deploy it in the node or server class path
This mechanism relies on the following rules:
- the provider interface for a node or server startup class extends the interface JPPFStartup, which itself extends java.lang.Runnable. Thus, writing a startup class consists essentially in writing code in the run() method.
- the provider interface implementation must have a no-arg constructor
- startup classes are instantiated and run just after the JPPF and custom MBeans have been initialized. This allows a startup class to subscribe to any notifications that an MBean may emit.
1 Node startup classes
1.1 Implement the node startup class provider interface
To make our startup class pluggable to the nodes, it must be recognized as a corresponding service instance. To this effect, we will create an implementation of the interface JPPFNodeStartupSPI, which will provide the node with enough information to create and run the startup class. This interface is defined as follows:
public interface JPPFNodeStartupSPI extends JPPFStartup { }
As we can see, this is just a marker interface, used to distinguish between node startup classes and server startup classes. As an example, we will create an implementation that simply prints a message when the node starts:
package org.jppf.example.startup.node; import org.jppf.startup.JPPFNodeStartupSPI; // This is a test of a node startup class public class TestNodeStartup implements JPPFNodeStartupSPI { @Override public void run() { System.out.println("I'm a node startup class"); } }
A server startup implementation may need information on the JPPF node itself. To this effect, you can define a method with this exact signature: public void setNode(Node). This method, if defined, will be called exactly once, before the run() method. It provides access to information on the node via the Node API, and can be used as follows:
public class TestNodeStartup implements JPPFNodeStartupSPI { private Node node; @Override public void run() { System.out.println("Node configuration: " + node.getConfiguration()); } // the JPPF node will detect that this method exists and call it public void setNode(Node node) { this. node = node; } }
1.2 Create the service definition file
If it doesn't already exist, we create, in the source folder, a subfolder named META-INF/services. In this folder, we will create a file named org.jppf.startup.JPPFNodeStartupSPI, and open it in a text editor. In the editor, we add a single line containing the fully qualified name of our startup class:
org.jppf.example.startup.node.TestNodeStartup
1.3 Deploy the startup class
Now we just create a jar that contains all the artifacts we have created: JPPF node startup provider class , along with the META-INF/services folder, and add it to the class path of either the server, if we want all nodes attached to the server to use the startup class, or of the node, if we only want one node to use it.
2 Server startup classes
2.1 Implement the server startup class provider interface
In the same way as for a node startup class, we need to implement the interface JPPFDriverStartupSPI, defined as follows:
public interface JPPFDriverStartupSPI extends JPPFStartup { }
As an example, we will create an implementation that simply prints a message when the server starts:
package org.jppf.example.startup.driver; import org.jppf.startup.JPPFNodeStartupSPI; // This is a test of a server startup class public class TestDriverStartup implements JPPFDriverStartupSPI { public void run() { System.out.println("I'm a server startup class"); } }
A server startup implementation may need information on the JPPF driver itself. To this effect, you can define a method with this exact signature: public void setDriver(JPPFDriver). This method, if defined, will be called exactly once, before the run() method. It allows implementers to access information on the driver via the JPPFDriver API. It can be used as follows:
public class TestDriverStartup implements JPPFDriverStartupSPI { private JPPFDriver driver; @Override public void run() { System.out.println("Server configuration: " + driver.getConfiguration()); } // the JPPF driver will detect that this method exists and call it public void setDriver(JPPFDriver driver) { this.driver = driver; } }
2.2 Create the service definition file
If it doesn't already exist, we create, in the source folder, a subfolder named META-INF/services. In this folder, we will create a file named org.jppf.startup.JPPFDriverStartupSPI, and open it in a text editor. In the editor, we add a single line containing the fully qualified name of our startup class:
org.jppf.example.startup.driver.TestDriverStartup
2.3 Deploy the startup class
Now we just create a jar that contains the JPPF server startup provider class , along with the META-INF/services folder, and add it to the class path of the server.
Main Page > Extending and Customizing JPPF > JPPF startup classes |