Integrating Activiti with Third-party Services - Activiti 5.x Business Process Management Beginner's Guide (2014)

Activiti 5.x Business Process Management Beginner's Guide (2014)

Chapter 8. Integrating Activiti with Third-party Services

In the previous chapter, we worked with the REST API provided by Activiti to implement and execute our business process. In this chapter, we will integrate various third-party tools with our Activiti Engine.

In this chapter, we will go through the following topics:

· Using the Liferay Portal with Activiti (http://www.liferay.com/)

· Integrating business rules (Drools Expert) with Activiti (http://www.jboss.org/drools/drools-expert.html)

· Deploying Activiti as an OSGi bundle (http://www.OSGi.org/Main/HomePage)

So far, we just focused on executing process definitions using the Activiti process engine. Now, let’s say there’s a requirement to manage the documents in Alfresco using Activiti; therefore, we will need to integrate them.

Using the Liferay Portal with Activiti

To start using Activiti with the Liferay Portal, we need to understand what a portal is and what the Liferay Portal is. So, we will first discuss portals, then the Liferay Portal, and finally, integrate Activiti with Liferay.

Understanding portals

A portal is where we can get information on various different topics from a single location. In the web world, a web portal refers to a website that offers access to various services, such as search results for other websites, and information regarding finance, health, the weather, and so on. The difference between a website and a web portal is that a web portal can store large volumes of data whereas a website can store only limited quantities of data. An example of a web portal is www.yahoo.com, where we can access various kinds of information, including the latest news, the weather, finance, and so on.

Exploring the Liferay Portal

Liferay is a web-based Java portal. Using Liferay, we can establish a portal on various platforms, such as the web platform, a web content management (WCM) system, the Integration platform, the Collaboration platform, or the Social platform. In this section, we will use Liferay-6.1 to implement our workflow. To do so, we will use Activiti.

Time for action – integrating Liferay with Activiti

To start integrating Liferay with Activiti, we need to install Liferay on our system. For the installation, we need to perform the following steps:

1. We can download the Liferay Version 6.1 Community Edition with the Tomcat bundle from http://www.liferay.com/downloads/liferay-portal/available-releases, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

2. Along with Liferay, you will get various ZIP files downloaded to your system. Within those ZIP files, there is a liferay-portal-tomcat-6.1.1-ce-ga3.zip file that contains the Tomcat bundled with Liferay.

3. On extracting the ZIP file to a specific location, you will get a folder structure as seen in the following screenshot, which will contain the folder in it:

Time for action – integrating Liferay with Activiti

4. To start the Liferay Portal, browse to the bin folder in liferay-portal-6.1.0-ce-ga3 ../liferay-portal-6.1.0-ce-ga3/tomcat-7.0.23/bin. If you are using a Windows system, you will have to execute the startup.bat file from the bin folder, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

If you are executing Liferay in a Linux system, you need to execute the catalina.sh file in your command prompt.

5. In the Linux command prompt, browse to the bin folder of lifeary-portal-6.1-ce-ga3 ../liferay-portal-6.1.0-ce-ga3/tomcat-7.0.23/bin and type the sh.catalina.sh run command, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

6. To check whether Liferay has started, open a web browser, and type the URL, localhost:8080, which will display the home page of Liferay, as you can see in the following screenshot:

Time for action – integrating Liferay with Activiti

7. Log in with the following credentials:

Email Address: <test@liferay.com>

Password: test

8. To integrate Liferay with Activiti, we need to get an Activiti web application WAR file, which is provided by emforge.net at http://www.emforge.net/web/activiti-liferay/files, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

9. As we are using Liferay Version 6.1.0, we need to download the Activiti Liferay 6.1.0 plugin.

10. On downloading the Activiti plugin, you will get the activiti-web-6.1.0.11.war file in your Downloads folder, as you can see in the following icon:

Time for action – integrating Liferay with Activiti

11. Now, we will deploy the activiti-web-6.1.0.11.war file on the Liferay Portal. As we have Liferay Portal already running, we will log in with the following credentials:

Email Address: <test@liferay.com>

Password: test

12. After logging into the Liferay Portal, we need to browse to the Control Panel tab from the Go to menu available at the top-right corner of the Liferay Portal; that is, navigate to Go to | Control Panel, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

13. Selecting the Control Panel tab will redirect you to a page where we need to select the Plugins Installation option from the Server tab, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

14. When we select the Plugins Installation option, it will open a page where we need to click on the Install More Portlets button (as shown in the following screenshot) to install the activiti-web-6.1.0.11.war file:

Time for action – integrating Liferay with Activiti

15. The Install More Portlets option will redirect you to a page that has various options. From those options, we need to select Upload File; in Upload a WAR file..., we need to select the activiti-web-6.1.0.11.war file and click on the Install button, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

16. On successful installation of the WAR file, a message regarding the successful installation of the file will be displayed on the console, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

17. Once the .war file is deployed successfully, you can view some of the default workflow files (as shown in the following screenshot) by navigating to Go to | Control Panel | Portal | Workflow:

Time for action – integrating Liferay with Activiti

18. To upload a new workflow, you can select the Add option, provide a name for your business process, and upload a process file to Liferay, as shown in the following screenshot:

Time for action – integrating Liferay with Activiti

19. On successfully uploading the process file, you can use it with any of the resources available for workflow implementation.

What just happened?

In this section, we went through the integration of Activiti with the Liferay Portal. We used a web plugin to configure Activiti with Liferay and uploaded a business process file into the Liferay Portal.

Integrating business rules in Activiti

Business rule is a vast topic and there are various books available that discuss it. In this section, we will give you a brief introduction to business rules and how to use and implement them.

We can think of a business rule as a plain set of words that say something meaningful about the business. For example: customers making a payment with a platinum card should be given a 20 percent discount on the purchase of products. This is a business rule, but it has been explained in simple words that a lay person can also understand.

To create and implement business rules, we require a business rule management system (BRMS) and Drools is an open source BRMS. To know more about Drools, you can refer to Drools JBoss Rules 5.0 Developer’s Guide, by Packt Publishing. In this chapter, we will cover just a brief overview of Drools. It is a JBoss project with the Apache license. The project consists of various modules, such as Drools Expert, Drools Guvnor, Drools Fusion, and Drools Planner.

Drools Expert

Drools Expert is a JSR-94-standard rule engine that is responsible for defining business rules and executing them. The rules are written using a domain specific language (DSL).

Drools Guvnor is a web-based user interface which we can use to manage business rules. It also supports the testing and authoring of business rules. It is considered to be a centralized repository for storing Drools rules.

Drools Fusion

The Drools Fusion module is used for handling complex event processing (CEP). The action taken on a set of data is based on the pattern matched on analyses of events that have occurred.

Drools Planner

The Drools Planner module is responsible for optimizing automated planning solutions.

Time for action – integrating and implementing business rules with Activiti

In this section, we will focus on the Drools Expert module. We can work with the Drools Expert module without configuring the rest of the modules. Activiti provides a business rule task to implement business rules in our process, as shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

In this chapter, we will modify the laptop ordering process that we created in the Time for action – designing your first process section of Chapter 3, Designing Your Process Using the Activiti Designer. We will add a business rule task after the payment task in our process, as you can view in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

We will perform the following steps to proceed with the implementation:

1. We will open our existing Laptop Human Process tab and add BusinessRuleTask from the Palatte | Task tab, as shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

2. On adding the BusinessRuleTask, we need to set its properties. Select BusinessRuleTask, open the Properties window, and select the Main config tab. We need to populate the Rule names and Input variables properties, as shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

3. In the Rule names field, we need to provide the name of the rule that we want to call from BusinessRuleTask.

4. Now, we will create a rule file with the .drl extension in which we will have our business logic written.

5. We can create a new file in Eclipse by navigating to File | New | File, as shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

6. The File option opens a pop-up window where we will create a file named payment.drl on a specific path, as shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

7. In the payment.drl file, we will write our business logic. The logic here is as follows: if the payment is made by credit card, provide a 5 percent discount on the price of the laptop. The following lines of code show the logic of this:

8. package org.activiti.book;

9. import com.book.activiti.LaptopOrder;

10.

11.rule “paymentType”

12.when

13. lapOrder : LaptopOrder(paymentMode == “creditCard”)

14.then

15. lapOrder.setAmtToBePaid((lapOrder.getLaptopAmount()*lapOrder.getLaptopQuantity())-((lapOrder.getLaptopAmount()*lapOrder.getLaptopQuantity())*5/100));

end

16. We need to create an activiti.cfg-mem-rules.xml file in the src/test/resources folder of our application to enable the rule engine, as shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

17. In the activiti.cfg-mem-rules.xml file, we need to provide an org.activiti.engine.impl.rules.RulesDeployer class, as shown in the following code snippet; this class is responsible for executing our business rule:

18.<bean id=”processEngineConfiguration”class=”org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration”>

19.

20. <property name=”databaseSchemaUpdate” value=”true” />

21. <property name=”customPostDeployers”>

22. <list>

23. <bean class=”org.activiti.engine.impl.rules.RulesDeployer” />

24. </list>

25. </property>

</bean>

26. After adding the tags shown in the preceding code within the XML file, we will get the following output:

Time for action – integrating and implementing business rules with Activiti

27. Now, we will create a LaptopOrder class file, as shown in the following screenshot, that will contain the getter and setter methods of our variables:

Time for action – integrating and implementing business rules with Activiti

28. The code in the LaptopOrder class will contain information regarding the customer and the laptop details, as follows:

29.public class LaptopOrder implements Serializable {

30.

31. private static final long serialVersionUID = 1L;

32.

33. private String customerName;

34. private String laptopName;

35. private long laptopQuantity;

36. private long laptopModelNo;

37. private String paymentMode;

38. private long amtToBePaid;

39. private long laptopAmount;

40. public long getLaptopAmount() {

41. return laptopAmount;

42. }

43.

44. public void setLaptopAmount(long laptopAmount) {

45. this.laptopAmount = laptopAmount;

46. }

47.

48. public long getAmtToBePaid() {

49. return amtToBePaid;

50. }

51.

52. public void setAmtToBePaid(long amtToBePaid) {

53. this.amtToBePaid = amtToBePaid;

54. }

55.

56. public String getCustomerName() {

57. return customerName;

58. }

59.

60. public void setCustomerName(String customerName) {

61. this.customerName = customerName;

62. }

63.

64. public String getLaptopName() {

65. return laptopName;

66. }

67.

68. public void setLaptopName(String laptopName) {

69. this.laptopName = laptopName;

70. }

71.

72. public long getLaptopQuantity() {

73. return laptopQuantity;

74. }

75.

76. public void setLaptopQuantity(long laptopQuantity) {

77. this.laptopQuantity = laptopQuantity;

78. }

79.

80. public long getLaptopModelNo() {

81. return laptopModelNo;

82. }

83.

84. public void setLaptopModelNo(long laptopModelNo) {

85. this.laptopModelNo = laptopModelNo;

86. }

87.

88. public String getPaymentMode() {

89. return paymentMode;

90. }

91.

92. public void setPaymentMode(String paymentMode) {

93. this.paymentMode = paymentMode;

94. }

}

95. To execute the business process, we will create a BusinessRuleTest class file, as shown in the following screenshot, using which we will test our business process:

Time for action – integrating and implementing business rules with Activiti

96. The code for testing our business process is as follows:

97.public class BusinessRuleTest {

98.

99. @Rule

100. public ActivitiRule activitiRule = new ActivitiRule(

101. “activiti.cfg-mem-rules.xml”);

102.

103. @Test

104. @Deployment(resources = { “laptopOrderHumanProcess.bpmn20.xml”,

105. “payment.drl” })

106. public void testLaptopProcess() {

107.

108. Map<String, Object> variableMap = new HashMap<String, Object>();

109. Map<String, Object> completeOrder = new HashMap<String, Object>();

110.

111. LaptopOrder laptopOrder = new LaptopOrder();

112. laptopOrder.setCustomerName(“Irshad”);

113. laptopOrder.setLaptopModelNo(3420);

114. laptopOrder.setLaptopName(“Dell”);

115. laptopOrder.setLaptopQuantity(2);

116. laptopOrder.setPaymentMode(“creditCard”);

117. laptopOrder.setLaptopAmount(30000);

118.

119. variableMap.put(“laptopOrder”, laptopOrder);

120.

121. ProcessInstance processInstance = activitiRule.getRuntimeService()

122. .startProcessInstanceByKey(“laptopHumanProcess”, variableMap);

123. assertNotNull(processInstance);

124.

125. TaskService taskService = activitiRule.getTaskService();

126.

127. Task task = taskService.createTaskQuery().singleResult();

128. completeOrder.put(“acceptOrder”, “true”);

129. taskService.complete(task.getId(), completeOrder);

130.

131. task = taskService.createTaskQuery().singleResult();

132. taskService.complete(task.getId(), variableMap);

133.

134. }

}

135. Now, we can perform a test on our business process by executing the BusinessRuleTest.java file. We can run the class file as a JUnit test.

136. On successful execution of the test file, we should see the output on the console, which should look similar to that shown in the following screenshot:

Time for action – integrating and implementing business rules with Activiti

What just happened?

In this section, we configured Activiti to use Drools rule engine in which we wrote our business logic.

Deploying Activiti as an OSGi bundle

Before starting with the implementation, we will get a brief introduction to OSGi. The following definition is given on Wikipedia:

“The OSGi framework is a module system and service platform for Java programming language that implements a complete and dynamic component model, something that does not exist in standalone Java/VM environments. Applications or components (coming in the form of bundles for deployment) can be remotely installed, started, stopped, updated, and uninstalled without requiring a reboot; management of Java packages/classes is specified in great detail. Application life cycle management (start, stop, install, etc.) is done via APIs that allow for remote downloading of management policies. The service registry allows bundles to detect the addition of new services, or the removal of services, and adapt accordingly.”

For more information, you can refer to Instant OSGi Starter, Packt Publishing.

Time for action – integrating Activiti with Apache Karaf

Apache Karaf is a small OSGi bundle with a lightweight container and is capable of deploying various applications and components. To deploy Activiti as an OSGi, we have to carry out the following steps:

1. To start the integration of Activiti with Karaf, there are some prerequisite installations to be done on our system. JDK should be installed with Version 1.5 or higher and Maven 3.0.3 should be configured in your system.

2. We need to download Activiti Karaf from http://code.google.com/p/activiti-karaf/downloads/list. This link contains files for both Linux and Windows operating systems, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

3. You can download the files corresponding to the operating system you are using. As we are working with a Linux system, we will download the Linux distribution file.

Time for action – integrating Activiti with Apache Karaf

4. Now, we will extract the file from a specific path. To use Activiti with Karaf, we need to do some configuration. You can browse to the ../activity-karaf-dist-1.0.0-SNAPSHOT/sources/activity-bpmn-archtype folder; there will be only an src folder and a pom.xml file,as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

5. As this archetype is currently not available in the public Maven repository, we need to install it in the public repository by executing the mvn install command from the command prompt, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

6. The mvn install command will start installing the activiti-bpmn-archetype in the local repository. The installation process will take some time. Once the installation is completed, you should get a BUILD SUCCESS message as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

7. After the successful installation of activiti-bpmn-archetype, we can create a bpmn project using activiti-bpmn-archtype.

8. To create the project, browse to the sources folder of your activiti-karaf-dist-1.0.0-SNAPSHOT path and type the following command:

mvn archetype:generate -B -DarchetypeGroupId=org.activiti.karaf.archetypes -DarchetypeArtifactId=activiti-bpmn-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -DgroupId=com.book.activiti.OSGi -DartifactId=activiti-OSGi -Dversion=1.0.0-SNAPSHOT

9. On the successful execution of the preceding command, you will get a BUILD SUCCESSFULL message on the console, as shown in the following screenshot, and a project will be created in your sources folder:

Time for action – integrating Activiti with Apache Karaf

10. Our project activiti-OSGi will be created in the sources folder of activiti-karaf-dist-1.0.0-SNAPSHOT. You can browse to ../activiti-karaf-dist-1.0.0-SNAPSHOT/sources and you should have folders mentioned in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

11. Now, we need to modify the pom.xml file of our activiti-OSGi project. The pom.xml file is located at the ../activiti-karaf-dist-1.0.0-SNAPSHOT/sources/activiti-OSGi path.

12. We need to make changes in the <parent></parent> element of the pom.xml file with the given code snippet:

13.<parent>

14.

15. <groupId>org.activiti.karaf</groupId>

16.

17. <artifactId>activiti-karaf-parent</artifactId>

18. <version>1.0.0-SNAPSHOT</version>

19.

20. <relativePath>../activiti-karaf-parent</relativePath>

21.

</parent>

22. By providing the previous groupId and artifactId, our project becomes an OSGi Activiti project and we can run it on the OSGi container.

23. In your ../activiti-karaf-dist-1.0.0-SNAPSHOT/sources/activiti-OSGi/src/main/process path, you will find a bpmn file named activiti-OSGi.bpmn20.xml, which is created in the project by default.

24. Now in order to execute our project, we need to create a package structure. To create a package structure, we have to type the mvn clean package command in our project folder path as mentioned in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

25. On the successful execution of the previous command, a target folder is created within ../activiti-karaf-dist-1.0.0-SNAPSHOT/sources/activiti-OSGi, which contains the activiti-OSGi-1.0.0-SNAPSHOT.jar file and various classes, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

26. Now, we will deploy our activiti-OSGi project on Activiti Karaf. To start activiti-karaf, we have to execute the ./karaf command from the command prompt by browsing to the ../activiti-karaf-dist-1.0.0-SNAPSHOT/bin path as viewed in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

27. On starting Activiti Karaf, we have started the Activiti OSGi engine.

28. Now, open a web browser and type http://localhost:8181/activiti-explorer. This URL will open the Activiti engine and you will get the login page, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

29. You can log in with the user ID kermit and password kermit. Then, browse to the Manage | Deployments | Upload new link and upload the activiti-OSGi.bpmn20.xml file, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

30. When the file is uploaded successfully, it will be available in Manage | Deployments, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

31. To view the process diagram, you can browse to Process | Processs definitions | activiti-OSGi where you will see the graphical representation of your business process, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

32. As we have deployed the process on the Activiti engine, we will now go back to the command prompt and check the list of processes deployed by executing the act:list command on the command prompt; we should be able to view our activiti-OSGi process in the list, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

33. We can also start the process from the OSGi command prompt by executing the act:start [process definition id] command, as shown in the following screenshot:

Time for action – integrating Activiti with Apache Karaf

What just happened?

In this section, we used an OSGi container called Apache Karaf to implement our Activiti business process.

Summary

In this chapter, we have come across configuring the Liferay Portal to use the Activiti .bpmn files, using a Drools rule engine within an Activiti business process, and using an OSGi container called Apache Karaf to implement an Activiti business process. In the next chapter, we will see how to implement advanced workflows using a Sub-Process and parallel gateways, and monitoring using BAM.