Scipt optimization

Hello everyone,

I’m working on a big project using Camunda as the bpm engine and me and my team are currently trying to optimize the script usage in the task listeners of the bpm’s user task activities.

I have a doubt to be clarified regarding the external script usage and I’ll be exposing here a sample scenario to ask you if it is possible to do it and how.

Scenario:

We deploy the bpmn process along with some scripts, let’s say the deployment files are:

  • test.bpmn
  • scriptA.js
  • scriptB.js

I want to know if it is possible to have a user task activity using a single task listener with an inline script which will call scriptA and scriptB (inside this single inline script).

If not possible, is it possible to use scriptA as external script and call scriptB inside scriptA? How?

I’m looking forward to hearing your opinion, thank you guys.

Why do you want to optimize this way? From a performance side it will very likely have no notable difference.

If you still want to do this, you can use Nashorn’s load function:

load(“classpath:someFolder/myscriptjs”)

See nashorn docs for more details.

Hello @StephenOTT ,

thank you for your reply. The ideia is to make it easier to maintain the scripts while reusing some functions in different activities of the bpmn process. We want to make the bpmn file detached from the script while avoiding to repeat code in the different scripts we call in the activities.

I’ve tried your solution but I believe I’m doing something wrong since it’s not working. Here’s the experiment I’ve made, please let me know what I’m doing wrong:

1 - In a task listener I’m using the external resource to run the main script:

scriptteste

2 - Here is the main script (script.js) content:

load('classpath:increment.js')

var number = 1;

var result =
increment(number);

task.setVariable("finalNumber", result);

3 - The second script (increment.js) content:

function increment(number) {
    number=number+1;
    return number;
}

4 - I’m getting this error:

ENGINE-03051 There was an exception while invoking the TaskListener. Message: 'Unable to evaluate script while executing activity 'Activity_1h9gm7z' in the process definition with id 'Process_1nhcy3w:1:88784980-7132-11eb-a3f0-0242ac110002':TypeError: Cannot load script from classpath:increment.js in <eval> at line number 1'

Thank you.

See Load script from external resource error - #2 by StephenOTT. You need to ensure your scripts are in the lib folder

@StephenOTT,

The camunda engine hosted in ours client’s environment, the only way we can comunicate with the engine is via Rest, so our js files are deployed through deployment/create Rest endpoint, we can’t put them wherever we want. Is there any way we can do it in this scenario?

Take a look at: ProcessProjectTemplate/helpers.md at master · StephenOTT/ProcessProjectTemplate · GitHub

But if you are loading from the DB it would be better if you made these optimizations before you deploy. otherwise the loading and parsing must occur on every execution of your parent script. If you optimize before deployment then you can benefit from compiled script caching (which you cannot benefit from if you are loading dynamic inner scripts on every execution)