adequate
adequate
adequate
adequate
 

JPPF
 Home   About   Download   Documentation   Forums 
May 22, 2013, 12:29:40 PM *
Welcome,
Please login or register.

Login with username, password and session length
Advanced search  
News: Registered users, your contribution is requested! Please participate in our JDK support poll
New users, please read this message. Thank you!
  Home Help Search Login Register  
Pages: [1]   Go Down

Author Topic: could a task cancel itslf?  (Read 441 times)

ernst

  • JPPF Padawan
  • *
  • Posts: 14
could a task cancel itslf?
« on: July 18, 2012, 09:16:04 PM »

hi all,

could a task itself decide it's failed? or cancel itself  an restart maybe on another node?

Logged

lolo

  • Administrator
  • JPPF Council Member
  • *****
  • Posts: 1434
    • JPPF Web site
Re: could a task cancel itslf?
« Reply #1 on: July 19, 2012, 08:43:14 PM »

Hello,

Yes, a task can decide that it failed and act accordingly. JPPF does not provide any API to cancel a single task, however it is possible to cancel the job currently executing in the node.
This requires a custom implementation of a node life cycle listener. Here is a simple example:

Code: [Select]
import org.jppf.management.JMXNodeConnectionWrapper;
import org.jppf.node.event.*;

// Simple node listener implementation that enables tasks
// to cancel the current job while executing in the node.
public class SelfCancelNodeListener implements NodeLifeCycleListener {
  // holds the uuid of the job being executed.
  private  static String currentJobUuid = null;

  @Override
  public void jobStarting(final NodeLifeCycleEvent event)
  {
    SelfCancelNodeListener.setCurrentJobUuid(event.getJob().getUuid());
  }

  @Override
  public void jobEnding(final NodeLifeCycleEvent event)
  {
    // reset the uuid
    SelfCancelNodeListener.setCurrentJobUuid(null);
  }

  // get the uuid of the job being executed.
  public static synchronized String getCurrentJobUuid() {
    return currentJobUuid;
  }

  // set the uuid of the job being executed.
  public static synchronized void setCurrentJobUuid(final String currentJobUuid) {
    SelfCancelNodeListener.currentJobUuid = currentJobUuid;
  }

  // cancel the job. "resubmit = true" means the job should be resubmitted to another node.
  public static void cancelCurrentJob(final boolean resubmit) {
    String uuid = getCurrentJobUuid();
    if (uuid != null) {
      // no-args contructor implies connection to local (same JVM) JMX server
      JMXNodeConnectionWrapper nodeJmx = new JMXNodeConnectionWrapper();
      nodeJmx.connect();
      try {
        nodeJmx.cancelJob(uuid, resubmit);
      } catch (Exception e) {
        System.out.println("error while attempting to cancel job with uuid=" + uuid);
        e.printStackTrace();
      }
    }
  }

  @Override
  public void nodeStarting(final NodeLifeCycleEvent event) {
  }

  @Override
  public void nodeEnding(final NodeLifeCycleEvent event) {
  }
}

Here, we can see that we need the NodeLifeCycleListener implementation so we can gain access to the job uuid, in order to cancel it. Also note the use of the boolean flag in JMXNodeConnectionWrapper.cancelJob(), to indicate whether the job should be resubmitted by the driver.

In your tasks this could be used as follows:

Code: [Select]
import org.jppf.server.protocol.JPPFTask;

// A simple task that may cancel the current job while executing in the node.
public class SelfCancelTask extends JPPFTask {
  @Override
  public void run() {
    // ... some code here ...

    // cancel the job and specify that we want the driver to resubmit it to another node
    SelfCancelNodeListener.cancelCurrentJob(true);
  }

  @Override
  public void onCancel() {
    System.out.println("this task has been cancelled");
  }
}

I hope this helps.

Sincerely,
-Laurent
Logged

ernst

  • JPPF Padawan
  • *
  • Posts: 14
Re: could a task cancel itslf?
« Reply #2 on: July 19, 2012, 10:47:06 PM »

no, i would restart the task. all my tasks are completely independent, so if one fails, it's no a good idea to kill the whole job...
so ok i have to solve this problem in another way
Logged

lolo

  • Administrator
  • JPPF Council Member
  • *****
  • Posts: 1434
    • JPPF Web site
Re: could a task cancel itslf?
« Reply #3 on: July 20, 2012, 08:06:14 AM »

Hello,

I agree this solution is not fitting for your use case.
One possibility would be to have the cancellation logic within the tasks, making it mark the cancelled tasks with a flag, then on the client side check the resulting tasks for this flag, and submit a new job with only those tasks that were cancelled.

Also, if you have advance knowledge of what would cause a task to abort in a specific node, you might want to consider using execution policies to avoid sending those tasks to inappropriate nodes and cancel them as a result. If you could elaborate a little on why your tasks would need to be cancelled, maybe we can devise a more appropriate strategy?

Sincerely,
-Laurent
Logged
Pages: [1]   Go Up
 
Support This Project Powered by SMF 2.0 RC5 | SMF © 2006–2011, Simple Machines LLC Powered by Parallel Matters Get JPPF at SourceForge.net. Fast, secure and Free Open Source software downloads