了解Eclipse作业系列

时间:2016-02-27 08:25:13

标签: eclipse eclipse-plugin eclipse-rcp jobs belongs-to

我正在学习Eclipse Job API,但我找不到任何关于如何使用带有示例的族类的文档。我正在努力创造3个工作,其中前两个属于同一个家庭,另一个属于不同的家庭。我无法创建一个可以引用特定家庭的工作。

代码如下:    包含职位描述的第一个文件

    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();
    }

提前致谢。请帮助我了解如何使用家人控制工作。

1 个答案:

答案 0 :(得分:1)

您的Job类必须覆盖belongsTo方法,以定义它们属于哪个系列:

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;
     }
};

注意:您的wakeUp来电无效,只需致电schedule即可开始工作。

另请遵循Java命名约定 - 变量名称以小写字母开头。如果不这样做会扰乱Stack Overflow代码格式化程序(除其他外)。

如果要在不同的系列中运行Job,则必须将系列传递给Job构造函数。类似的东西:

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'