Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make simple workflow from existing code?

Tags:

java

workflow

I have created several tasks, each takes an input, an execution function which keeps updating its status, and a function to get output of this task. They will execute in serial or parallel. Some outputs are List so there will be loops as well.

public class Task1 { //each task looks like this
    void addInput(String key, String value){
       ....
    }
    void run(){
       ....
       updateStatus();
       ....
    }
    HashMap getOutput(){
       ....
    }
    Status getStatus(){
       ....
    }
}

I want to make a workflow from these tasks and then I will use the workflow structure information to build a dynamic GUI and monitor outputs of each task. Do I have to write a workflow execution system from scratch or is there any simple alternative available?

Is there any workflow engine to which I can give (in XML may be) my Java classes, input and output and execution functions and let it execute?

like image 698
SMUsamaShah Avatar asked Sep 24 '14 11:09

SMUsamaShah


2 Answers

In Java world, your use case is called as BPM (Business process management).

In .Net world, this is called as Windows Workflow Foundation (WWF).

There are many java based open source BPM tools. The one i like is jBPM.

This is more powerful and can be integrated with rule engines like Drools.

Sample jBPM Screenshot: jBPM Diagram

Also Activiti is another good choice.

Sample Activiti Screenshot: Activiti Diagram

like image 114
thangamanikasi Avatar answered Oct 08 '22 11:10

thangamanikasi


Check out Activity. This is not strictly designed to solve your use case, but you may use and adapt it's process engine to help you because it's written purely in java. Activity is rather process modeling engine so it's not designed to controll tasks running in parallel at runtime, howerver you will get many things which you can reuse "out of the box".

  • You will get tasks linking basing on xml file
  • You will get your gui for linking tasks for free (basing on eclipse)
  • You will get the GUI in web browser to browse running processes, start new, and see current status of the tasks: http://activiti.org/userguide/index.html#N12E60
  • You will get the reporting engine for free, where you can see reports and charts ("how long time did the tasks take", "how long was the process was running"
  • You will get the REST API for free. Other application will be able to get the current state of your application via simple REST calls

So going in this direction you will get many things for free. From programmer point of View you can for example inherit from Task class from Activity api. Late when the task is completed call

taskService.complete(task.getId(), taskVariables);

You can also go another way arround. So supossing that your class which calculates in background is called CalculationTask, you can connect CalculatinTasks with new instance of Activity Task. By this you will get a bridge to Activity process engine. So you can do something like

class CustomActivityTask extends Task { // inherit from Activity Task class to add your own fields
    private int someStateOne;
    private String someOtherState;

   (...)
   // getters and setters

}

class CalculationTask {
   private CustomActivityTask avtivityTask; // by updating the state of this task you are updating the state of the task in Activity process engine 
   private RuntimeService activityRuntimeServiece;

   public void run() { // this is your execution functin

      while (true) { 
         // calulate
         activityTask.setSomeStateOne(45)
         activityTask.setSomeOtherState("Task is almost completing..."); 

         (...)

         if (allCompleted) {
             activityRuntimeServiece.complete(avtivityTask.getId(), taskVariables);
             break;

        }

  }
}
like image 41
walkeros Avatar answered Oct 08 '22 10:10

walkeros