Tag Archives: jBPM

How to Customize Work Item Handlers in jBPM 6

W/ Configuration File

1) Create the SampleWorkItemHandler.java file:

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

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.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() # incompatible with CDI
]

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

drools.workItemHandlers=SampleWorkItemHandlers.conf

W/ KIE API

1) Create the SampleWorkItemHandler.java file:

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

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.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 java.util.HashMap;
import java.util.Map;

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

import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.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 RuntimeManager runtimeManager = ... // or inject it

    final RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(...);

    final KieSession sess = engine.getKieSession();

    final WorkItemManager workItemManager = sess.getWorkItemManager();

    workItemManager.registerWorkItemHandler("Sample", new SampleWorkItemHandler()); // or inject it

    ...
  }

}

W/ Work Item Handler Producer

1) Create the SampleWorkItemHandler.java file:

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

import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemHandler;
import org.kie.api.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 SampleWorkItemHandlerProducer.java file:

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

import org.jbpm.runtime.manager.api.WorkItemHandlerProducer;
import org.kie.api.runtime.process.WorkItemHandler;

public final class SampleWorkItemHandlerProducer implements WorkItemHandlerProducer {

  @Override
  public Map<String, WorkItemHandler> getWorkItemHandlers(final String identifier, final Map<String, Object> params) {
    final Map<String, WorkItemHandler> workItemHandlers = new HashMap<String, WorkItemHandler>();

    workItemHandlers.put("Sample", new SampleWorkItemHandler()); // inject if using CDI

    return workItemHandlers;
  }

}

3) Create the SampleServlet.java file:

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

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

import org.jbpm.runtime.manager.impl.cdi.InjectableRegisterableItemsFactory;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.manager.RuntimeEnvironment;
import org.kie.api.runtime.manager.RuntimeEnvironmentBuilder;
import org.kie.api.runtime.manager.RuntimeManager;

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

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final RuntimeEnvironment env = RuntimeEnvironmentBuilder.Factory.get()...registerableItemsFactory(new InjectableRegisterableItemsFactory()).get(); // or inject it

    final RuntimeManager runtimeManager = ... // or inject it

    final RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(...);

    final KieSession sess = engine.getKieSession();

    ...
  }

}
Advertisements

How to Create a Process Event Listener for jBPM

import org.drools.event.process.ProcessCompletedEvent;
import org.drools.event.process.ProcessEventListener;
import org.drools.event.process.ProcessNodeLeftEvent;
import org.drools.event.process.ProcessNodeTriggeredEvent;
import org.drools.event.process.ProcessStartedEvent;
import org.drools.event.process.ProcessVariableChangedEvent;

public final class SampleProcessEventListener implements ProcessEventListener {

  @Override
  public void afterNodeLeft(final ProcessNodeLeftEvent event) {
  }

  @Override
  public void afterNodeTriggered(final ProcessNodeTriggeredEvent event) {
  }

  @Override
  public void afterProcessCompleted(final ProcessCompletedEvent event) {
  }

  @Override
  public void afterProcessStarted(final ProcessStartedEvent event) {
  }

  @Override
  public void afterVariableChanged(final ProcessVariableChangedEvent event) {
  }

  @Override
  public void beforeNodeLeft(final ProcessNodeLeftEvent event) {
  }

  @Override
  public void beforeNodeTriggered(final ProcessNodeTriggeredEvent event) {
  }

  @Override
  public void beforeProcessCompleted(final ProcessCompletedEvent event) {
  }

  @Override
  public void beforeProcessStarted(final ProcessStartedEvent event) {
  }

  @Override
  public void beforeVariableChanged(final ProcessVariableChangedEvent event) {
  }
  
}

How to Suspend a Task with the jBPM Human Task API

import org.drools.SystemEventListenerFactory;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.TaskClientHandler.TaskOperationResponseHandler;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.responsehandlers.BlockingTaskOperationResponseHandler;

public final class JbpmSample {

  public static void main(final String[] args) throws Exception {
    final TaskClient client = new TaskClient(new MinaTaskClientConnector("SampleClient", new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

    if (client.connect("localhost", 9123)) {
      final TaskOperationResponseHandler handler = new BlockingTaskOperationResponseHandler();

      client.suspend(1, "userId", handler);

      client.disconnect();
    }

  }

}

How to Stop a Task with the jBPM Human Task API

import org.drools.SystemEventListenerFactory;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.TaskClientHandler.TaskOperationResponseHandler;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.responsehandlers.BlockingTaskOperationResponseHandler;

public final class JbpmSample {

  public static void main(final String[] args) throws Exception {
    final TaskClient client = new TaskClient(new MinaTaskClientConnector("SampleClient", new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

    if (client.connect("localhost", 9123)) {
      final TaskOperationResponseHandler handler = new BlockingTaskOperationResponseHandler();

      client.stop(1, "userId", handler);

      client.disconnect();
    }

  }

}

How to Skip a Task with the jBPM Human Task API

import org.drools.SystemEventListenerFactory;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.TaskClientHandler.TaskOperationResponseHandler;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.responsehandlers.BlockingTaskOperationResponseHandler;

public final class JbpmSample {

  public static void main(final String[] args) throws Exception {
    final TaskClient client = new TaskClient(new MinaTaskClientConnector("SampleClient", new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

    if (client.connect("localhost", 9123)) {
      final TaskOperationResponseHandler handler = new BlockingTaskOperationResponseHandler();

      client.skip(1, "userId", handler);

      client.disconnect();
    }

  }

}

How to Resume a Task with the jBPM Human Task API

import org.drools.SystemEventListenerFactory;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.TaskClientHandler.TaskOperationResponseHandler;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.responsehandlers.BlockingTaskOperationResponseHandler;

public final class JbpmSample {

  public static void main(final String[] args) throws Exception {
    final TaskClient client = new TaskClient(new MinaTaskClientConnector("SampleClient", new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

    if (client.connect("localhost", 9123)) {
      final TaskOperationResponseHandler handler = new BlockingTaskOperationResponseHandler();

      client.resume(1, "userId", handler);

      client.disconnect();
    }

  }

}

How to Release a Task with the jBPM Human Task API

import org.drools.SystemEventListenerFactory;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.TaskClientHandler.TaskOperationResponseHandler;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.responsehandlers.BlockingTaskOperationResponseHandler;

public final class JbpmSample {

  public static void main(final String[] args) throws Exception {
    final TaskClient client = new TaskClient(new MinaTaskClientConnector("SampleClient", new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));

    if (client.connect("localhost", 9123)) {
      final TaskOperationResponseHandler handler = new BlockingTaskOperationResponseHandler();

      client.release(1, "userId", handler);

      client.disconnect();
    }

  }

}