Showing posts with label Activiti. Show all posts
Showing posts with label Activiti. Show all posts

Wednesday, January 22, 2014

Activiti/camunda BPM: custom behavior and BPMN extension elements using Blueprint

Introduction 

 

In the last few weeks I have been working on a problem regarding OSGi-Blueprint and Activiti. Because it wasn't as easy as I would have hoped I want to share my solution with you. I will start by explaining my environment, show you the problem and then I will explain my first attempt. After that I will present my solution. Finally I will show some ideas how to make it better and things that I did not test.

Just a short hint about the writing: when I reference a class or some XML it's written in italic, e.g. Object. When you see "process engine", I am talking about the whole thing, but when you see ProcessEngine it's the actual class.

My environment

 

I use the Activiti-framework in version 5.12.1, Apache Aries in version 1.0.0 with a little modification and my own ProSt bundles. ProSt can be found here. The README.md explains why and what I changed in Aries.

I haven't tried, yet, but I am pretty sure that camunda BPM suffers the same problem because
both share the same MailActivityBehavior and BlueprintELResolver classes and use <extension-elements> for injection. So if you prefer camunda and see "activiti" somewhere you just have to replace it with "camunda" in your head ;-)

The problem 

 

In general, I just wanted to send an e-mail during my process-execution. Sounds pretty easy, right?

My SendMailWithAttachmentBehaviour class extends the previously mentioned MailActivitiBehavior class. The process definition contains all the necessary information to send the e-mail, e.g. from, to and subject. Only the attachment is missing, which I get from the execution environment.

Because I use Blueprint I cannot use the activit:class or type="mail" attributes in the process definition. I have to declare the class this way:
activiti:deleExpression="${sendMailWithAttachmentBehavior}"

A little hint: the name in the braces has to match the one used as bean id in the blueprint.xml.

The other ways do not work with OSGi because of class visibility etc.

The easy part was to extend the BlueprintELResolver class (ProStBlueprintELResolver) and add a way to add custom behavior classes at the moment.

So, what happens when the process engine tries to resolve the expression?
When the bundle is loaded Blueprint creates a dynamic proxy and registers it at the ProStBlueprintELManager.
After the process reaches the ServiceTask which delegates to the ${sendMailWithAttachmentBehaviour} the process-engine asks its ExpressionLanguageResolvers if they know something with the name "sendMailWithAttachmentBehaviour". Logically the proxy is found.
After that the process engine tries to set the extension-elements at the class.
First it tries to find setter methods and if it cannot find setters it tries field injection. (see ClassDelegate.applyFieldDeclaration())
Both ways do not work.
But why?
Of course a proxy does not have any fields. But why is it not possible to just add the setters to the SendMailWithAttachmendBehaviour class?
The call is proxy.getClass().getMethods() and according to the documentation this will return all the methods of the interfaces that the proxy was created with. ActivityBehavior does not declare the setSubject() etc. methods because they are only needed for e-mails.

First attempt

 

At first I thought the solution was quite obvious. I would just export a second interface containing the setters like this:

<bean id="sendMailWithAttachment" class="de.blogspot.wrongtracks.prost.example.behaviour.SendMailWithAttachmentBehaviour" />
  
<service ref="sendMailWithAttachment">
  <interfaces>
<value>org.activiti.engine.impl.pvm.delegate.ActivityBehavior</value>
<value>de.blogspot.wrongtracks.prost.example.behavior.ExtensionElementsMailSetter</value>
  </interfaces>
</services>
But wait, if you take a look at the (old) context.xml you can see that my reference listener just listens for ActivityBehavior and not the other interface. That's why the created proxy won't contain the methods from the other interface. Too bad...

The solution

 

I found the solution accidentally while reading the Apache Aries Blueprint documentation. This chapter points out that you can also listen for service references.
I changed the methods to accept a ServiceReference instead of a ActivitiyBehavior and when the expression should be resolved I use the BundleContext to get the service. At that point it is not a proxy, it is the implementation.

Then everything works just fine. I don't even need the setter interface anymore.
You can see the solution when you look at the new context.xml and the previously mentioned ProStBlueprintELResolver. (the previously showed link pointed to an old version so nothing would be spoiled ;-) )

That's it, that is my solution to add custom behavior to the process engine and use extension elements in the BPMN XML.

How could we improve the whole thing?

 

Strangely, I have no idea how the whole thing could be improved. I would like to hear your ideas. Also, I would like to know if you think that the way presented here is good or bad or something in between.

What didn't I try?

 

You should note that I have not tried to find out how JavaDelegates behave in the same situation. I just did not have time and I wanted to show you my solution as soon as I finished it.

Friday, July 12, 2013

Comparison of different BPMN-modelling tools

Recently, I stumbled upon the problem that the Activiti-engine wouldn't accept a BPMN-XML-file which I modeled with camunda Modeler.
No error message or warning was displayed, but I could see in the log-file that parsing started.
The process just didn't arrive in the repository.

Because of that, I was wondering, how good the interoperability of the different engines and tools is at the moment.
BPMN is standardised and should work on every tool and engine. But as usual, reality shows us, that it's different.

This blog post covers several modelling tools and how good they work together. In a later blog post I'll feed the different BPMN-files to different engines.

I chose the tools for no special reasons.
Activiti and camunda because I work with Activiti (and camunda forked it).
SemTalk because I do have a license for it (it's the only commercial tool I chose).
The rest is more or less randomly in the list and because they are available for free.

I compared:
  • Activiti Modeler 1.0 (Activiti Version 5.13)
  • camunda Modeler 2.0.12
  • Yaoqiang Version 2.2.2
  • Bizagi 2.5.1.1
  • Bonita BPM Studio 6.0.0
  • MS Vision 2010/ SemTalk 4.2.0.4230
  • Activiti Designer  5.12.0
You can find my results in a PDF, which I placed here.

To give a short explanation of the colours used:
  • green -> everything was alright
  • yellow -> it worked with some problem
  • red -> it didn't work or there were some major problems
  • grey -> it couldn't be tested or was unnecessary.
Bizagi is completely grey, because it has no im- or export for BPMN-XML.

This is the example process I used:
I wanted the example process to be very simple. It includes the basics, which I think should be supported.
It was modeled with every tool and opened with every tool. When testing if editing works, I added a task to the second pool.

Next to the result-table in the PDF, I took some notes during the tests, which I want to share:
  • Activiti Modeler can't display pools or lanes
  • Activiti Modeler can't display message flows
  • Activiti Modeler needs a Tomcat server (or similar), because it's part of Activiti Explorer
  • Yaoqiang would work better with some guides
  • camunda Modeler needs Eclipse, because it's a plug-in
  • modelling a flow (arrow) in SemTalk is too complicated
  • Yaoqiang, because of it's page layout, becomes confusing with big diagrams
  • Bizagi, Bonita and Yaoqiang validate the model
  • To show message passing, Bonita forces you to explicitly model sender and receiver
  • for Activiti Modeler, every file hat to be a .bpmn20.xml and not just .bpmn 
  • Activiti Designer can't display message flows

I don't want to name a winner, because I depends on what you're working on.
Some modelling tools are embedded (like Activiti Modeler and Bonita) and some are standalone (like Yaoqiang) and there are different use-cases and environments in which you would prefer one or the other tool.

But I got to admit, that I am a little bit disappointed by Activiti Modeler. It can't work with any of the the BPMN-files the other tools exported.

I hope my results will be helpful for people who have to chose a modelling tool and I hope that some of the tool-developers will try to improve their tools because of my results. Have fun ;)

Update: I added the results for Activiti Designer to the PDF. It works slightly better than the Modeler but could be better.

Tuesday, June 4, 2013

Using the Activiti-Engine as bundle in JBoss AS 7.2

Introduction


I am quite new to OSGi and when trying to deploy and use the Activiti-Engine as OSGi bundle in JBoss (I'll usw "JBoss" as abbreviation for JBoss AS 7.2) I have had some problems. 
The only example about OSGi and Activiti I was able to find was the one from Activiti in Action which uses Apache Karaf. Of course those environments differ a lot.
So I wanna share my knowledge with you.

 

Motivation 

 

Before we start, I'd like explain why I want to use the combination of Activit, OSGi and JBoss. I won't argue about the pros and cons of using OSGi, there is enough literature about that.

The fact that Activiti scans every new bundle if it contains a process definition will become very helpful for future processes.
Also, updating a process definition by replacing a single bundle and not replacing e.g. a whole WAR-file, seems like an advantage.

But, why do I want to use JBoss and not e.g. Karaf?

When JBoss changed to version 7 they restructured their whole server to use OSGi/JBoss modules. So, OSGi is a part of JBoss.
Last time I checked, JBoss was the only fully JEE 6 compliant server with OSGi integration and because I need EJBs for client - server communication I need JBoss. (At this point, I have no idea how to combine EJBs and OSGi but I'll try to figure that out, soon)

Those are my reasons so let's see how we can get those three running.

 

Let's start

 

Environment


To make sure that everyone can easily repeat my steps, I'll start with the environment/versions:
  • JBoss AS 7.2
  • OSGi 4.2 (included in JBoss)
  • Activiti 5.12.1
Please notice, that I use JBoss AS 7.2, which you have to compile yourself at the moment. With version 7.1.1 I have had a problem.
I had the server running locally under Linux Mint 14.1.

 

Bundle dependencies 


The example from Activiti in Activiti uses a feature.xml to resolve all the dependencies and install them from a Maven repository. Unfortunately, this involves some Karaf "magic". That's why I had to download all dependencies manually. (I also tried

mvn org.apache.felix:maven-bundle-plugin:wrap/bundleall
on the activiti-engine and -osgi project, but that didn't work out for me)

We need the following JARs (I added links to make downloading them easier):

Downloading yourself Apache Aries seems a little bit odd because the JBoss documentation states that there is blueprint support. Nevertheless, I couldn't find, nor activate it, so I was forced to include it myself.

Another interesting thing is, that I couldn't start the included h2 database (com.h2database.h2). That's why I downloaded it, too. Please notice, that you will only need the h2-bundle if you want to use a h2-database (makes sense, doesn't it ;) )

We could throw all those bundles into the <<jboss-root>>/standalone/deployments directory (if you're using JBoss in standalone-mode), but I think a better approach is to place the more general JARs in the <<jboss-root>>/module directory. 
    I placed the three aries-bundles, the h2-bundle and the two slf4j-bundles there.
    For every bundle you have to repeat the following steps:
    If the bundle name is e.g. com.a.b.c.jar create in <<jboss-root>>/module the directory com/a/b/main and place the bundle in the main directory. (Don't worry about the <<jboss-root>>/module/system -directory.)

    After that, alter the standalone.xml and add a new capability under the OSGi-Subsystem, e.g.:

    <subsystem xmlns="urn:jboss:domain:osgi:1.2" activation="eager">
       <properties>

        ...
       </properties>
       <capabilities>
          <capability name="com.a.b.c" startlevel="1"/>
       </capabilities>

    </subsystem>

    Pick whichever startlevel you think is appropriate.

    In fact, you can pick any directory-structure you want to, as long as the path in the standalone.xml is correct. That means you could place the h2-bundle e.g. in <<jboss-root>>/module/org/foo/bar/main and JBoss would still be able to find it. Only the last directory has to be named "main".

    After you've done that, take the other bundles an put them into  
    <<jboss-root>>/standalone/deplyoments

    An that's all. These steps should get Activiti running as OSGi-bundle.
    My whole osgi-subsystem configuration looks like this:

    <subsystem xmlns="urn:jboss:domain:osgi:1.2" activation="eager">
                <properties>
                    <property name="org.osgi.framework.startlevel.beginning">
                        2
                    </property>
                </properties>
                <capabilities>
                    <capability name="javax.servlet.api:v25" startlevel="1"/>
                    <capability name="javax.transaction.api" startlevel="1"/>
                    <capability name="org.osgi.core" startlevel="1"/>
                    <capability name="org.osgi.enterprise" startlevel="1"/>
                    <capability name="org.slf4j" startlevel="1"/>
                    <capability name="org.slf4j.impl" startlevel="1"/>
                    <capability name="org.apache.aries.util" startlevel="1"/>
                    <capability name="org.apache.aries.proxy" startlevel="1"/>
                    <capability name="org.apache.aries.blueprint" startlevel="1"/>
                    <capability name="org.h2" startlevel="1"/>
                    <capability name="org.apache.felix.log" startlevel="2"/>
                    <capability name="org.jboss.osgi.logging" startlevel="2"/>
                    <capability name="org.apache.felix.configadmin" startlevel="2"/>
                </capabilities>
            </subsystem> 


    And in my deployments-directory you can find those jars:
    • activiti-bpmn-converter-5.12.1.jar
    • activiti-bpmn-model-5.12.1.jar
    • activiti-engine-5.12.1.jar
    • activiti-osgi-5.12.1.jar
    • com.springsource.javax.transaction-1.1.0.jar
    • com.springsource.org.apache.commons.lang-2.4.0.jar
    • joda-time_2.1.0.jar
    • org.mybatis.mybatis_3.1.1.jar

     

    Blueprint activation

     

    The Activiti-OSGi bundle contains the basic classes Blueprint needs. But we have to register all those services. That's why we need a Blueprint content.xml.

    Thanks to Tijs Rademakers we can copy that from here. It's the example code from/for Activiti in Action.
    We can also use the book-osgi-app in JBoss.
    Just run mvn package on the book-engine, book-process and book-task projects.

    After packaging those three, I had to make a minor change in the MANIFEST.MF and content.xml of the book-engine-project.
    In the content.xml you have to replace the jdbc-url with whatever fits to your database.
    In the MANIFEST.MF I had to remove the Import-Package versions for everything related to activiti, because the generated MANIFEST requires version [5.9, 6) and my activiti-bundles don't contain any version information.

    Now place those three bundles into the deployment-directory and everything should work as expected.
    To test the deployment, I wrote a simple bundle with an activator to look up the Deployment-Service and see, if a process defnition could be found. 

    Summary 

     

    Get all the bundles, place some in the modules-directory and some in the deployment-directory, copy some content.xmls and everything should work. Sounds easy, but it took
    me a while to figure it out ;)

    So have fun with Activiti, OSGi and JBoss!
    In case you find any mistake, feel free to point them out.

     

    Copyright @ 2013 Wrong tracks of a developer.

    Designed by Templateiy