Tutorial: a BPMN 2.0 Hello World With Activiti 5.0.alpha4 in 5 steps

With the 5.0.alpha4 release just out of the door, I thought it was time to demonstrate how easy it is to get started with the latest version of Activiti. Note That I’m not using the demo setup here, if you want that, check out my previous article.

In this tutorial, I’ll show you how we’ll run a very simple “Hello-World”-ish BPMN 2.0 business process on Activiti. From there on, you should be able to explore all the capabilities of Activiti, only limited by your imagination. In a later article, I’ll show you how to run this hello world app on MySQL.

Prerequisites

To follow this tutorial you’ll need

  • Eclipse (or any other Java IDE)
  • Ant
  • 5 minutes of your time

Step 0: unzip the distribution

If you haven’t got it already, download the alpha4 release from the Activit website. Unzip this file.

Step 1: create a DB schema

Boot up an in-memory H2 database, that is shipped with the Activiti distribution:

cd activiti-5.0.alpha4/setup/

ant h2.install h2.start

You can now create the Activiti schema:

ant db.create

The result should look like this:

If you want, you can now inspect the database schema:

cd activiti-5.0.alpha4/apps/h2

java -cp h2*.jar org.h2.tools.Server -web

(I’m sorry for that last line, we’ll create an ant target to start up the web console in the next release)

Open your browser, and go to http://localhost:8082. Enter jdbc:h2:tcp://localhost/activiti in the field ‘JDBC URL’, leave all the rest as it is and click ‘connect’. You should see the Activiti schema in all its glory:

Step 2: Setup project

Open up Eclipse, and create a new Java project.

Add all the libraries from activiti-5.0.alpha4/lib/ to your classpath. You don’t need them all actually, but for speed we’ll just take the easy path here.

Step 3: Generate a configuration

The Activiti engine needs a small configuration file to run in a standalone way (ie not within a dependency container such as Spring for example). This config file can be generated from the terminal.

cd activiti-5.0.alpha4/setup

ant cfg.create

(The config file generator takes as input the build.properties and the build.${your_databae}.properties files. Tweaking these is content for a later blogpost!)

A config file and a jar with the same file is generated for you in the folder activiti-5.0.alpha4/setup/build/activiti-cfg and activiti-5.0.alpha4/setup/build/activiti-cfg.jar. Add this jar file to your project in Eclipse.

Step 4: Create the Hello World BPMN 2.0 process

The process we’ll model is very simple: a start and stop event, with a Hello-World step in between.

Granted, it can hardly be called a ‘business process’, but all things start small!

The XML representation of this process looks as follows. Add this XML file to your project and call it ‘hello_world.bpmn20.xml:


<?xml version="1.0" encoding="UTF-8"?>

<definitions id="definitions"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
targetNamespace="http://www.activiti.org/bpmn2.0">

  <process id="helloWorld">

    <startEvent id="start" />
    <sequenceFlow id="flow1" sourceRef="start" targetRef="script" />
    <scriptTask id="script" name="HelloWorld" scriptFormat="groovy">
      <script>
        System.out.println("Hello world")
      </script>
    </scriptTask>
    <sequenceFlow id="flow2" sourceRef="script" targetRef="theEnd" />
    <endEvent id="theEnd" />

</process>

</definitions>

You can see here that our ‘hello world’ step is basically the execution of a Groovy script saying hello to the world. Note that the id of the process is ‘helloWorld’.

Step 5: run it

All that is left now, is actually running the process. Create a new class in your project and add a main method.

First, boot up the process engine:

ProcessEngine processEngine = new DbProcessEngineBuilder()
    .configureFromPropertiesResource("activiti.properties")
    .buildProcessEngine();
ProcessService processService = processEngine.getProcessService();

You can see that we’re constructing the process engine using our generated configuration file. The process engine gives access to different services (such as taskService, processService, etc.), as you can see on the last line.

To run the process, it first need to be ‘deployed’ to the engine. Deploying means that the process is parsed and stored in the Activiti database:

processService.createDeployment()
    .addClasspathResource("hello-world.bpmn20.xml")
    .deploy();

And now we can execute the process, using the id of the process as ‘key’:

processService.startProcessInstanceByKey("helloWorld");

Which gives as output the following:

Next stop: uber-BPM-powers

And that’s all thats needed to run your first BPMN 2.0 Hello World process!

Of course, this is only the start of a very interesting journey. Best place to get familiar with the capabilities of Activiti and BPMN 2.0, is to check out our userguide.

Stay tuned for articles that will build further on this example (eg. using Maven, MySQL and enhancing the process) !

15 Comments

  1. Falko Menge August 4, 2010

    I guess, you meant ‘Eclipse (or any other Java IDE)’?

  2. Joram Barrez August 4, 2010

    Yup, indeed Falko. Thanks!

  3. Aditya August 5, 2010

    Hi,

    I am getting below error in step 1 while creating the database in windows XP

    Buildfile: build.xml
    [echo] Activiti home = ..
    [echo] Activiti version = 5.0.alpha4

    h2.install:

    internal.classpath.libs:

    internal.taskdef.launch:

    h2.start:

    [launch] waiting for launch completion msg ‘TCP server running on’…
    [launch] java.lang.NoClassDefFoundError: org/h2/tools/Server
    [launch] Caused by: java.lang.ClassNotFoundException: org.h2.tools.Server
    [launch] at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    [launch] at java.security.AccessController.doPrivileged(Native Method)
    [launch] at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    [launch] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    [launch] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    [launch] at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    [launch] at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    [launch] Could not find the main class: org.h2.tools.Server. Program will exit.
    [launch] Exception in thread “main”
    [launch] launched process completed

    BUILD SUCCESSFUL
    Total time: 0 seconds

  4. Joram Barrez August 5, 2010

    Hi Aditya,

    I just tried to run the script on a vanilla Windows XP machine here, and all works fine.

    However, it looks like your h2.install is not creating or copying any file (I get 2 lines of output). Did you change any folder names or something alike?

  5. Michael August 11, 2010

    Hi,

    I am some steps ahead and installed the complete demo.setup and tried to test the financial report demo with the alpha4 🙂

    Kermit is creating a new financial process -> fozzie is claiming it ->fozzie is completing it -> kermit wants to see the results

    When I try to open the completed task list with kermits account there is only a “Data Error.” showen as list entry. This is in the unassigned task list and the management task list.

    I am using a MacBook and don’t know how to solve this problem.

    Cheers

    Michael

    activiti log:
    [org.activiti.impl.interceptor.DefaultCommandExecutor]
    23:59:00,268 FIN | ooo Connection Opened
    23:59:00,269 FIN | ==> Executing: select g.* from ACT_ID_GROUP g, ACT_ID_MEMBERSHIP membership where g.ID_ = membership.GROUP_ID_ and membership.USER_ID_ = ? and g.TYPE_ = ?
    23:59:00,269 FIN | ==> Parameters: kermit(String), security-role(String)
    23:59:00,271 FIN | <== Columns: ID_, REV_, NAME_, TYPE_
    23:59:00,271 FIN | <== Row: admin, 1, System administrator, security-role
    23:59:00,271 FIN | Executing: select distinct(T.*) from ACT_TASK T inner join ACT_TASKINVOLVEMENT I on I.TASK_ID_ = T.ID_ WHERE T.ASSIGNEE_ is null and I.TYPE_ = ‘candidate’ and ( I.GROUP_ID_ IN ( ? ) ) order by T.ID_
    23:59:00,419 FIN | ==> Parameters: management(String)
    23:59:00,420 FIN | <== Columns: ID_, REV_, NAME_, DESCRIPTION_, ASSIGNEE_, PRIORITY_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, CREATE_TIME_, START_DEADLINE_, COMPLETION_DEADLINE_, SKIPPABLE_
    23:59:00,420 FIN | Executing: select count(distinct T.ID_) from ACT_TASK T inner join ACT_TASKINVOLVEMENT I on I.TASK_ID_ = T.ID_ WHERE T.ASSIGNEE_ is null and I.TYPE_ = ‘candidate’ and ( I.GROUP_ID_ IN ( ? ) )
    23:59:00,550 FIN | ==> Parameters: management(String)
    23:59:00,550 FIN | <== Columns: COUNT(DISTINCT T.ID_)
    23:59:00,551 FIN | <== Row: 1
    23:59:00,551 FIN | xxx Connection Closed
    23:59:00,629 FIN | === command org.activiti.impl.query.AbstractListQuery$3@6810a098 finished

  6. Joram Barrez August 18, 2010

    Michael, nice thast you are trying out the demo.setup!

    However, I’ve never seen the ‘data error’ you mention. Do you happen to have a stacktrace of the Tomcat logs ?

  7. ahmet kara October 12, 2010

    Hi,
    I tried maven version of this post, but I got error, “NoClassDefFoundError” like below, what is the reason, I put all dependencies as you wrote, thanks for your interest.

    Exception in thread “main” java.lang.NoClassDefFoundError: javax/el/ELContext
    at org.activiti.impl.cfg.ProcessEngineFactory.createDefaultExpressionManager(ProcessEngineFactory.java:267)
    at org.activiti.impl.cfg.ProcessEngineFactory.init(ProcessEngineFactory.java:107)
    at org.activiti.impl.cfg.ProcessEngineFactory.createProcessEngine(ProcessEngineFactory.java:169)
    at org.activiti.DbProcessEngineBuilder.buildProcessEngine(DbProcessEngineBuilder.java:214)
    at com.yaser.TestMain.main(TestMain.java:18)
    Caused by: java.lang.ClassNotFoundException: javax.el.ELContext
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    … 5 more

  8. Joram Barrez October 13, 2010

    Things have changed quite a bit since alpha4, so not all info may be accurate now.

    I’ll update the post in the near future !

  9. Winko November 20, 2010

    Joram,

    I love your “How to” but I get stuck with step 3 (step 2 is also a bit short…) would you please be so kind, expanding this “How to”?

    Regards,

    Winko

  10. yi February 23, 2011

    Thanks a lot, the tutorial is helpful
    How can I use a java task instead of a script task to print hello world?

  11. Joram February 23, 2011

    Just replace the scriptTask by a <serviceTask activiti:class="org.activiti.MyClass", where MyClass implements the JavaDelegate interface

  12. sandeep August 27, 2012

    Hi Every One
    I have followed the instructions to connect mysql database

    But,I am unable to connect ,getting error in login page itsef .

    My Configuration is like this

    db=mysql
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/activiti
    jdbc.username=root
    jdbc.password=chiguru

    Please can any elobrate the connection steps to mysql database.

    For my project i need to integrate workflow so

    Thanks in Advance.

  13. Balakrishna May 16, 2013

    Hi,

    I am getting following error while executing the program. Kindly assist me.

    May 16, 2013 10:23:36 PM org.activiti.impl.ProcessEngineImpl
    INFO: ProcessEngine default created
    Exception in thread “main” org.activiti.ActivitiException: couldn’t read input stream hello_world.bpmn20.xml
    at org.activiti.impl.util.IoUtil.readInputStream(IoUtil.java:36)
    at org.activiti.impl.repository.DeploymentBuilderImpl.addInputStream(DeploymentBuilderImpl.java:43)
    at org.activiti.impl.repository.DeploymentBuilderImpl.addClasspathResource(DeploymentBuilderImpl.java:52)
    at com.activiti.HelloWorld.main(HelloWorld.java:19)
    Caused by: java.lang.NullPointerException
    at org.activiti.impl.util.IoUtil.readInputStream(IoUtil.java:30)
    … 3 more

    Thanks!

  14. Ayusman October 2, 2013

    Hi Joram,
    I was trying to follow this article using Activiti 5.13 (I know this article was based on 5.0.alpha4)
    I do see the database scripts under \database\create.

    However the setup folder is missing.

    Is the decision to remove the scripts and standalone setup a conscious decision?

    I find having standalone setup for BPM helps developer understanding and hence more adaptation.

    Can I make the current activiti release standalone (like JBPM 4.4 used to be)?

    Thanks

  15. Joram Barrez October 4, 2013

    Yes, we did remove the automagic install a while ago, as there were too many things that could go wrong at different operating systems, etc.
    At the moment, simply drop the activiti war in a Tomcat (see http://activiti.org/userguide/index.html#demo.setup.one.minute.version)

Leave a Reply

Your email address will not be published. Required fields are marked *