cancel
Showing results for 
Search instead for 
Did you mean: 

Activiti + osgi

bhj2
Champ in-the-making
Champ in-the-making
Does anybody already use activiti with OSGI? Was it a good experience?

Best regards!
30 REPLIES 30

trademak
Star Contributor
Star Contributor
Hi,

In my example I show how you can invoke Java service tasks deployed in a seperate bundle from your process definition.
You're absolutely right that the Script task is not working. This has to do with the script task implementation and the OSGi classloading.
So for now you can use the Java service task approach with OSGi.

Best regards,

kaech
Champ in-the-making
Champ in-the-making
Thanks for the quick response!

kaech
Champ in-the-making
Champ in-the-making
Tijs,

using ServiceTasks basically works.
But I have problems with custom data types which are required in our enterprise scenario.

This is my process:

  <process id='businessProcess' name='Business process'>
    <startEvent id='start' />
    <sequenceFlow id='flow1' sourceRef='start' targetRef='serviceTask1'/>
    <serviceTask id='serviceTask1' activiti:delegateExpression='${taskBean1}'/>
    <sequenceFlow id='flow2' sourceRef='serviceTask1' targetRef='serviceTask2'/>
    <serviceTask id='serviceTask2' activiti:delegateExpression='${taskBean2}'/>
    <sequenceFlow id='flow3' sourceRef='serviceTask2' targetRef='end'/>
    <endEvent id='end'/>
  </process>

In taskbean1 I create a custom object like this:

public void execute(ActivityExecution execution) throws Exception {
  Customer customer = new Customer();
  customer.setName("Max");
  execution.setVariable("customer", customer);
}

In taskbean 2 I try to read the custom object like this:

public void execute(ActivityExecution execution) throws Exception {
  Customer customer = (Customer) execution.getVariable("customer");
  System.out.println("Customer name is " + customer.getName());
}

The taskbeans reside in a task bundle as shown in your Example.
When I run this process I get an exception from Activiti saying that the Customer object is unknown.

org.activiti.engine.ActivitiException: couldn't find type for org.mytypes.Customer@c6d2a0
at org.activiti.engine.impl.variable.DefaultVariableTypes.findVariableType(DefaultVariableTypes.java:62)
at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.createVariableLocal(VariableScopeImpl.java:212)
at org.activiti.engine.impl.persistence.entity.VariableScopeImpl.setVariable(VariableScopeImpl.java:164)
at net.pleus.sbp.process.code.JavaTask.execute(JavaTask.java:14)

Actually that is what I expected, cause the Customer type is only visible in the bundle containing the tasks not in the Activiti bundle.

A solution would be to use only standard types (int, string, etc.). But this would be a great limitation as typical enterprise applications need to use custom complex datatypes.
Another option might be to add a dynamic import to the Activiti-OSGi-bundle.
What do you think? What would be the best way to go?

trademak
Star Contributor
Star Contributor
Hi,

Right that's not very elegant.
I'll have a look into this and how we can improve it in the Activiti OSGi module.

Best regards,

kaech
Champ in-the-making
Champ in-the-making
There is another issue.
I slightly modified the example from above and used plain strings as variables like this:

taskbean1:

public void execute(ActivityExecution execution) throws Exception {
  execution.setVariable("customer", "Max");
  for(String name: execution.getVariableNames()){
    System.out.println("TaskBean1-Variable=" + name);
  }
  execution.takeAll(execution.getActivity().getOutgoingTransitions(),Collections.singletonList(execution)); 
}

taskbean2:

public void execute(ActivityExecution execution) throws Exception {
  for(String name: execution.getVariableNames()){
    System.out.println("TaskBean2-Variable=" + name);
  }
  String customer = (Customer) execution.getVariable("customer");
  System.out.println("TaskBean2-Customer name is " + customer);
  execution.takeAll(execution.getActivity().getOutgoingTransitions(),Collections.singletonList(execution)); 
}

When I execute the process the following is dumped on the console:

TaskBean1-Variable=Max
TaskBean2-Customer name is null

It seems that variable customer is properly written in TaskBean1 but not available in TaskBean2.
How can that be? Do you have any suggestions what could be wrong here?

trademak
Star Contributor
Star Contributor
Hi,

I've committed updates to the activiti-osgi and activiti-engine modules to make a deployment on Apache Karaf work out-of-the-box without additional coding.
The OSGi example of Activiti in Action is changed accordingly.
You now can run script tasks on the Apache Karaf container and you can add JavaBean variables like Customer.
You don't have to implement the ActivityBehavior interface no longer, you can now use the easier JavaDelegate approach.

Best regards,

kaech
Champ in-the-making
Champ in-the-making
I updated everything according to the current book example and built Activiti 5.8-SNAPSHOT to get the latest version.
Now the customer string variable is passed from taskBean1 to taskBean2.  Smiley Happy

But I still can't use our custom Customer type neither in the Groovy script nor in the JavaDelegate classes.
The Customer class can not be found.
Am I missing something? What did you change to make the complex types work?

trademak
Star Contributor
Star Contributor
Hi,

But my example includes a Customer object, did you check that?
Does your object implements Serializable?

Best regards,

kaech
Champ in-the-making
Champ in-the-making
Now it works. I can create Customers in Groovy and JavaDelegates and consume them in other JavaDelegates later in the process.  Smiley Very Happy

But when I update the bundle containing the Customer type I get an exception in the JavaDelegate at the following line

Customer customer = (Customer) execution.getVariable("customer");
Error executing command: org.mytypes.Customer cannot be cast to org.mytypes.Customer.
The customer variable was created in a ScriptTask using Groovy.
When I create the Customer object in a JavaDelegate it works.

Any ideas?

Another question. I have subscribed to Activitiinaction MEAP. Where can I find the information about Activiti + OSGi?

trademak
Star Contributor
Star Contributor
Hi,

Did you uninstall the previous bundle and install the new version? Or did you just install the new version bundle?
The Activiti + OSGi chapter will become available over the next weeks. I'm currently finishing writing it.

Best regards,