Tuesday, February 11, 2014

Starting with Acitiviti (BPM Engine)

Background

I am busy working on a project which has a very small workflow or business process component to it. One of the requirements was that we had to use Activiti to manage the business processes. My first port of call was the "Activiti in Action" book. When I felt that had enough ammunition to take on Activiti, I gave it a go.

There were a few bumps along the road especially because I was trying to incorporate Acitiviti into an existing Spring Maven application. In this blog I am going to show you how to get your Activiti code running in a Maven Spring application.

Activiti Architecture

The Basic Activiti architecture is pretty simple. There is a database which is used to maintain and persist state. You can manipulate the state of objects within the database by using the Activiti engine or API. This can be the Activiti libraries or dependencies in your application or you can also interrogate the database using the Rest services.

The Activiti Explorer is a web application which uses the API to view and change objects housed in the database. The Activiti Explorer also has a tool (Activiti Modeler) which can be used to model BPMN processes. You can model your process using this tool and then export the configuration into an xml file. You can then use this xml file in your application without having to model the process manually. 

Downloads

You will need to download Tomcat and the latest Activiti Release. The only 2 artefacts of concern to you for this tutorial can be located in the wars directory:
  • activiti-rest.war
  • activiti-explorer.war 
You can deploy these 2 war files on your Tomcat server. To be really specific, you only need the activiti-explorer. You might need to add the H2 database Driver to your Tomcat server.

Do I need a database?

Activiti does use a database but you do not need one explicitly. Activiti is packaged with an embedded H2 database. This means that when you run the application, it will access this embedded database automatically. So even though you are not specifying a database, there is one being used.

Add these dependencies to your Maven POM file

     <!-- Activiti Stuff-->  
     <dependency>  
       <groupId>org.activiti</groupId>  
       <artifactId>activiti-engine</artifactId>  
       <version>5.14</version>  
     </dependency>  
     <dependency>  
       <groupId>org.activiti</groupId>  
       <artifactId>activiti-spring</artifactId>  
       <version>5.14</version>  
     </dependency>  
 <!-- Spring Stuff-->  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-context</artifactId>  
       <version>${spring.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-jdbc</artifactId>  
       <version>${spring.version}</version>  
     </dependency>  
     <dependency>  
       <groupId>org.springframework</groupId>  
       <artifactId>spring-tx</artifactId>  
       <version>${spring.version}</version>  
     </dependency>  
 <!-- H2 Driver-->  
     <dependency>  
       <groupId>com.h2database</groupId>  
       <artifactId>h2</artifactId>  
       <version>1.3.175</version>  
     </dependency>  

Add this repository to your Artefact repository or your Maven POM file

     <repository>  
       <id>Alfresco Maven Repository</id>  
       <url>https://maven.alfresco.com/nexus/content/groups/public/</url>  
     </repository>  

Design a Simple Activiti Workflow process using Activiti Modeler

This is the sample process that I designed. It is simple and self explanatory.
Here is the exported xml file contents (SampleActivitiProcess.bpmn20.xml):
 <?xml version="1.0" encoding="UTF-8"?>  
 <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">  
  <process id="SampleActivitiProcess" name="SampleActiviti" isExecutable="true">  
   <startEvent id="start" name="start"></startEvent>  
   <userTask id="SomeUserTask" name="SomeUserTask"></userTask>  
   <endEvent id="end" name="end"></endEvent>  
   <sequenceFlow id="flowFromStart" name="flowFromStart" sourceRef="start" targetRef="SomeUserTask"></sequenceFlow>  
   <sequenceFlow id="flowToEnd" name="flowToEnd" sourceRef="SomeUserTask" targetRef="end"></sequenceFlow>  
  </process>  
  <bpmndi:BPMNDiagram id="BPMNDiagram_SampleActivitiProcess">  
   <bpmndi:BPMNPlane bpmnElement="SampleActivitiProcess" id="BPMNPlane_SampleActivitiProcess">  
    <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">  
     <omgdc:Bounds height="30.0" width="30.0" x="165.0" y="210.0"></omgdc:Bounds>  
    </bpmndi:BPMNShape>  
    <bpmndi:BPMNShape bpmnElement="SomeUserTask" id="BPMNShape_SomeUserTask">  
     <omgdc:Bounds height="80.0" width="100.0" x="301.0" y="185.0"></omgdc:Bounds>  
    </bpmndi:BPMNShape>  
    <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">  
     <omgdc:Bounds height="28.0" width="28.0" x="495.0" y="211.0"></omgdc:Bounds>  
    </bpmndi:BPMNShape>  
    <bpmndi:BPMNEdge bpmnElement="flowFromStart" id="BPMNEdge_flowFromStart">  
     <omgdi:waypoint x="195.0" y="225.0"></omgdi:waypoint>  
     <omgdi:waypoint x="301.0" y="225.0"></omgdi:waypoint>  
    </bpmndi:BPMNEdge>  
    <bpmndi:BPMNEdge bpmnElement="flowToEnd" id="BPMNEdge_flowToEnd">  
     <omgdi:waypoint x="401.0" y="225.0"></omgdi:waypoint>  
     <omgdi:waypoint x="495.0" y="225.0"></omgdi:waypoint>  
    </bpmndi:BPMNEdge>  
   </bpmndi:BPMNPlane>  
  </bpmndi:BPMNDiagram>  
 </definitions>  
I would advise you to always change the ID's of all of the elements and also provide them with descriptive names. This helps you out tremendously when developing against your BPMN process.

Let us start Coding

The general process of running an Activiti process is as follows:

Design BPMN Process

You can use Activiti Modeler from Activiti Explorer, the Eclipse Activiti Plugin or manually build the xml representing your business process flow.

Deploy Process

You can use Activiti Explorer to deploy or programatically deploy your business process flow. Doing it programatically, you have 2 options:

  • The Spring way
  • The Standard java way

Run Process Instance

You reference a process which is deployed and start a new process instance.

Create Activiti process engine

The are 3 ways to achieve this:

In Memory

 ProcessEngine processEngine = ProcessEngineConfiguration  
         .createStandaloneInMemProcessEngineConfiguration()  
         .buildProcessEngine();  

This is the easiest and fastest way to get up and running. It references the embedded database and uses the default configuration.

File based

 ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();  
The database connection settings are referenced from the activiti.cfg.xml

Here is a sample file refencing the embedded default H2 database:
 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  http://www.springframework.org/schema/beans/spring-beans.xsd">  
   <!--<bean id="processEngineConfiguration"  
      class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">-->  
   <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">  
     <property name="jdbcUrl"  
          value="jdbc:h2:/tmp/activiti;AUTO_SERVER=TRUE" />  
     <property name="jdbcDriver" value="org.h2.Driver" />  
     <property name="jdbcUsername" value="sa" />  
     <property name="jdbcPassword" value="" />  
     <!-- Database configurations -->  
     <property name="databaseSchemaUpdate" value="true" />  
     <!-- job executor configurations -->  
     <property name="jobExecutorActivate" value="false" />  
     <!-- mail server configurations -->  
     <property name="mailServerPort" value="5025" />  
     <property name="history" value="full" />  
   </bean>  
 </beans>  

Basic Spring Style

     ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Service-core-application-context.xml");  
     ProcessEngine processEngine = (ProcessEngine) ctx.getBean("processEngine");  

Here is the Spring configuration required:
   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
     <property name="driverClass" value="org.h2.Driver" />  
     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
     <property name="username" value="sa" />  
     <property name="password" value="" />  
   </bean>  
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
     <property name="dataSource" ref="dataSource" />  
   </bean>  
   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
     <property name="databaseType" value="mysql" />  
     <property name="dataSource" ref="dataSource" />  
     <property name="transactionManager" ref="transactionManager" />  
     <property name="databaseSchemaUpdate" value="true" />  
     <property name="deploymentResources"  
          value="classpath*:SampleActivitiProcess.bpmn20.xml" />  
     <property name="jobExecutorActivate" value="false" />  
   </bean>  
   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
   </bean>  
   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
You can see in this way, Spring handles deploying the BPMN Process for you.

It is important to know that BPMN Process xml files can only be of 2 types and have to end in the following extensions for the API to process / deploy the files:

  1. .bpmn20.xml (Activiti Modeler)
  2. .bpmn (Eclipse Activit Plugin)
The 2 services that we require is the RuntimeService and the RepositoryService:
 RepositoryService repositoryService = processEngine.getRepositoryService();  
 RuntimeService runtimeService = processEngine.getRuntimeService();  

Remember that if you are using the Spring way, the respective beans already exist.
We use the RepositoryService to deploy the process, if it has not already been done (the spring way or via the Activiti Explorer console):
 repositoryService.createDeployment()  
         .addClasspathResource("SampleActivitiProcess.bpmn20.xml")  
         .deploy();  

We then start a process using the RuntimeService:
 String procId = runtimeService.startProcessInstanceByKey("SampleActivitiProcess").getId();  

A process is considered to be active if it is not in an end state or not suspended. You can use the Task Service to manipulate active tasks:
 TaskService taskService = processEngine.getTaskService();  
 List<Task> tasks = taskService.createTaskQuery().active().list();  
Remember that the TaskService bean already exists if you are using the Spring way.

You can use the TaskService to query, claim and complete tasks:
 taskService.claim(task.getId(), "sampleUser");  
 taskService.complete(task.getId());  

Once the Process in a Final state, you can't use the TaskService for these processes but need to use the HistoryService:
 HistoryService historyService = processEngine.getHistoryService();  
     HistoricProcessInstance historicProcessInstance =  
         historyService.createHistoricProcessInstanceQuery().processInstanceId(procId).singleResult();  
     System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());  
Remember that the HistoryService bean already exists if you are using the Spring way.

Conclusion

This should be enough information to get you up and running with Activiti. Remember to start off small and build on incrementally. This way you can understand the process in much greater detail.
Activiti is a great tool and what is even more amazing is that this piece of software is open source and free to use.

As always, I would love to hear your comments or questions. You can also follow me on Twitter or Google plus. I have added shortcuts to my profile in the About me link.


Tuesday, January 21, 2014

Back to Java Basics - Installing Java

I can't remember learning how to install java at varsity. Maybe, the instructions were on the first tutorial and I never had to bother with it again or it might have just been installed for us. The point being; when I started at my first job and had to install java on both my Windows machine and my allocated workspace on a Unix, I just could not get it right.

Was I crazy?

This might have been the case but then again whenever I feel an inference in the direction that I am crazy, I often find solace in believing that everyone else in this world is crazy and I being the only sane one. Moving on ... I did google the usual "How to install java on windows" and followed the instructions meticulously but still it did not work. You can imagine how I felt when I had to ask one of the seniors to help me install java. It was not straight forward for him as well. He spent about 30 minutes to an hour. Messing around with environment variables through command line and all kinds of weird nonsense. Eventually he got it working and I was amazed. My first thoughts were that if getting java installed was so difficult, how would I cope as a java developer? It turned out that the windows machine I inherited belonged to a techie who installed every possible software he could get his hands on. You might be asking: but this has nothing to do with installing java???

Oh, you are so wrong. I eventually got to understand the sorcery behind installing java. Up to this day I still find senior java engineers struggling with this concept but in a different problem space. As a practising consultant we often have to work with systems that are running in production with multiple versions of java installed but running some undesired version. I have seen even the most senior engineers spending hours trying to sort this mess out.

By contrast to the title, I am not going to show you how install java. I am going to clear up all the mystery that surrounds:
  • Installing java step by step and it just not working for you
  • Having installed a specific version of java but when running "java -version" it says that it is running some other obscure version

Understand Environment Variables

It is of utmost importance that you understand how environment variables work. I have written an article and would suggest that you read this article very carefully and become comfortable with the concepts before proceeding:

Setting Environment Variables on Unix and Windows


If you reached this point, you are really comfortable with environment variable ;-)
Cool, let us continue...

Installing Java

For me there are just 3 important parts to installing java:

  1. The jdk package (jdk folder)
  2. Setting the JAVA_HOME environment variable
  3. Updating the path variable to reference the jdk binary or executables

Point number 2 is not even required, you will understand later.

Once you have the jdk package (folder containing bin, include, jre, lib & Other files) you are 90% there. If you type java through a command line at this point, unfortunately java will not work. The only thing required here is to get the operating system to reference the java binary or executables. If you have read my other post, you should have realised that I am talking about setting the path variable. All we need to do is add the path / location of the bin directory to the path environment variable.

Now, if you try and run java from a command line it should work. If you have followed my instructions correctly and it is still not working then; open up a new command line terminal (fully explained in my other post) or this is were things get really interesting.

Why is the JAVA_HOME environment variable not important? 

The JAVA_HOME environment variable would contain a reference to the base directory of jdk. When setting the path, it is advised that you only add JAVA_HOME/bin so that the operating system can find the binaries executable files. There is no functional difference by specifying the full directory path to the bin directory. It is merely a standard and preferred convention.

Why the convention?

The convention is there so that if you reference an alternate version of java, you would only need to update the JAVA_HOME environment variable. The path variable is quite long containing multiple references to different executable files. It can become "difficult" working with this long string and a typo can affect some other program.
So in a nutshell, the JAVA_HOME environment variable is a convenience variable for the purposes of this article. For your broader knowledge, the JAVA_HOME environment variable might be referenced by programs that require java. Certain web servers or application servers might need this variable be set in order for it to function. It uses this variable to reference libraries or files for it to function correctly.

Installing java today(The Interesting part I spoke about earlier)

I have noticed that when you download a java jdk today, it is packaged with an installation procedure. This procedure completes the process mentioned above on your behalf but even if you use the installer, you might still not get java to work or an undesired version might be running.

Other programs mess with the path variable 

There are so many programs, applications, servers and processes that require java. When installing them, they either reference the java installation already installed on your machine (good program) or contain it's own minimised / customised jdk or jre. If they are installed and append to your path variable, you are done! What happens is that every time you run a java comand, the system evaluates the path variable from left to right and looks in each directory for an executable called java. It will find the first occurrence, which in this scenario is a minimised / customised program specific one execute and terminate. I have personally been bitten by this when installing Oracle server and even some profiling tools.
How do we fix this?
I specifically highlighted, bolded, increased font size and underlined the word "add" earlier in this post. When most people hear add they think that it is append(put at the back). You can even prepend(put in the front) the path variable. This is a very cool trick. Now you have a way of guaranteeing that your desired java executable is executed first. Prepending the path variable saves you from figuring out which reference is problematic. You can figure this out by removing the references one at a time up until you get it working.

Conclusion

The path variable can be a source of major headaches but if you understand both environment variables really well and what is required to install java you should be fine. You would also be comfortable hosting multiple versions of java and be able to reference a specific version if and when required by merely changing your JAVA_HOME environment variable.

As Always, I would love to hear your comments or suggestions on how I can improve this post. I am also more than willing to lend a hand if you are having issues in your environment!

Wednesday, January 8, 2014

Setting Environment Variables on Unix and Windows

In this post I will try and explain the different ways on how to set environment variables on both Windows and Unix operating systems. I am also going to address the scope when setting an environemt variable.

What environment variables are set on my machine?

You can easily check this by typing "set" into a command line terminal on both windows or unix.
Unix and windows:
set
How do you retrieve the value of a particular environment variable?
Unix:
echo $environment_variable
Windows:
echo %environment_variable%

What are environment variables?

Environment variables are operating system level variables that can affect the running of a process. This meaning is slightly obscure but let me try to explain it the way I understand it. The way I see it, environment variables are just aliases for file or directory paths. All they are are just short names or references to a directory on your system. The reason for their existence as you may have realised by now is that they are dynamic and are uniformly used in programs (ie. no hard coding paths).

There is however one special environment variable. This is the "path" variable. The path variable contains multiple directory locations separated by either a ";" on windows or ":" in unix. Each directory location references a program's binary files or executable files. As an example, if you were to type in "java" on the command line interface of both windows or unix, the OS would search each of the directories in the path variable and identify the executable or binary with the name java. When it identifies the executable matching the name, it will execute the process and terminate. This is a very interesting and important statement.

  1. The path variable is evaluated from left to right scanning each directory for an executable matching the name.
  2. It executes the first occurrence of the executable and terminates.
  3. If no matches are made, a command not found message is returned.

How to set environment variables? 


Environment variables can be set either programmatically or manually with both being equal but they can be set for different levels of scope.

Global Scope

This is the highest level and is available to all users of the system. 
Setting:
On unix this can be achieved by adding the environment variable to /etc/profile or /etc/profile.d file. On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a System Variable.
You will only be able to set a variable on this level if you are an administrator on the system.  

Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent.

User Scope

Environment variables set at this level is only available to the user that is logged in.
Setting:
On unix this can be achieved by adding the environment variable to /Users/<username>/.bash_profile file. Once an environment variable is set, you would need to log in again for the change to take effect or you can execute the .bash_profile script which will set the environment variable for the open session.

On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a User variable.
 
Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent for the user.

Session Scope

Environment variables set at this level only last for the duration that the command line terminal is open. It is lost as soon as the terminal is closed.
Setting:
On unix this can be achieved by typing the following in a command line terminal: 
export VARIABLE=value  # for Bourne, Bash and related shells
setenv VARIABLE value  # for csh and related shells
On windows this can be achieved by typing the following in a command line terminal:
SET VARIABLE=value

Script Scope

Environment variables set at this level only last for the duration that the script runs. It is lost as soon as the script terminates, unless the script is executed from command line terminal. In this case the session scope rules apply.
Setting:
Same as in the session scope but is embedded in a shell or batch script.

Setting the "path" environment variable

All of the above applies to normal environment variables. Remember we said that the "path" variable is special and contains multiple directories. You can edit the path environment variable in the same way as mentioned for normal variables as above for the most part. 

Here are other variations which might be useful when setting the "path" environment variable.
When setting on windows use the ";" symbol as a separator:
SET path=C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the ":" symbol as a separator:
export path=/interesting/informing/techies:/important/informing/techies
If you wanted to append or prepend a new directory location to the existing "path" environment variable:      
When setting on windows use the %VAR% notation:
SET path=%path%:C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the $VAR notation:
SET path=$path:/interesting/informing/techies;/important/informing/techies

Remember prepending or appending to the path variable makes a huge difference!!!

Who uses and maintains environment variables?

Applications and programs use environment variables. They also maintain the environment variables by doing the necessary creations, deletions and updates during installation, updating and uninstalling processes. 

Therefore, messing around with environment variables without a clear and proper understanding can cause your system not to work as desired. My suggestion is that if you do want to test something out, try it out on the session scope. In this way your system is not permanently affected. 

With this said, you can also use environment variables in your applications. They are great at abstracting paths and are also a solution to hard coding. 

Conclusion

I hope that this gives you a clear understanding of environment variables and their scope. As always, I would appreciate any feedback and comments are always welcome.

If you have issues in your environment I am more than happy to lend a hand!