Skip to content

Commit b618432

Browse files
committed
SMX4-1191: Add Activiti with Camel example
git-svn-id: https://svn.apache.org/repos/asf/servicemix/smx4/features/trunk@1353980 13f79535-47bb-0310-9956-ffa450edef68
1 parent 90547ed commit b618432

File tree

10 files changed

+454
-1
lines changed

10 files changed

+454
-1
lines changed

NOTICE

+5
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@
99
This product contains software developed by
1010
Open Participation Software for Java (http://www.ops4j.org/).
1111

12+
This software contains unmodified binary redistributions for H2 database engine (http://www.h2database.com/),
13+
which is dual licensed and available under a modified version of the MPL 1.1 (Mozilla Public License)
14+
or under the (unmodified) EPL 1.0 (Eclipse Public License).
15+
An original copy of the license agreement can be found at: http://www.h2database.com/html/license.html
16+

assemblies/apache-servicemix/src/main/filtered-resources/features.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,16 @@
319319
<feature name="activiti" version="${activiti.version}" resolver="(obr)">
320320
<feature version="${cxf.version}">cxf-specs</feature>
321321
<feature version="${aries.version}">transaction</feature>
322+
<feature version="${camel.version}">camel</feature>
322323
<bundle dependency="true">mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.wsdl4j/${wsdl4j.bundle.version}</bundle>
323324
<bundle dependency="true">mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.mybatis/${mybatis.bundle.version}</bundle>
324325
<bundle dependency="true">mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.scripting-api-1.0/${servicemix.specs.version}</bundle>
325326
<bundle dependency="true">mvn:commons-lang/commons-lang/${commons-lang.version}</bundle>
327+
<bundle dependency="true">mvn:com.h2database/h2/${h2.version}</bundle>
326328
<bundle>mvn:org.activiti/activiti-engine/${activiti.version}</bundle>
327329
<bundle>mvn:org.activiti/activiti-osgi/${activiti.version}</bundle>
328330
<bundle>mvn:org.activiti/activiti-camel/${activiti.version}</bundle>
329-
<bundle>mvn:org.apche.servicemix.activiti/org.apache.servicemix.activiti.config/${project.version}</bundle>
331+
<bundle>mvn:org.apache.servicemix.activiti/org.apache.servicemix.activiti.config/${project.version}</bundle>
330332
</feature>
331333

332334
</features>
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
Activiti and Camel Example
19+
==========================
20+
21+
Purpose
22+
-------
23+
This example will show you how to use Activiti inside Apache ServiceMix and how
24+
interact with your Activiti processes from within you Camel routes.
25+
26+
In our example, we define a simple order process, that process the incoming orders
27+
and subsequently waits for its delivery. Once the delivery notification has been
28+
received, another bit of processing occurs before the business process ends.
29+
30+
We use Camel routes to start new process instances and notify running processes
31+
about deliveries. We also use Camel routes to implement the order and delivery
32+
processing itself.
33+
34+
35+
Explanation
36+
-----------
37+
In the Blueprint XML file (activiti-camel.xml), we are:
38+
1. setting up our Camel context with our Camel routes
39+
2. setting up the ActivitiComponent that will allow us to interact between our business process and our Camel routes
40+
3. registering a ContextProvider instance to make your CamelContext available through the ${camel} expression in your
41+
business process definitions files
42+
43+
The OrderProcess.bpmn20.xml business process definition defines the BPMN definition for our process. This process is
44+
automatically deployed as soon as bundle is started:
45+
46+
start --> processOrder --> waitForDelivery --> processDelivery --> end
47+
48+
The ActivitRouteBuilder class defines 4 routes:
49+
1. The first route will process files in the var/activiti-camel/order and for every file, a new business process instance
50+
will be started. The Camel route will also assign a business key to the new process (the file name) and add a few
51+
extra variables to the process.
52+
2. The second route will process files in the var/activiti-camel/delivery and once again uses the file name to notify
53+
running processes about order deliveries.
54+
3. The third route will be triggered by the BPMN process when it executes its 'processOrder' service task.
55+
4. The fourth route will be triggered by the BPMN process when it executes its 'processDelivery' service task.
56+
57+
58+
Prerequisites for Running the Example
59+
-------------------------------------
60+
1. You must have the following installed on your machine:
61+
62+
- JDK 1.6 or higher
63+
64+
- Maven 3.0.2 or higher (for building)
65+
66+
For more information, see the README in the top-level examples
67+
directory.
68+
69+
2. Start ServiceMix by running the following command:
70+
71+
<servicemix_home>/bin/servicemix (on UNIX)
72+
<servicemix_home>\bin\servicemix (on Windows)
73+
74+
75+
Running the Example
76+
-------------------
77+
To install and run the example where you build the example bundle
78+
yourself, complete the following steps:
79+
80+
1. Before being able to run this example, you have to install some additional
81+
features into the container first to add support for the Activiti.
82+
83+
features:install activiti
84+
85+
2. Build the example by opening a command prompt, changing directory to
86+
examples/activiti/activiti-camel (this example) and entering the following Maven
87+
command:
88+
89+
mvn clean install
90+
91+
If all of the required OSGi bundles are available in your local Maven
92+
repository, the example will build very quickly. Otherwise it may
93+
take some time for Maven to download everything it needs.
94+
95+
The mvn install command builds the example deployment bundle and
96+
copies it to your local Maven repository and to the target directory
97+
of this example.
98+
99+
3. Install the example by entering the following command in
100+
the ServiceMix console:
101+
102+
osgi:install mvn:org.apache.servicemix.examples/activiti-camel/${project.version}
103+
104+
4. Once the bundle has been started, you will see a var/activiti-camel/order directory
105+
under your ServiceMix installation directory. If you create files in that directory,
106+
you will see output like this appearing in the log file.
107+
108+
Processing order 1508 created on 2012-06-26 11:50:19
109+
original message: <message/>
110+
Process to handle incoming order file has been started (process instance id 14808)
111+
112+
At that point, you have a running process instance for order 1508 that is waiting for delivery.
113+
114+
5. To notify the process about the delivery, you have to create a file with the same name
115+
in the var/activiti-camel/delivery directory. For example, to signal the delivery of order 1508,
116+
create a file named var/activiti-camel/delivery/1508. As soon as the Camel route picks up the
117+
file, you will see more output from the business process:
118+
119+
Notifying process about delivery for order 1508
120+
Processing delivery for order 1508 created on 2012-06-26 11:50:19
121+
original message: <message/>
122+
123+
124+
Stopping and Uninstalling the Example
125+
-------------------------------------
126+
First, find the bundle id for the deployed example bundle by doing
127+
128+
osgi:list
129+
130+
and looking for a line that looks like this one
131+
132+
[ 317] [Active ] [Created ] [ ] [ 80] Apache ServiceMix :: Examples :: Activiti :: Activiti Camel (${project.version})
133+
134+
In the above case, the bundle id would be 317
135+
136+
137+
To stop the example, enter the following command in the ServiceMix
138+
console:
139+
140+
osgi:stop <bundle_id>
141+
142+
143+
To uninstall the example, enter one of the following commands in
144+
the ServiceMix console:
145+
146+
osgi:uninstall <bundle_id>
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<!--
7+
Licensed to the Apache Software Foundation (ASF) under one or more
8+
contributor license agreements. See the NOTICE file distributed with
9+
this work for additional information regarding copyright ownership.
10+
The ASF licenses this file to You under the Apache License, Version 2.0
11+
(the "License"); you may not use this file except in compliance with
12+
the License. You may obtain a copy of the License at
13+
14+
http://www.apache.org/licenses/LICENSE-2.0
15+
16+
Unless required by applicable law or agreed to in writing, software
17+
distributed under the License is distributed on an "AS IS" BASIS,
18+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
See the License for the specific language governing permissions and
20+
limitations under the License.
21+
-->
22+
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<artifactId>activiti-examples</artifactId>
27+
<groupId>org.apache.servicemix.examples</groupId>
28+
<version>4.5.0-SNAPSHOT</version>
29+
</parent>
30+
31+
<artifactId>activiti-camel</artifactId>
32+
<packaging>bundle</packaging>
33+
<name>Apache ServiceMix :: Examples :: Activiti :: Activiti Camel</name>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>org.apache.camel</groupId>
38+
<artifactId>camel-core</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.activiti</groupId>
42+
<artifactId>activiti-camel</artifactId>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.felix</groupId>
50+
<artifactId>maven-bundle-plugin</artifactId>
51+
<configuration>
52+
<instructions>
53+
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
54+
<Bundle-Description>${project.description}</Bundle-Description>
55+
<Import-Package>*</Import-Package>
56+
<Private-Package>org.apache.servicemix.examples.activiti</Private-Package>
57+
</instructions>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.servicemix.examples.activiti;
18+
19+
import org.apache.camel.Body;
20+
import org.apache.camel.Exchange;
21+
import org.apache.camel.Handler;
22+
import org.apache.camel.Header;
23+
import org.apache.camel.builder.RouteBuilder;
24+
import org.apache.camel.language.Simple;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
import static org.activiti.camel.ActivitiProducer.PROCESS_KEY_PROPERTY;
30+
31+
/**
32+
* Camel routes that interact with the business process defined in the
33+
* OSGI-INF/activiti/OrderProcess.bpmn20.xml file
34+
*/
35+
public class ActivitiRouteBuilder extends RouteBuilder {
36+
37+
private final Helper helper = new Helper();
38+
39+
@Override
40+
public void configure() throws Exception {
41+
/*
42+
* This route will start a new OrderProcess instance. Using the PROCESS_KEY_PROPERTY, we are assigning a
43+
* business key to our process to allow for easier correlation in later processing steps. We are also
44+
* sending a Map containing additional variables to add to the process instance.
45+
*/
46+
from("file:var/activiti-camel/order")
47+
.setBody(bean(helper))
48+
.setProperty(PROCESS_KEY_PROPERTY, simple("file:name"))
49+
.to("activiti:OrderProcess")
50+
.log("Process to handle incoming order file has been started (process instance id ${body})");
51+
52+
/*
53+
* This route will notify a running OrderProcess of an order delivery event. Here too, we are setting the
54+
* PROCESS_KEY_PROPERTY to correlate the delivery message with right order process instance.
55+
*/
56+
from("file:var/activiti-camel/delivery")
57+
.log("Notifying process about delivery for order ${file:name}")
58+
.setProperty(PROCESS_KEY_PROPERTY, simple("file:name"))
59+
.to("activiti:OrderProcess:receiveDelivery");
60+
61+
/*
62+
* The BPMN process can also trigger Camel routes as part of the process. In these routes, the variables that
63+
* you added to the process are available as Exchange properties. The next two routes will be triggered while
64+
* processing the order and the order delivery.
65+
*/
66+
from("activiti:OrderProcess:processOrder")
67+
.log("Processing order ${property.orderid} created on ${property:timestamp}")
68+
.log(" original message: ${property.message}");
69+
70+
from("activiti:OrderProcess:processDelivery")
71+
.log("Processing delivery for order ${property.orderid} created on ${property:timestamp}")
72+
.log(" original message: ${property.message}");
73+
}
74+
75+
/*
76+
* A few helper methods used for routing
77+
*/
78+
public static final class Helper {
79+
80+
/*
81+
* This method will extract information from the Exchange (using Camel annotations) and put them in a
82+
* Map that will be used for setting up the process' variables.
83+
*/
84+
@Handler
85+
public Map getProcessVariables(@Body String body,
86+
@Header(Exchange.FILE_NAME) String filename,
87+
@Simple("${date:now:yyyy-MM-dd kk:mm:ss}") String timestamp) {
88+
Map<String, Object> variables = new HashMap<String, Object>();
89+
variables.put("message", body);
90+
variables.put("orderid", filename);
91+
variables.put("timestamp", timestamp);
92+
return variables;
93+
}
94+
}
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<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"
19+
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
20+
21+
22+
<process id="OrderProcess">
23+
24+
<startEvent id="start"/>
25+
26+
<sequenceFlow id="flow1" sourceRef="start" targetRef="processOrder"/>
27+
28+
<serviceTask id="processOrder" activiti:delegateExpression="${camel}"/>
29+
30+
<sequenceFlow id="flow2" sourceRef="processOrder" targetRef="receiveDelivery"/>
31+
32+
<receiveTask id="receiveDelivery" name="Wait for Delivery" />
33+
34+
<sequenceFlow id="flow3" sourceRef="receiveDelivery" targetRef="processDelivery"/>
35+
36+
<serviceTask id="processDelivery" activiti:delegateExpression="${camel}"/>
37+
38+
<sequenceFlow id="flow4" sourceRef="processDelivery" targetRef="end"/>
39+
40+
<endEvent id="end"/>
41+
42+
</process>
43+
44+
</definitions>

0 commit comments

Comments
 (0)