How to Customize Work Item Handlers in jBPM

W/ Configuration File

1) Create the SampleWorkItemHandler.java file:

import java.util.HashMap;
import java.util.Map;

import org.drools.runtime.process.WorkItem;
import org.drools.runtime.process.WorkItemHandler;
import org.drools.runtime.process.WorkItemManager;

public final class SampleWorkItemHandler implements WorkItemHandler {

  public void abortWorkItem(final WorkItem item, final WorkItemManager manager) {
    manager.abortWorkItem(item.getId());
  }

  public void executeWorkItem(final WorkItem item, final WorkItemManager manager) {
    final Map<String, Object> resultMap = new HashMap<String, Object>();

    resultMap.put("name", "value");

    manager.completeWorkItem(item.getId(), resultMap);
  }

}

2) Create the /META-INF/SampleWorkItemHandlers.conf file:

[
  "Sample": new SampleWorkItemHandler()
]

3) Create the /META-INF/drools.session.conf file:

drools.workItemHandlers=SampleWorkItemHandlers.conf

W/ Knowledge API

1) Create the SampleWorkItemHandler.java file:

import java.util.HashMap;
import java.util.Map;

import org.drools.runtime.process.WorkItem;
import org.drools.runtime.process.WorkItemHandler;
import org.drools.runtime.process.WorkItemManager;

public final class SampleWorkItemHandler implements WorkItemHandler {

  public void abortWorkItem(final WorkItem item, final WorkItemManager manager) {
    manager.abortWorkItem(item.getId());
  }

  public void executeWorkItem(final WorkItem item, final WorkItemManager manager) {
    final Map<String, Object> resultMap = new HashMap<String, Object>();

    resultMap.put("name", "value");

    manager.completeWorkItem(item.getId(), resultMap);
  }

}

2) Create the SampleServlet.java file:

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.WorkItemManager;

@SuppressWarnings("serial")
@WebServlet(name = "SampleServlet", value = "/sample")
public final class SampleServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

    final KnowledgeBase kbase = kbuilder.newKnowledgeBase();

    final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

    final WorkItemManager manager = ksession.getWorkItemManager();

    manager.registerWorkItemHandler("Sample", new SampleWorkItemHandler());
  }

}

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