How to implement tLoop in Talend?

2019-08-25 09:48发布

I'm new to Talend and need an example job to implement tLoop. I want to run a job 10 times if it fails. I've looked at the documents, but I can't seem to figure this out.

标签: talend
1条回答
Rolldiameter
2楼-- · 2019-08-25 10:15

This answer has 2 sections

  1. Creating a loop with tJava

  2. Retying a failed connection to a data source 5 times (with adding tJavaFlex)

___________________________________

SECTION 1 : Creating a loop with tJava

-----------------------------------------------------------

I just write a tJava component and then iterate to false. Like this

Step 1: create a context variable

enter image description here

Step 2: write some java code in tJava (tJava1)

// setting loop flag
context.continueLooping = true;
//log.info("Starting job...");

then connect On Component Ok

enter image description here

Step 3: Create the tLoop

in the loop condition put your context context.continueLooping which should be true by the first iteration.

enter image description here

then iterate

enter image description here

to the next tJava (tJava2)

if ( ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 1)
{
   // code

}
else if(((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 2) 
{
   // code

}
else if (((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 3)
{
   // code

}
else if (((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 4)
{
   // code

}
else if (((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) == 5)
{
   // code

   context.continueLooping = false;
  // log.info("DONE");
}

else 
{
   context.continueLooping = false;
  // log.error("out of bounds...");

} 

this tJava runs different code for each iteration till it reaches 5 I use this area to count stuff and load value to other contexts and more.

Then it runs the nest part n times till the context value is set to false.

enter image description here

___________________________

SECTION 2 : TO Retry Failed Connections

___________________________

if you need to retry a DB connection.

add a tJavaFlex between tLoop1 and tJava2 like so

enter image description here

and add the following code in the 3 sections Start:

// start part of your Java code
try{   

Main:

// here is the main part of the component,
// a piece of code executed in the row
// loop
if ( ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) > 1)
{
Thread.sleep(10000);
}

End:

// end of the component, outside/closing the loop
}catch (Exception e) {

if ( ((Integer)globalMap.get("tLoop_1_CURRENT_ITERATION")) > 5)
{
context.continueLooping = false;
}
else
{
System.out.println("Connection failed. Retrying...next");
}


}      

and add On Component Ok tJava with the code to stop looping on the success (tJava3)

context.continueLooping = false; 
查看更多
登录 后发表回答