4.15. Domain-Specific Tasks
A domain-specific task is a task with custom properties and handling for a given domain or company. You can use it repeatedly in different business processes and accommodate interactions with other technical system.
In Red Hat JBoss BPM Suite, domain-specific task nodes are referred to as custom work items or custom service nodes.
When creating custom work items, define the following:
- Work Item Handler
A work item handler is a Java class that defines how to execute a custom task. Tasks are executed in the Execution Engine, which contains a work item handler class, that defines how to handle the particular work item. For the Execution Engine to execute your custom work item, you need to:
- Create a work item handler class for the custom work item.
- Register the work item handler with the Execution Engine.
- Work Item Definition
- A work item definition defines how the custom task is presented (its name, icon, parameters, and similar attributes).
4.15.1. Work Item Definition
You can define a work item definition in:
- Red Hat JBoss Developer Studio Process Designer
- Web Process Designer
A work item has the following properties:
-
name– A unique identifier in the given work item set. -
description– A description of your work item set. -
version– A version number. -
parameters– A set of work item parameters used as properties. -
displayName– A name displayed in the palette. -
icon– A path to the icon file. -
category– A name of the node in the palette into which the work item is added. If the defined category does not exit, a new category is created. -
defaultHandler– AWorkItemHandlerclass that is used to execute the work item. -
dependencies– The dependencies for thedefaultHandlerclass.
4.15.2. Creating Custom Work Item Definition
JBoss Developer Studio Process Designer
To create a custom work item definition (WID) in JBoss Developer Studio Process Designer, follow these steps:
Create
WID_NAME.widinMETA-INF. For example,$PROJECT_HOME/src/main/resources/META-INF/WID_NAME.wid.This file is identical to a work item definition file created in Business Central.
-
Copy all the icons you want to use into
$PROJECT_HOME/src/main/resources/icons.
Web Process Designer
To create a custom work item definition (WID) in the Web Process Designer, follow these steps:
- Log into Business Central.
- Click Authoring → Project Authoring.
- Choose the organizational unit and repository of your project to view the assets in your project.
Click WORK ITEM DEFINITIONS → WorkDefinitions.
The WorkDefinitions asset is created by default and contains a number of pre-set work item definitions.
The Work Item Definitions editor opens. Add your WID at the end, for example:
[ "name" : "Google Calendar", "description" : "Create a meeting in Google Calendar", "version" : "1.0", "parameters" : [ "FilePath" : new StringDataType(), "User" : new StringDataType(), "Password" : new StringDataType(), "Body" : new StringDataType() ], "displayName" : "Google Calendar", "icon" : "calendar.gif", ]
Add the imports required by your WID. For example:
import org.drools.core.process.core.datatype.impl.type.StringDataType; import org.drools.core.process.core.datatype.impl.type.ObjectDataType;
NoteYou have to separate the previous definition with a comma ",". Otherwise, the validation will fail.
- Click Validate to make sure your definition is correct.
- Click Save.
To upload a custom icon for your work item definition, follow these steps:
- Click New Item → Uploaded file.
- In the Create new Uploaded file dialogue box, define the resource name, including file extension.
-
Click Choose File and upload the file (
pngorgif, 16x16 pixels). - Click Ok.
You can now refer to your icon in your WID. Your WID is in the Process Designer, in the Service Tasks section by default.
4.15.3. Work Item Handler
A work item handler is a Java class used to execute or abort (during asynchronous execution) work items. The class defines the business logic of the work item, for example how to contact another system and request information, which is then parsed into the custom task parameters. Every work item handler must implement org.kie.api.runtime.process.WorkItemHandler, which is a part of the KIE API.
For more information about work item handlers, see Appendix B. Service Tasks from Red Hat JBoss BPM Suite User Guide.
You can customize the behavior of your work item by registering different work item handlers on different systems.
Red Hat JBoss BPM Suite comes with multiple work item handlers in the following modules:
-
The
jbpm-bpm2module in theorg.jbpm.bpmn2.handlerpackage contains the following work item handlers: - ReceiveTaskHandler (for the BPMN <receiveTask> element)
- SendTaskHandler (for the BPMN <sendTask> element)
- ServiceTaskHandler (for the BPMN <serviceTask> element)
-
The
jbpm-workitemsmodule in packages withinorg.jbpm.process.workitemcontains, for example: - ArchiveWorkItemHandler
- WebServiceWorkItemHandler
- TransformWorkItemHandler
- RSSWorkItemHandler
- RESTWorkItemHandler
- JavaInvocationWorkItemHandler
- JabberWorkItemHandler
- JavaHandlerWorkItemHandler
- FTPUploadWorkItemHandler
- ExecWorkItemHandler
- EmailWorkItemHandler
The work item handlers must define the executeWorkItem() and abortWorkItem() methods as defined by the WorkItemHandler interface. These are called during runtime on work item execution.
When a work item is executed, the following is performed:
- Information about the task is extracted from the WorkItem instance.
- The work item business logic is performed.
The Process instance is informed that the work item execution finished (as completed or aborted) using the respective method of the WorkItemManager:
public class GoogleCalendarHandler implements WorkItemHandler { @Override public void executeWorkItem(WorkItem workItem, WorkItemManager manager) { Map<String, Object> results = new HashMap<String, Object>(); // obtain parameters String filePath = (String) workItem.getParameter("FilePath"); String user = (String) workItem.getParameter("User"); // execute the custom logic here // pass results to next processing, for example Object result; results.put("Result", result); manager.completeWorkItem(workItem.getId(), results) } @Override public void abortWorkItem(WorkItem workItem, WorkItemManager manager) { manager.abortWorkItem(workItem.getId()); } }If you use the work item in a maven project, you need to declare the following dependency:
<dependency> <groupId>org.jbpm</groupId> <artifactId>jbpm-workitems</artifactId> <version>6.4.0.Final-redhat-3</version> </dependency>
If a work item cannot be completed immediately and it is required that the Process execution continues while the work item completes the execution, the Process execution can continue asynchronously and the work item manager can be notified about the work item completion later.
To abort the work item, use the WorkItemHandler.abortWorkItem() before it is completed. For more information about asynchronous execution, see Section 5.1.4.1, “Asynchronous execution” .
4.15.4. Registering Work Item handler in Business Central
To register a work item handler in Business Central, follow these steps:
Procedure: Uploading JAR File
- Log into Business Central.
- Click Authoring → Artifact repository.
- Click Upload and select the JAR file of your work item handler.
- Click Upload.
Procedure: Adding Dependencies
- Click Authoring → Project Authoring.
- Click Open Project Editor.
- Click Project Settings: Project General Settings and select Dependencies list from the menu.
- Click Add from repository and select the file you have uploaded.
Procedure: Registering Work Item Handler
- Click Authoring → Project Authoring.
- Click Open Project Editor.
- Click Project Settings: Project General Settings and select Deployment descriptor from the menu.
- Navigate to Work Item handlers and click Add.
-
Enter the name of your custom work item definition into the first
Valuefield with no white spaces. For example, GoogleCalendar. -
Instantiate your work item handler in the second field. For example, if your work item is in the
com.samplepackage,new com.sample.GoogleCalendarHandler(). - Click Save.
If you want your work item handler to be available for all your projects, place the JAR file in DEPLOY_DIR/business-central.war/WEB-INF/lib/.
If you want to register your work item handler for all your projects, you can do so in {SERVER_HOME}/business-central.war/WEB-INF/classes/META-INF/kie-wb-deployment-descriptor.xml.
4.15.5. Registering Work Item Handler Outside of Business Central
If you use RuntimeManager directly, use the following example:
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder()
.userGroupCallback(userGroupCallback)
.addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
.registerableItemsFactory(new DefaultRegisterableItemsFactory() {
@Override
public Map<String, WorkItemHandler> getWorkItemHandlers(RuntimeEngine runtime) {
Map<String, WorkItemHandler> handlers = super.getWorkItemHandlers(runtime);
handlers.put("async", new AsyncWorkItemHandler(executorService, "org.jbpm.executor.commands.PrintOutCommand"));
return handlers;
}
})
.get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);-
userGroupCallbackis in theorg.jbpm.services.task.identitypackage. -
executorServiceis in theorg.jbpm.executor.ExecutorServiceFactorypackage.
If you want to include custom WorkItemHandler, you can implement the RegisterableItemsFactory interface. Alternatively, you can extend the following existing implementation and add your handlers:
-
org.jbpm.runtime.manager.impl.SimpleRegisterableItemsFactory -
org.jbpm.runtime.manager.impl.DefaultRegisterableItemsFactory -
org.jbpm.runtime.manager.impl.KModuleRegisterableItemsFactory -
org.jbpm.runtime.manager.impl.cdi.InjectableRegisterableItemsFactory
For further information about the implementation, see the org.jbpm.runtime.manager.impl.* package.
The recommended practice is to use the Service API and register your work item handlers in KJAR in kie-deployment-descriptor.xml.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.