JPPF, java, parallel computing, distributed computing, grid computing, parallel, distributed, cluster, grid, cloud, open source, android, .net
JPPF, java, parallel computing, distributed computing, grid computing, parallel, distributed, cluster, grid, cloud, open source, android, .net
JPPF

The open source
grid computing
solution

 Home   About   Features   Download   Documentation   On Github   Forums 

JPPF startup classes

From JPPF 6.0 Documentation

Revision as of 09:41, 27 May 2017 by Lolocohen (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

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.

Related sample: “Startup Classes” sample in the JPPF samples pack.


1 Node startup classes

Step 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 {
  public void run() {
    System.out.println("I'm a node startup class");
  }
}

Step 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

Step 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.

Important note: when a node startup class is deployed on the server, the objects it creates (for instance as singletons) can be reused from within the tasks executed by the node.

2 Server startup classes

Step 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");
  }
}

Step 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

Step 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



JPPF Copyright © 2005-2020 JPPF.org Powered by MediaWiki