Pages

Saturday, May 25, 2013

HumanTask Event Listeners - WSO2 BPS


HumanTask engine comes with a humantask event generator. These task events are generated when a task goes trough a state transition. An event contains details about tasks and its state transition. So by writing a custom event listener to these events, a developer can easily enhance the HumanTask engine's functionalities.

What developer can do with a task event listener.
  • Can do custom java code/ web service invocations.
  • Can retrieve task information. 
  • Developer free to implement their own logic depending on their requirement.

Sample Event Listener.

EventListener class should implement HumanTaskEventListener class and onEvent method. See following sample code.
package org.wso2.carbon.humantask.sample;
import org.wso2.carbon.humantask.core.api.event.HumanTaskEventListener;
import org.wso2.carbon.humantask.core.api.event.TaskEventInfo;
import org.wso2.carbon.humantask.core.dao.TaskType;
public class SimpleEventListener implements HumanTaskEventListener {
@Override
public void onEvent(TaskEventInfo taskEventInfo) {
System.out.println("[ Task ID : " + taskEventInfo.getTaskInfo().getId() + " ] [ Event : " + taskEventInfo.getEventType() + " ] at " + taskEventInfo.getTaskInfo().getModifiedDate());
if (taskEventInfo.getTaskInfo().getType() == TaskType.TASK) {
System.out.println("\tTask Name :" + taskEventInfo.getTaskInfo().getName());
System.out.println("\tTask Subject :" + taskEventInfo.getTaskInfo().getSubject());
System.out.println("\tTask Description :" + taskEventInfo.getTaskInfo().getDescription());
System.out.println("\tTask Owner : " + taskEventInfo.getTaskInfo().getOwner());
} else if (taskEventInfo.getTaskInfo().getType() == TaskType.NOTIFICATION) {
System.out.println("\tNotification Name :" + taskEventInfo.getTaskInfo().getName());
System.out.println("\tNotification Subject :" + taskEventInfo.getTaskInfo().getSubject());
System.out.println("\tNotification Description :" + taskEventInfo.getTaskInfo().getDescription());
}
}
}


How to deploy a custom listener to WSO2 BPS

  1. Build a jar file of including your listener.
  2. Copy it into /repository/components/lib. Where is the root directory of BPS server.
  3. Uncomment TaskEventListeners in HumanTask configuration file (/repository/conf/humantask.xml) and give your Event Listener class name for ClassName.
  4. eg:
    <TaskEventListeners>
    <ClassName>org.wso2.carbon.humantask.sample.SampleEventListener</ClassName>
    </TaskEventListeners>
    view raw humantask.xml hosted with ❤ by GitHub

    Note: you can have multiple listener classes.
  5. Start BPS.
  6. Execute a task.

No comments:

Post a Comment