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 

Receiving notifications from the tasks

From JPPF 6.2 Documentation

Jump to: navigation, search
Main Page > Customizing JPPF > Tasks notifications


We have seen in Task objects > Sending notifications from a task that JPPF tasks can send notifications with the method Task.fireNotification(Object, boolean). It is possible to register listeners for these notifications via the the service provider interface (SPI). These listeners must implement the interface TaskExecutionListener, defined as follows:

public interface TaskExecutionListener extends EventListener {
  // Called by the JPPF node to notify a listener that a task was executed
  void taskExecuted(TaskExecutionEvent event);

  // Called when a task sends a notification via Task.fireNotfication(Object, boolean)
  void taskNotification(TaskExecutionEvent event);
}

The method taskExecuted() is always called by the JPPF node, whereas taskNotification() is always invoked by user code. Both methods receive events of type TaskExecutionEvent, defined as follows:

public class TaskExecutionEvent extends EventObject {
  // Get the JPPF task from which the event originates
  public Task<?> getTask()

  // Get the object encapsulating information about the task
  public TaskInformation getTaskInformation()

  // Get the user-defined object to send as part of the notification
  public Object getUserObject()

  // If true then also send this notification via the JMX Mbean,
  // otherwise only send to local listeners
  public boolean isSendViaJmx()
}

Once a task execution listener is implemented, it is plugged into the JPPF nodes using the service provider interface:

  • create a file in META-INF/services named org.jppf.node.event.TaskExecutionListener
  • in this file, add the fully qualified class name of your implementation of the interface
  • copy the jar file or class folder containing your implementation and service file to the classpath of the server, if you want all the nodes to register a listner instance, or to the classpath of specific individual nodes.


The following example prints the notifications it receives to the node's output console:

package my.test;
import ...;

public class MyTaskListener implements TaskExecutionListener {
  @Override
  public synchronized void taskExecuted(TaskExecutionEvent event) {
    System.out.println("Task " + event.getId() + " completed with result : " +
      event.getTask().getResult());
    System.out.println("cpu time = " + event.getTaskInformation().getCpuTime() +
      ", elapsed = " + event.getTaskInformation().getElapsedTime());
  }

  @Override
  public synchronized void taskNotification(TaskExecutionEvent event) {
    System.out.println("Task " + event.getId() + " sent user object : " +
      event.getUserObject());
  }
}

To use this listener, you need to create a file META-INF/services/org.jppf.node.event.TaskExecutionListener containing the line “my.test.MyTaskListener” and add it, along with the implementation class, to the server's or node's classpath.

Main Page > Customizing JPPF > Tasks notifications



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