jBPM 5 In-Memory Task Service

Leave a comment

December 10, 2011 by huionn

Based on the discussion in http://community.jboss.org/thread/162963, I am able to rewrite https://huionn.wordpress.com/2011/06/26/ad-hoc-task-in-jbpm-5/ to be in-memory task service (instead of using task server through Hornet).

As my current application targets at small firms, BPM using BPMN is overkill. So I decide to remove the dependency of WorkItem/WorkManager from task service.

Direct use of TaskServiceSession  API is simpler (without chained response handlers)  and likely to be faster (without overhead of serialization and deserialization).

In addition, I also learn how to set and get ContentData of tasks this time.

public class InMemoryAdHocTaskTest extends BaseTest {

	protected void setUp() throws Exception {

	public void testAdHocTask() throws Exception {

		// Create the parent task
		Task newParentTask = createParentTask();
		taskSession.addTask(newParentTask, null);

		// Test if the task is successfully created
		List<TaskSummary> tasks = taskSession.getTasksAssignedAsPotentialOwner(
				"Darth Vader", "en-UK");
		assertEquals(1, tasks.size());
		TaskSummary task = tasks.get(0);
		// Create the child task
		Task newSubTask1 = createTask("TaskNameChild1", "CommentChild1",
				"Bobba Fet", task);
		taskSession.addTask(newSubTask1, null);

		// Create the child task2
		Task newSubTask2 = createTask("TaskNameChild2", "CommentChild2",
				"Jabba Hutt", task);
		taskSession.addTask(newSubTask2, null);

		// Start the parent task
		System.out.println("Starting task " + task.getId());
		startTask(task, "Darth Vader");
		System.out.println("Started task " + task.getId());

		// Check if the parent task is InProgress
		Task parentTask = taskSession.getTask(task.getId());
		// Get all the subtask created for the parent task based on the
		// potential owner
		List<TaskSummary> subTasks = taskSession.getSubTasksByParent(
				parentTask.getId(), "en-UK");
		TaskSummary subTaskSummary1 = subTasks.get(0);
		TaskSummary subTaskSummary2 = subTasks.get(1);
		addEventListener(subTaskSummary1.getId(), task);
		addEventListener(subTaskSummary2.getId(), task);

		// Starting the sub task 1
		System.out.println("Starting sub task " + subTaskSummary1.getId());
		startTask(subTaskSummary1, "Bobba Fet");
		System.out.println("Started sub task " + subTaskSummary1.getId());

		// Starting the sub task 2
		System.out.println("Starting sub task " + subTaskSummary2.getId());
		startTask(subTaskSummary2, "Jabba Hutt");
		System.out.println("Started sub task " + subTaskSummary2.getId());

		// Check if the child task 1 is InProgress
		Task subTask1 = taskSession.getTask(subTaskSummary1.getId());
		// Check if the child task 2 is InProgress
		Task subTask2 = taskSession.getTask(subTaskSummary2.getId());

		// Complete the child task 1
		System.out.println("Completing sub task " + subTask1.getId());
		HashMap<String, String> result = new HashMap<String, String>();
		result.put("status", "OK");
		completeTask(subTask1, "Bobba Fet", result);
		System.out.println("Completed sub task " + subTask1.getId());

		// Complete the child task 2
		System.out.println("Completing sub task " + subTask2.getId());
		HashMap<String, String> result2 = new HashMap<String, String>();
		result2.put("result", "100");
		result2.put("approved", "yes");
		completeTask(subTask2, "Jabba Hutt", result2);
		System.out.println("Completed sub task " + subTask2.getId());

		// Check if the child task 1 is Completed

		subTask1 = taskSession.getTask(subTask1.getId());
		// Check if the child task 2 is Completed
		subTask2 = taskSession.getTask(subTask2.getId());
		// check results of 2 sub tasks
		parentTask = taskSession.getTask(parentTask.getId());
		// dummy check for sub task results
		for (Comment c : parentTask.getTaskData().getComments()) {
			System.out.println("subtask result: " + c.getText());

		long outputContent1 = subTask1.getTaskData().getOutputContentId();
		Content content = taskSession.getContent(outputContent1);
		Map resultMap = (Map) ContentDataUtils
		long outputContent2 = subTask2.getTaskData().getOutputContentId();
		content = taskSession.getContent(outputContent2);
		resultMap = (Map) ContentDataUtils.fromContent(content.getContent());

		completeTask(parentTask, "Darth Vader", null);

		// Check is the parent task is Complete
		parentTask = taskSession.getTask(parentTask.getId());		

	private void completeTask(Task task, String userId, Serializable result) {
		if (result != null) {
			taskSession.taskOperation(Operation.Complete, task.getId(), userId,
					null, ContentDataUtils.toContentData(result), null);
		} else {
			taskSession.taskOperation(Operation.Complete, task.getId(), userId,
					null, null, null);

	private void startTask(TaskSummary task, String userId) {
		taskSession.taskOperation(Operation.Start, task.getId(), userId, null,
				null, null);

	private Task createTask(String taskName, String comment, String actorId,
			TaskSummary parentTask) {
		Task task = new Task();
		if (taskName != null) {
			List<I18NText> names = new ArrayList<I18NText>();
			names.add(new I18NText("en-UK", taskName));
		if (comment != null) {
			List<I18NText> descriptions = new ArrayList<I18NText>();
			descriptions.add(new I18NText("en-UK", comment));
			List<I18NText> subjects = new ArrayList<I18NText>();
			subjects.add(new I18NText("en-UK", comment));

		TaskData taskData = new TaskData();
		if (parentTask != null) {
			// Sub Task Data

		PeopleAssignments assignments = new PeopleAssignments();
		List<OrganizationalEntity> potentialOwners = new ArrayList<OrganizationalEntity>();

		if (actorId != null && actorId.trim().length() > 0) {
			String[] actorIds = actorId.split(",");
			for (String id : actorIds) {
				potentialOwners.add(new User(id.trim()));
			// Set the first user as creator ID??? hmmm might be wrong
			if (potentialOwners.size() > 0) {
				taskData.setCreatedBy((User) potentialOwners.get(0));

		List<OrganizationalEntity> businessAdministrators = new ArrayList<OrganizationalEntity>();
		businessAdministrators.add(new User("Administrator"));


		return task;

	private Task createParentTask() {
		Task parentTask = createTask("TaskNameParent", "CommentParent",
				"Darth Vader", null);
		List<SubTasksStrategy> subTaskStrategies = new ArrayList<SubTasksStrategy>();
		subTaskStrategies.add(new OnParentAbortAllSubTasksEndStrategy());

		return parentTask;

	private void addEventListener(long taskId, TaskSummary parentTask) {
		taskService.addEventListener(new SimpleTaskEventListener(parentTask));

	public class SimpleTaskEventListener extends DefaultTaskEventListener {
		private final TaskSummary parentTask;

		public SimpleTaskEventListener(TaskSummary parentTask) {
			this.parentTask = parentTask;

		public void taskCompleted(TaskCompletedEvent event) {
			System.out.println("Task " + event.getTaskId() + " completed");


The codes are modified from WSHumanTaskHandlerBaseTest.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: