jBPM7

jBPM7 – Hello World

Let’s get our hands dirty in creating a simple Hello World project.

1. Tech Stack

We should have the following software to create a jBPM Project.

  1. Java 1.7 or higher version
  2. Eclipse IDE for Enterprise Java Developers
  3. jBPM7 plugin should install with eclipse IDE
2. Create a jBPM Project

We are going to create a simple maven jBPM project. Let’s follow the below steps

Download jBPM hello world project
Download counts: 4481
  1. After downloading, unzip the project.
  2. Go to Eclipse IDE -> File -> Import -> Maven -> Existing Maven project -> Select the project.

STEP 1: To create a new jBPM project in Eclipse, we’ll go to File -> New -> Other -> jBPM Project . Then Click next.

STEP 2: Choose the ‘create a project and populate it with some example‘ option and then click next.

STEP 3: Select ‘maven‘ and ‘Add a simple hello world process‘ options. Then Providing the name of our project we can hit finish. Eclipse will do all the hard work for us and will download the required Maven dependencies to create a jBPM project for us .

STEP 4: Successfully, we created our first sample jBPM project. Let’s look at the project folder structure and also see the explanation of project’s files below .

A. It is maven project. pom.xml has jBPM7 dependencies.

<properties>
    <runtime.version>7.0.0.Final</runtime.version>
</properties>

<dependencies>
     <dependency>
       <groupId>org.kie</groupId>
       <artifactId>kie-api</artifactId>
       <version>${runtime.version}</version>
    </dependency>

    <dependency>
      <groupId>org.jbpm</groupId>
      <artifactId>jbpm-test</artifactId>
      <version>${runtime.version}</version>
    </dependency>
<dependencies>

B. kmodule.xml file defining in a declarative way the KieBases and KieSessions that can be created from it. This file has to be placed in the resources/META-INF folder of the Maven project while all the other Kie artifacts, such as BPMN, DRL or a Excel files, must be stored in the resource packages.

<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
     <kbase name="kbase" packages="com.hi.techpoints"/>
</kmodule>

C. The file extension is .bpmn and it opens in the BPMN2 diagram editor. The *.bpmn files will be executed by jBPM runtime environment.

The right panel of the designer lists the nodes we selected earlier while setting up the Eclipse plugin. We’re going to use these nodes to create our process model. The middle panel is the workspace, where we’ll create the process models. The left side is the properties tab, where we can set the properties of a process or node.

In this HelloWorld model, we’ll be using the:

  • Start Event – required to start the process instance
  • Script Task – enables Java snippets
  • End Event – required to end the process instance

D. ProcessMain.java is a standalone program and it has BPMN file execution code. All the BPMN files are loaded in kiebase as processes. We need to pass the respective process ids to the jBPM engine in order to execute them.

3. Run a jBPM project
public class ProcessMain { 
    private static final Logger log = LoggerFactory.getLogger(ProcessMain.class);
    private static final boolean usePersistence = true;
    
    public static final void main(String[] args) throws Exception {
        //1. Load *.bpmn file into the knowledge base
        KieBase kbase = readKnowledgeBase();
        
        //2. Create a jBPM RuntimeManager
        RuntimeManager runtimeManager = createRuntimeEnvironment(kbase);
        
        //3. Get RuntimeEngine out of manager
        RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(EmptyContext.get());
         
        //4. Create KieSession from runtimeEngine - already initialized *.bpmn file on the environment 
        KieSession ksession = runtimeEngine.getKieSession();
        
        //5. Start a new process instance here
        ksession.startProcess("com.hi.techpoints.helloworld");
        log.info("Process started ...");
        
        //6. At Last dispose the runtime engine
        runtimeManager.disposeRuntimeEngine(runtimeEngine);
        runtimeManager.close();
    }

ProcessMain.java is a standalone program, just run the class file. After running the program, it will print ‘Hello world!’.

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs