From 87322c859fc581b43b1ceaabc618c5abe70203b6 Mon Sep 17 00:00:00 2001 From: Stephan Pelikan Date: Tue, 21 Nov 2017 13:26:56 +0100 Subject: [PATCH] Issue 36: Introduce optional variables --- README.md | 4 +++- .../camel/common/CamelServiceCommonImpl.java | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 104cf3b..4746b00 100644 --- a/README.md +++ b/README.md @@ -21,13 +21,15 @@ Use the following expression in a ServiceTask to send all the process instance v ``` ${camel.sendTo('')} ``` +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('', '')} ``` - +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: ``` diff --git a/camunda-bpm-camel-common/src/main/java/org/camunda/bpm/camel/common/CamelServiceCommonImpl.java b/camunda-bpm-camel-common/src/main/java/org/camunda/bpm/camel/common/CamelServiceCommonImpl.java index 48acbe3..4d57f29 100644 --- a/camunda-bpm-camel-common/src/main/java/org/camunda/bpm/camel/common/CamelServiceCommonImpl.java +++ b/camunda-bpm-camel-common/src/main/java/org/camunda/bpm/camel/common/CamelServiceCommonImpl.java @@ -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; @@ -44,9 +46,15 @@ public Object sendTo(String endpointUri, String processVariables, String correlationId) { Collection vars; if (processVariables == null) { + vars = new LinkedList(); ActivityExecution execution = Context.getBpmnExecutionContext() .getExecution(); - vars = execution.getVariableNames(); + final Set variableNames = execution.getVariableNames(); + if (variableNames != null) { + for (String variableName : variableNames) { + vars.add(variableName + "?"); + } + } } else if ("".equals(processVariables)) { vars = Collections.emptyList(); } else { @@ -61,10 +69,15 @@ private Object sendToInternal(String endpointUri, .getBpmnExecutionContext().getExecution(); Map variablesToSend = new HashMap(); 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); }