ZedBrannigan ZedBrannigan - 1 month ago 24x
Java Question

How to end tLoop when desired criteria reached

Here is my process:

Job Layout and tJava_1 configuration

You can see the Java code in tJava_1. It parses out the data from the file and if it reached the last search page it will set a value to true. This is meant to be the stop criteria for the tLoop_1, as shown here:

tLoop configuration

Am I handling the Talend variables wrong? How can I stop the loop from running once I reached the criteria in my Java code?


You don't need the "Declaration" or the "Iteration" values. What you want is a variable in the "Condition" that is a boolean that evaluates to true and is set to false when you are wanting to end the loop.

Best practice says you should use context variables for actual contexts supplied to a job either on run time or when starting a child job and then use the globalMap for globally available variables across a single job.

So in this case you should initialise a boolean globalMap variable set to true (you could do this in a tJava component with globalMap.put("loop",true) or use the tSetGlobalVar component) and then in your tJavaRow you can set the globalMap variable to false with globalMap.put("loop",false).

If you were to use "loop" as your globalMap variable then you'd use ((Boolean)globalMap.get("loop")) in your "Condition" in the tLoop.

A worked simple example of this might look like the following:

While loop job layout

First we set the loop variable to true as we want it to at least start!

tSetGlobalVar configuration

And then we set the condition of the while loop to the boolean global variable:

tLoop configuration

And then finally we run some code in the tJava component that does something and conditionally sets the global variable to false, ending the loop:

if (((Integer)globalMap.get("tLoop_2_CURRENT_ITERATION")) == 3) {
    globalMap.put("loop", false);
    } else {