Search code examples
talend

Talend: Update global variable in subjob


My simplified job look like this:

tSetGlobalVar--->(onSubJobOK)--->tRunJob--->(onSubJobOK)--->tJava

  • tSetGlobalVar will define a global variable and its initial value let's say: myKey:"firstValue"
  • tRunJob will run a sub job which contain only a second tSetGlobalVar component that's supposed to set a new value to the global variable defined in the master job: ((String)globalMap.get("myKey")): "newValue" also tried this: "myKey": "newValue"
  • tJava used just to debug, it's code is like below: System.out.println(((String)globalMap.get("myKey")));

Actual output: firstValue

Expected output: newValue

Is there any other way to modify the value of a global variable in a subjob and get the updated value in the master job ?


Solution

  • In Talend you can pass context variables to subjobs and they work like standard Java variables, so if you pass something unmodifiable (like Strings or basic types) you won't get any change back, but if you pass "by reference" types you will get your object changed and you will see the changes made by the subjob as the father job still holds the reference to the changed object.

    So the solution to your problem is to simply pass the globalMap from the Father job to the child job, so the child can read and change that map and the father job will see any change. The map you pass will be a different map than the globalMap of the child job and will be used only where you use it.

    Unfortunately Talend have only one "by reference" type: the Object type, so you will need to pass anything as object anc cast it back where you will use it, but I think that is not such a big problem.

    Here are the images for the job: Running the father Father job with executin results

    Passing the map to the child enter image description here

    The child retrieves the map from the contex variable and uses the map enter image description here