Palladio Workflow Engine/Hello World Tutorial

Aus SDQ-Wiki
Wechseln zu: Navigation, Suche

Introduction

In this tutorial we build a simple Job and successively extend the example.

As a general note: The code provided in this tutorial is examplary code and not intended for production. To provide a more compact tutorial we have also removed a lot of comments etc. This is not aligned with our coding guidelines but accepted here for space reasons.

An Eclipse project containing the HelloWorld example sources is available in the svn:
https://svnserver.informatik.kit.edu/i43/svn/code/WorkflowEngine/trunk/de.uka.ipd.sdq.workflow.helloworld/
(user: anonymous /password: anonymous)

The First Hello World Job

First, we start with a simple job just saying hello to whomever provided to its constructor:

 1 package de.uka.ipd.sdq.workflow.helloworld;
 2 
 3 import org.eclipse.core.runtime.IProgressMonitor;
 4 
 5 import de.uka.ipd.sdq.workflow.jobs.CleanupFailedException;
 6 import de.uka.ipd.sdq.workflow.jobs.IJob;
 7 import de.uka.ipd.sdq.workflow.jobs.JobFailedException;
 8 import de.uka.ipd.sdq.workflow.jobs.UserCanceledException;
 9 
10 public class HelloWorldJob implements IJob {
11   private String who = "World";
12 	
13   public HelloWorldJob(String who) {
14       this.who = who;
15   }  
16   
17   @Override
18   public void execute(IProgressMonitor monitor) throws JobFailedException, UserCanceledException {
19       System.out.println("Hello "+who); // job specific processing
20   }
21 
22   @Override
23   public String getName() {
24       return "Hello World Job";
25   }
26 
27   @Override
28   public void cleanup(IProgressMonitor monitor) throws CleanupFailedException {
29       // Nothing to clean up after a run.
30   }
31 }

The most straightforward way to realize a job is to implement the IJob interface. The interface requires to provide a method named execute() which takes a progress monitor as a parameter one can report its job specific progress to.
Within the execute() method we print our hello message to the system output (line 19). This is the place your custom logic / processing has to be implemented.
During execution, a job can check if the user has canceled the processing and throw a UserCanceledException.
In addition, a JobFailedException can be thrown to identify that the job could not be processed successfully.

Providing the getName() method, a Job has to return a name which identifies him e.g. in context of a workflow.
The cleanup() method can contain any clean up of blocked resources etc. and is called by the workflow lifecycle management.

Job Execution

Jobs can be executed directly. To give this a first try, we use a Main class with a standalone main() method and provide a NullProgressMonitor to the job:

 1 package de.uka.ipd.sdq.workflow.helloworld;
 2 
 3 import org.eclipse.core.runtime.NullProgressMonitor;  
 4 import de.uka.ipd.sdq.workflow.jobs.JobFailedException;
 5 import de.uka.ipd.sdq.workflow.jobs.UserCanceledException;
 6 
 7 public class Main {
 8   
 9   public static void main(String[] args) throws JobFailedException, UserCanceledException {
10     HelloWorldJob job = new HelloWorldJob("World");
11     job.execute(new NullProgressMonitor());
12   }
13   
14 }

The example will prompt Hello World to our console.

The Hello World Workflow

In the example above, we executed a job in a very raw manner. For example, the cleanup() method has not been called afterward, which is essential because a job might need to free some resources etc.
So a better way to run a job is to execute it in the context of a Workflow. A Workflow itself is a specific type of the a job which encapsulates other jobs and takes care for their lifecycle. In addition, a workflow provides some logging facilities to provide information about the executed jobs as well as error handling.

In the example below, we create a workflow, and add our hello world job to it.

 1 package de.uka.ipd.sdq.workflow.helloworld;
 2 
 3 import org.apache.log4j.BasicConfigurator;
 4 import org.apache.log4j.ConsoleAppender;
 5 import org.apache.log4j.PatternLayout;
 6 import de.uka.ipd.sdq.workflow.Workflow;
 7 import de.uka.ipd.sdq.workflow.jobs.JobFailedException;
 8 import de.uka.ipd.sdq.workflow.jobs.UserCanceledException;
 9 
10 public class MainWorkflow {
11 
12   public static void main(String[] args) {
13     
14     // set up a basic logging configuration
15     BasicConfigurator.resetConfiguration();
16     BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%m%n")));
17         
18     HelloWorldJob job = new HelloWorldJob("World");
19     Workflow myWorkflow = new Workflow(job);
20     myWorkflow.run();
21   }
22 }

Note: Our main method no longer throws exceptions. The workflows run method (line 12) already encapsulates and handles any exceptions thrown by the jobs. In line 15 and 16 we initialize a log4j logging infrastructure. The workflow automatically logs workflow information to a log4j logger which requires a prepared log4j environment. Typically, this is already done in your environment. Furthermore, the Palladio Workflow Engine provides infrastructure to send those logs to the console of your active Eclipse instance: Palladio Workflow Engine/Logging.

Now, when we execute our program we get the following output on our console:

 Creating workflow engine and starting workflow
 Palladio Workflow-Engine: Running job Hello World Job
 Hello World
 Task Sequential Job Execution completed in 1.57792E-4 seconds
 Cleaning up...
 Workflow engine completed task

As you can see, the workflow takes care for and reports about the workflow lifecycle.

Composed Hello World Workflow Chain

A workflow executing a single job is not very interesting. To build "real" workflows, jobs can be plugged together to build more complex composite jobs. The Palladio Workflow Engine implements its composition concept based on specific composite job types: (see also Composite Job Types)

Two different main composite types are provided which again could be composed into more complex jobs: SequentialJob and ParallelJob. In the following, we extend our hello world example to make use of these two types.

Sequential Job Composition

To execute a sequence of jobs, simply create a SequentialJob and add your jobs to be executed in one after each other (See code example below).
In addition to the default constructor the SquentialJob provides a constructor allowing to pass a boolean parameter to decide if the cleanup() method of each job should be called immediately after its execution (true) or after all jobs have been completed (false). The immediate clean up is the default behaviour to free resources as soon as possible.

 1 package de.uka.ipd.sdq.workflow.helloworld;
 2 
 3 import org.apache.log4j.BasicConfigurator;
 4 import org.apache.log4j.ConsoleAppender;
 5 import org.apache.log4j.PatternLayout;
 6 
 7 import de.uka.ipd.sdq.workflow.Workflow;
 8 import de.uka.ipd.sdq.workflow.jobs.JobFailedException;
 9 import de.uka.ipd.sdq.workflow.jobs.SequentialJob;
10 import de.uka.ipd.sdq.workflow.jobs.UserCanceledException;
11 
12 public class MainSequentialWorkflow {
13  
14   public static void main(String[] args) {
15     
16     // set up a basic logging configuration
17     BasicConfigurator.resetConfiguration();
18     BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%m%n")));
19         
20     SequentialJob jobSequence = new SequentialJob();
21     
22     jobSequence.add(new HelloWorldJob("Palladio"));
23     jobSequence.add(new HelloWorldJob("Workflow"));
24     jobSequence.add(new HelloWorldJob("Engine"));
25     
26     Workflow myWorkflow = new Workflow(jobSequence);
27     myWorkflow.run();
28   }
29 }

The code above creates a workflow executing three HelloWorldJobs greeting "Palladio", "Workflow", "Engine" as shown in this activity diagram:
Palladio-workflow-engine-sequential-hello-world-example.png

Executing the code above will produce the following console output:

 Creating workflow engine and starting workflow
 Palladio Workflow-Engine: Running job Sequential Job
 Palladio Workflow-Engine: Running job Hello World Job
 Hello Palladio
 Palladio Workflow-Engine: Running job Hello World Job
 Hello Workflow
 Palladio Workflow-Engine: Running job Hello World Job
 Hello Engine
 Task Sequential Job Execution completed in 2.32203E-4 seconds
 Task Workflow Execution completed in 5.90558E-4 seconds
 Cleaning up...
 Workflow engine completed task

Parallel Job Composition

 1 package de.uka.ipd.sdq.workflow.helloworld;
 2 
 3 import org.apache.log4j.BasicConfigurator;
 4 import org.apache.log4j.ConsoleAppender;
 5 import org.apache.log4j.PatternLayout;
 6 import de.uka.ipd.sdq.workflow.Workflow;
 7 import de.uka.ipd.sdq.workflow.jobs.JobFailedException;
 8 import de.uka.ipd.sdq.workflow.jobs.ParallelJob;
 9 import de.uka.ipd.sdq.workflow.jobs.UserCanceledException;
10 
11 public class MainParallelWorkflow {
12 
13   public static void main(String[] args) {
14     
15     // set up a basic logging configuration
16     BasicConfigurator.resetConfiguration();
17     BasicConfigurator.configure(new ConsoleAppender(new PatternLayout("%m%n")));
18         
19     ParallelJob parallelJob = new ParallelJob();
20     
21     parallelJob.add(new HelloWorldJob("Palladio"));
22     parallelJob.add(new HelloWorldJob("Workflow"));
23     parallelJob.add(new HelloWorldJob("Engine"));
24     
25     Workflow myWorkflow = new Workflow(parallelJob);
26     myWorkflow.run();
27   }
28 }


The code above creates a workflow executing three HelloWorldJobs greeting "Palladio", "Workflow", "Engine" as shown in this activity diagram:
Palladio-workflow-engine-parallel-hello-world-example.png

Executing this workflow will produce the following logging output:

 Creating workflow engine and starting workflow
 Palladio Workflow-Engine: Running job CompositeJob <Hello World Job Hello World Job Hello World Job >
 Hello Palladio
 Hello Workflow
 Hello Engine
 Task Workflow Execution completed in 0.002190325 seconds
 Cleaning up...
 Workflow engine completed task

As you can see, the hello messages are produced in the order they have been placed in the parallel job but they do not have to. Each parallelized job is executed in a separate Thread and the order of job execution depends on your thread management.
The ParallelJob provides a constructor to specify the number of threads to be used to execute the parallel jobs. The default setting is -1 which specifies to use the CPUs default setting of available threads.

However, an important point is, that the parallelized Jobs of the Workflow Engine are not deamons as when executing parallel tasks of Eclipse. So they are joined again and one can build workflows with parallized jobs followed by another job which is executed after the parallel job executions have been joined.