I am learning Eclipse Job API but I couldn't find any documentation regarding how to use family class with an example. I am trying to create 3 jobs in which first two belong to same family and other one belong to the different family. I am not able to create a job which can refer to a particular family.
the code is given below: First file containing Job description
public class Jobsclass{
public static void eclipsejobsfamily() {
Job MyFirstJob = new Job("My First Job") {
protected IStatus run(IProgressMonitor monitor) {
System.out.println("Hello World (1st from a background First job)");
return Status.OK_STATUS;
}
public boolean belongsTo(Object family) {
// TODO Auto-generated method stub
if (Jobsutil.MY_FAMILY.equals(family)) { return true; }
return false;
}
};
Job MySecondJob = new Job("My Second Job") {
protected IStatus run(IProgressMonitor monitor) {
System.out.println("Hello World ( 2nd from a background Second job)");
return Status.OK_STATUS;
}
public boolean belongsTo(Object family) {
// TODO Auto-generated method stub
if (Jobsutil.MY_FAMILY.equals(family)) { return true; }
return false;
}
};
Job MyThirdJob = new Job("My First Job") {
protected IStatus run(IProgressMonitor monitor) {
System.out.println("Hello World (1st from a background First job)");
return Status.OK_STATUS;
}
public boolean belongsTo(Object family) {
// TODO Auto-generated method stub
if (Jobsutil.THE_FAMILY.equals(family)) { return true; }
return false;
}
};
MyFirstJob.wakeUp();
MyFirstJob.schedule();
MySecondJob.setPriority(Job.SHORT);
MySecondJob.schedule();
MyThirdJob.wakeUp();
MyThirdJob.schedule();
}
protected IStatus run(IProgressMonitor monitor) {
// TODO Auto-generated method stub
return null;
}
}
FILE 2:
public class Jobsutil {
public static final String MY_FAMILY = "myJobFamily";
public static final String THE_FAMILY = "theOtherFamily";
}
public JobAction() {
}
THIRD Class Method RUN():
/**
* The action has been activated. The argument of the
* method represents the 'real' action sitting
* in the workbench UI.
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
MessageDialog.openInformation(
window.getShell(),
"LearnJobAPI",
"First example");
if (Jobsutil.MY_FAMILY.equals("myJobFamily")){
}
//Job.getJobManager().find("MY_FAMILY");
//Job.getJobManager().wakeUp("MY_FAMILY");
// Jobsclass.eclipsejobsfamily();
this.manager.wakeUp(Jobsutil.MY_FAMILY);
Job[] jobscreated= this.manager.find(Jobsutil.MY_FAMILY);
Jobsclass.eclipsejobsfamily();
}
Thanks in advance. Please help me understand how to control Job using family.
Your Job
classes must override the belongsTo
method to define which families they belong to:
Job myFirstJob = new Job("My First Job") {
@Override
protected IStatus run(IProgressMonitor monitor) {
System.out.println("Hello World (1st from a background First job)");
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == MY_FAMILY;
}
};
Note: Your wakeUp
calls are doing nothing, just call schedule
to run a job.
Also please follow the Java naming conventions - variable names start with lower case. Failing to do this upsets the Stack Overflow code formatter (among other things).
If you want to run the Job
in different families you will have to pass in the family to the Job constructor. Something like:
class MyJob extend Job
{
private final Object jobFamily;
MyJob(String name, Object family) {
super(name);
jobFamily = family;
}
@Override
protected IStatus run(IProgressMonitor monitor) {
System.out.println("Hello World (1st from a background First job)");
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == jobFamily;
}
}
Job myFirstJob = new MyJob("My First Job", "A"); // family 'A'
Job myFirstJob = new MyJob("My First Job", "B"); // family 'B'