Skip to content

Commit

Permalink
Issue 36: Introduce optional variables
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanpelikan committed Nov 21, 2017
1 parent 482a4a6 commit 87322c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ Use the following expression in a ServiceTask to send all the process instance v
```
${camel.sendTo('<camel endpoint>')}
```
Hint: You will also get variables which were set but contain a null value.

Alternatively you can specify which process instance variables you want to send to Camel with:

```
${camel.sendTo('<camel endpoint>', '<comma-separated list of process variables>')}
```

Hint: missing or null value variables will cause to throw an IllegalArgumentException. You can append a question mark to each name of a variable which is allowed to be missing or null (e.g. 'mayBeNullVar?,mustNotBeNullVar').

Additionally you can specify a correlationKey to send to Camel. It can be used to correlate a response message. The route for the response must contain a parameter correlationKeyName with the name of the process variable which is used for correlation:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
Expand Down Expand Up @@ -44,9 +46,15 @@ public Object sendTo(String endpointUri, String processVariables,
String correlationId) {
Collection<String> vars;
if (processVariables == null) {
vars = new LinkedList<String>();
ActivityExecution execution = Context.getBpmnExecutionContext()
.getExecution();
vars = execution.getVariableNames();
final Set<String> variableNames = execution.getVariableNames();
if (variableNames != null) {
for (String variableName : variableNames) {
vars.add(variableName + "?");
}
}
} else if ("".equals(processVariables)) {
vars = Collections.emptyList();
} else {
Expand All @@ -61,10 +69,15 @@ private Object sendToInternal(String endpointUri,
.getBpmnExecutionContext().getExecution();
Map<String, Object> variablesToSend = new HashMap<String, Object>();
for (String var : variables) {
Object value = execution.getVariable(var);
if (value == null) {
throw new IllegalArgumentException("Process variable '" + var
+ "' no found!");
Object value;
if (var.endsWith("?")) {
value = execution.getVariable(var.substring(0, var.length() - 1));
} else {
value = execution.getVariable(var);
if (value == null) {
throw new IllegalArgumentException("Process variable '" + var
+ "' no found!");
}
}
variablesToSend.put(var, value);
}
Expand Down

0 comments on commit 87322c8

Please sign in to comment.