Java service task with Activiti workflow
Activiti provides the Java service task that allows to invoke an external Java class. And here, we will see how to do it. You shoud practice the previous tutorial to be clear with the process model design before working on this.
Create an external Java class as a jar file
In Eclipse, create a new Maven Project.
In your project, edit the file
pom.xml by adding:
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>5.22.0</version>
</dependency>
Create a package org.activiti.
In the package
org.activiti, create a class
MyJavaDelegate with the following code:
package org.activiti;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class MyJavaDelegate implements JavaDelegate {
public void execute(DelegateExecution execution) throws Exception {
String var = (String) execution.getVariable("input");
var = var.toUpperCase();
execution.setVariable("input", var);
}
}
Right click on your project and select Run As→Maven Install to build a jar file.
Copy a jar file in the folder target to apache-tomcat-7.0.12\webapps\activiti-explorer\WEB-INF\lib
Restart your tomcat.
Design the "Displaying Upper Case" process model
In Activiti, login as admin with Processes→Model workspace→New model.
Enter new model Name: Displaying Upper Case. Click Create.
Firstly, enter the following process properties:
Process identifier: uppercase
Name: Displaying Upper Case
Design a process model by drag and drop model elements, you should have something similar to this picture:
On the start event, specify the following properties:
Initiator: initiator
Form properties:
id: input, Name: Input, Type: string
On the Get uppercase activity, specify the property Class: org.activiti.MyJavaDelegate
On the output activity, specify the following properties:
Assignments→Assignee: ${initiator}
Form properties:
id: input, Name: Output, Type: string
Click on Save the model icon, then click on Save and close editor.
In Processes→Model workspace, select your Displaying Upper Case process model, then go to Model action and select Deploy.
Executing a process model
Go to Processes→Deployed process definitions, select your newly created process model, then click on Start process.
Enter some text in the
input, then click on
Start process*.
- Go to Processes→My instances
to see the status of your executing process.
As you can see, the service task Get uppercase
has automatically executed and invoked an external java class MyJavaDelegate
. Now the current task is output
which needed to be handled by you.
- Select Tasks→Inbox
to see the output, then click on Complete task** button to end this process.
Back to top