在Camunda的脚本任务中调试Javascript [英] Debugging a Javascript within Camunda's Script-Task

查看:402
本文介绍了在Camunda的脚本任务中调试Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Camunda Process 中的 Javascript 类型的 Script Task 中合并两个数组>.这是我的脚本:

I'm trying to merge two arrays in a Script Task of type Javascript within a Process in Camunda. Here's my script:

var arr1 = execution.getVariableTyped("arr1", true);
var arr2 = execution.getVariableTyped("arr2", true);

var merged = [];

for (var i1 in arr1) {
    var found = false;
    for (var i2 in merged) {
        if (arr1[i1].id == merged[i2].id) {
            found = true;
            break;
        }
    }
    if (!found) {
        merged.push(arr1[i1]);
    }
}

for (var i1 in arr2) {
    var found = false;
    for (var i2 in merged) {
        if (arr2[i1].id == merged[i2].id) {
            found = true;
            break;
        }
    }
    if (!found) {
        merged.push(arr2[i1]);
    }
}

execution.setVariable("arr1", merged);
execution.removeVariable("arr2");

执行上述脚本时,将引发异常:

When the mentioned script is executed, it throws an exception:

无法完成任务b4fb856a-6a92-11e5-9774-005056c00008:无法完成 序列化变量'arr1'中的对象:SPIN/JACKSON-JSON-01009无法 将对象'jdk.nashorn.internal.objects.NativeArray@5ff42b74'映射到json 节点

Cannot complete task b4fb856a-6a92-11e5-9774-005056c00008: Cannot serialize object in variable 'arr1': SPIN/JACKSON-JSON-01009 Unable to map object 'jdk.nashorn.internal.objects.NativeArray@5ff42b74' to json node

那是为什么?有什么问题,我该如何解决?反正有没有调试这样的脚本?

Why is that? What's the problem and how can I fix it? Is there anyway to debug such scripts?

推荐答案

Nashorn对于Javascript数组具有类似NativeArray的类,因为Javascript数组未绑定到像Java中的String[]这样的元素类型.因此,Nashorn创建NativeArray的实例.显然,流程引擎无法存储NativeArray的实例,因为它没有实现java.io.Serializable,并且也无法通过JSON和XML序列化器进行序列化.实际上,JSON序列化程序会尝试这样做,但是会抛出您看到的异常.

Nashorn has classes like NativeArray for Javascript arrays because a Javascript array is not tied to an element type like for example String[] in Java. Thus, Nashorn creates an instance of NativeArray. Apparently, the process engine is not able to store instances of NativeArray since it does not implement java.io.Serializable and is not serializable by the JSON and XML serializer either. In fact, the JSON serializer attempts to do so but throws the exception you see.

你可以做

execution.setVariable("arr1", Java.to(merged, "java.lang.Object[]"));

NativeArray转换为Java Object[].如果要从Java代码访问该数组,则可以使用更具体类型的数组.来源: Nashorn文档

to convert the NativeArray to a Java Object[]. If you want to access the array from Java code, you could use a more specifically typed array. Source: Nashorn documentation

注意:

对于 JDK 8版本> = 1.8u40 ,移交的类型不是NativeArray,而是包装了NativeArrayScriptObjectMirror实例(请参见

For JDK 8 versions >= 1.8u40, the type handed over is not NativeArray but an instance of ScriptObjectMirror that wraps a NativeArray (see this question for details). Apparently, the same code can be used to solve the issue.

这篇关于在Camunda的脚本任务中调试Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆