Tutorial: Running the BPMN 2.0 Hello World example on MySQL (with Maven)

Last week, I showed you how to get a ‘BPMN 2.0 Hello World’ up and running with the latest release of Activiti. Today, we’ll take the very same example a step further and make it run on MySQL and mavenize it all.

Interested in just the code? Just scroll down to the bottom of the post or click here.

Prerequisites

The required stuff remains the same: Eclipse (or any other IDE) with a Maven plugin (I’m using m2eclipse), Java and Ant.

Project creation

We’re just creating a vanilla Maven project. I’ve used the Eclipse wizard (New -> Maven Project), but some commandline-magic should also do the trick.The end result should look like this in Eclipse:

Dependencies

To run our Hello World example on MySQL, we’ll need two dependencies:

  • The Activiti engine jar, obviously
  • The MySQL database driver

Accordingly, the pom.xml looks like this:


<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>be.jorambarrez.tutorial</groupId>
   <artifactId>helloworld</artifactId>
   <version>1.0</version>

  <dependencies>
    <dependency>
       <groupId>org.activiti</groupId>
       <artifactId>activiti-engine</artifactId>
       <version>5.0.alpha4</version>
    </dependency>
   <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.13</version>
    </dependency>
  </dependencies>

  <repositories>
    <repository>
      <id>activiti.repo</id>
      <url>http://maven.alfresco.com/nexus/content/repositories/activiti/</url>
    </repository>
  </repositories>

</project>

The process

The Hello world process is the same as the one we’ve used in the previous article.

The corresponding XML also remains the same. Call it hello-world.bpmn20.xml and add it to src/main/resources.


<?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>

Running the process

To run the process, we’ll be using the same main method as in the original article: bootstrap the engine, deploy the process and run it:

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

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

 // Run
 processService.startProcessInstanceByKey("helloWorld");

The only thing that’s left to do, is add an activiti.properties file to src/main/resources that now points to a MySQL database. Alternatively, you could also use the cfg.create Ant target which is in the setup script of the Activiti distribution (see previous article).

database=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.1.102:3306/activiti
jdbc.username=activiti
jdbc.password=activiti
db.schema.strategy=create

Note that we’re setting the schema creation strategy to ‘create‘, since want to create the schema the first time we run our code. If you want to rerun your code afterwards, be sure to change it it ‘check-version‘.

Also, don’t forget to create the schema ‘activiti’ in MySQL and to add the user ‘activiti’ with enough permissions in MySQL.

Now just run it. The output should be non-surprising:

With that difference that we now actually are running on the MySQL database! See the proof on the following picture:

Download the source of this tutorial

The source for this tutorial can be downloaded as a ZIP file here. Just import it into Eclipse, point it your MySQL config and enjoy the ride!

10 Comments

  1. MB January 18, 2011

    Any plans to publish the Activiti artifacts to the Maven central repository?

  2. Joram Barrez January 18, 2011

    They are currently published against the Alfresco maven repo. I wouldnt know how to get those to the central repo …

  3. sandeep August 27, 2012

    Hi,

    I have followed the instructions to connect mysql database

    I am getting follwing error

    J���
    5.5.15�°���Ep&ia,TB�ÿ÷�€����������,WPmS7X7JN)#�mysql_native_password�!��ÿ„#08S01Got packets out of order

    can anyone please help me out,
    i neeed to integraten this workflow to coming project

    Thanks in advance

  4. Suresh June 5, 2013

    Hai.. I am importing this project and getting the errors in JUnit test.

    The Errors are..
    java.lang.NoClassDefFoundError: org/springframework/core/io/Resource
    at org.activiti.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:145)
    at org.activiti.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:141)
    at org.activiti.engine.impl.test.TestHelper.getProcessEngine(TestHelper.java:223)
    at org.activiti.engine.test.ActivitiRule.initializeProcessEngine(ActivitiRule.java:104)
    at org.activiti.engine.test.ActivitiRule.starting(ActivitiRule.java:96)
    at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:46)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: java.lang.ClassNotFoundException: org.springframework.core.io.Resource
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    … 20 more

    Plz anyone help plz….

  5. Joram Barrez June 10, 2013

    How is that possible? This posts is about a really old version of Activiti where no Spring was used in that time….

    did you upgrade the engine of Activiti in the pom.xml? if so, this code wont work anymore of course (the activiti.properties approach is not used anymore for a long time)

  6. Ram August 29, 2013

    Import Spring core jar in your project. That should resolve the issue.

  7. Joram Barrez September 2, 2013

    @Ram: you’re right of course. I forgot about the config file being a spring file.

  8. ram November 29, 2013

    getting the below error.

    Exception in thread “main” org.activiti.ActivitiException: Error while building ibatis SqlSessionFactory:
    ### Error building SqlSession.
    ### The error may exist in SQL Mapper Configuration
    ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: java.net.ConnectException: Connection refused: connect
    at org.activiti.impl.persistence.IbatisPersistenceSessionFactory.(IbatisPersistenceSessionFactory.java:184)
    at org.activiti.DbProcessEngineBuilder.buildProcessEngine(DbProcessEngineBuilder.java:262)
    at Main.main(Main.java:12)
    Caused by: org.apache.ibatis.exceptions.IbatisException:
    ### Error building SqlSession.

  9. murthi November 29, 2013

    getting the below exception while executing this program
    Exception in thread “main” org.activiti.ActivitiException: Error while building ibatis SqlSessionFactory:
    ### Error building SqlSession.
    ### The error may exist in SQL Mapper Configuration
    ### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: java.net.ConnectException: Connection refused: connect
    at org.activiti.impl.persistence.IbatisPersistenceSessionFactory.(IbatisPersistenceSessionFactory.java:184)
    at org.activiti.DbProcessEngineBuilder.buildProcessEngine(DbProcessEngineBuilder.java:262)
    at Main.main(Main.java:12)
    Caused by: org.apache.ibatis.exceptions.IbatisException:
    ### Error building SqlSession.

  10. Joram Barrez November 29, 2013

    Please post questions to the Activiti forum: http://forums.activiti.org/

    Also, your exception seems to indicate that the jdbc connection properties you use to connect to your db are wrong.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>