execution.setVariable("var", 42);
Behind the scenes, the process engine performs some magic to squeeze numbers, booleans, or even complex Java objects into the database and return it from there on the next call:
int myLuckyNumber = (Integer) execution.getVariable("var");
With 7.2 (and the latest alpha), we introduce the typed value API. It is not a whole new API but rather of a gentle extension. Like before, the variable API is map-based and no existing code breaks. With the typed value API, performing the same task as above, it is possible to write
execution.setVariable("var", Variables.integerValue(42));
and
TypedValue typedValue = execution.getValueTyped("var");
You might have noticed the TypedValue which is the very core of this concept. Instead of the plain variable value, there is an intermediate object, the typed value.
What can you do with it? Assume there is a variable called “var” in four different process instances:
runtimeService.setVariable(processInstance1.getId(), "var", 42000); runtimeService.setVariable(processInstance2.getId(), "var", (short) 42); runtimeService.setVariable(processInstance3.getId(), "var", (long) 123123123); runtimeService.setVariable(processInstance4.getId(), "var", 745.0d);
The four variables all differ in type and exact value. Yet, all are numeric values. In order to find all process instances with variable value > 10, the following can be written:
runtimeService.createProcessInstanceQuery() .variableValueGreaterThan("var", 10).list();
Unfortunately, this returns only one process instance, processInstance1
. Why? Because 42 is an integer value and the query only searches for process instances with an integer value greater than 10.
Often, this is not what users want. All values are numeric and can be compared on a conceptual level. That is where typed values come to the rescue. Now, it is possible to write the following process instance query:
runtimeService.createProcessInstanceQuery() .variableValueGreaterThan("var", Variables.numberValue(10)).list();
This query returns all four process instances. The process engine detects that the types integer, short, long, and double are all subordinates of the type number. It now knows that you do not mean integers with value greater than 10 but any number value that is greater than 10.
Why should you care? Sometimes you don’t know the type a variable in question has. In Cockpit, users specify a numeric value to query with. Its exact type is of little interest. While some MyBatis and SQL hacks made this possible in Cockpit before 7.2, it is now a well-defined API feature. And the best: You can use it in your own queries, wherever variable values can be specified.
That is not all to it; the typed value API has much more to offer. To learn more about it, stay tuned for the release webinar, blog posts, tutorials, and examples to follow. Of course, you can right now have a look at the documentation and discover the typed value API.