0

I created a BPMN Collaboration model with a Script-Task of type Javascript in it. Then I instantiated a process instance with a process variable like so:

Variable name: arr
Object type name: java.util.ArrayList
Serialization Data Format: application/x-java-serialized-object
Value: [{ "id": 10 }]

Then I passed the script task with the following code:

var arr = execution.getVariable("arr");
execution.setVariable("arr2", arr);

And it ran successfully. Then I amended the script task like this:

var arr = execution.getVariable("arr");
arr.add({ "id" : 2 });
execution.setVariable("arr2", arr);

And redeployed, re-instantiated; and to my surprise it resulted in an error:

Cannot submit task form xxx: Cannot serialize object in variable 'arr': jdk.nashorn.internal.scripts.JO4

Can someone please tell me what am I doing wrong?

[UPDATE]

I just found something new, if I modify the ArrayList like this:

var arr = execution.getVariable("arr");
arr.add(2);
execution.setVariable("arr2", arr);

It works just fine! And the modified value is:

[{ "id": 10 }, 2]

Which means setVariable has problem serializing the modified list. So how should I serialize it?

Mehran
  • 15,593
  • 27
  • 122
  • 221

1 Answers1

3

Basically this is the same the problem as described here. Instead of an array created in Javascript, we are dealing with an object here, which apparently Nashorn internally represents as an instance of the class jdk.nashorn.internal.scripts.JO4 and the process engine isn't able to serialize that (with JDK >= 8u40, a JO4 instance is wrapped in a ScriptObjectMirror instance when calling a Java method but the problem is the same).

To create an instance of a Java class, you can use the following code:

var HashMap = Java.type("java.util.HashMap");
var instance = new HashMap();
instance.id = 2;
arr.add(instance);
execution.setVariable("arr2", arr);

Source: Nashorn documentation

thorben
  • 1,577
  • 8
  • 14
  • I'm not sure if there exists one or not, but I think a documentation explain these would be really helpful. Thanks. – Mehran Oct 06 '15 at 12:50