Skip to main content

Understanding jBPM based on Struts background

Posted by edgars on August 28, 2007 at 2:45 AM PDT

When you try teach anything new for someone, IMHO the easiest way is to do that relating with something you already know. So far Struts is a framework that almost everybody touched at least once. So I will use some of Strtus concepts to explain jBPM process execution.

Process Languages

jBPM is not just a Process Machine, locked in just one way to promote interaction between tasks, in fact jBPM is a process-language engine, which you can use JPDL, Seam Page Flow or even BPEL as an execution language. By default, jBPM uses JPDL, however you may use others.

Understanding a Process

A process shall be a number of tasks described in several flows, which depending of some condition or behavior you may get different ways. So I will show you guys a very simples process, where you can see an expenses-report approval process:

processimage.jpg

We have just one TASK, so we may think we have just one ACTION, so is really easy to compare a process execution context with a HttpSession execution context, because both may have variables, a sequence of facts and interactions with Systems or Humans. But you may ask: “What? Are you trying convince that a Process Execution Instance may be as simple as an HttpSession?”. My answer: Exactly!

Creating a Servlet Controler to handle the Process execution

In jBPM a task is represented by a Token, each one has a name, in addition may have an action associated with some event. These events can be for example “node-enter”, “node-leave”, so we must tell via a Servlet forward the execution for a specific Node, which in that case can be our Action (like in Struts).

 public class ServletBPM extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

ProcessDefinition processDefinition;

static JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();

static JbpmContext bpmContext = jbpmConfiguration.getCurrentJbpmContext();

....

public void init() throws ServletException {
super.init();
processDefinition =
ProcessDefinition.parseXmlInputStream
(getServletContext().getResourceAsStream
("/WEB-INF/processes/reembolso/processdefinition.xml"));
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

ProcessInstance instance =
new ProcessInstance(processDefinition);

instance.getContextInstance().setVariable("request", request);
instance.getContextInstance().setVariable("response", response);

Token token = instance.getRootToken();

String acao = request.getParameter("action");

        token.signal(acao);
        }

Look that into doGet method we are creating a new ProcessInstance and putting 2 variables (request and response), and through the value into parameter action, we know the node(task) to execute, in other words, the “Action” to execute.

When you can the method signal is fired, this Token invoke its associated Action class, which may be similar to common Action from Struts. Take a look on the following piece of code:

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.exe.ExecutionContext;

import br.com.redhat.bpm.pojo.Reembolso;

public class EntregaReportAction implements ActionHandler {

HttpServletRequest request;

HttpServletResponse response;


private static final long serialVersionUID = -2649703227240922449L;

public void execute(ExecutionContext ctx) throws Exception {

//getting an HttpResponse from Context

request = (HttpServletRequest) ctx.getVariable("request");

response =  (HttpServletResponse) ctx.getVariable("response");


Reembolso r = new Reembolso();

r.setFuncionario(request.getParameter("funcionario"));

r.setPeriodo(request.getParameter("data"));

r.setValor(new Double(request.getParameter("valor")));


                       JPAResolver.getResolver(Reembolso.class).save(r);

RequestDispatcher rd = request.getRequestDispatcher("aguardeProcessamento.jsp");

rd.forward(request, response);

}

So, as you can see this ActionHandler gets the request and response variables from the process context and execute some business logic and from this ActionHandler we forward the next page, quite similar we can do with Struts's Action, or much easier based on Seam Framework.

Keep in mind that all context variables and process instance you may store into a Database for any other further information or management.

Conclusion

BPM is not a so complicated discipline inside SOA world, however I've talked with many people in Brazil with some difficulties to understand some BPM Concepts. I hope this post help no only my Brazilian friend but anybody with similar doubts about that.

Related Topics >>