How to implement tLoop in Talend?

我的梦境 提交于 2019-12-25 03:35:17

问题


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.


回答1:


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

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

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

then connect On Component Ok

Step 3: Create the tLoop

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

then iterate

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.

___________________________

SECTION 2 : TO Retry Failed Connections

___________________________

if you need to retry a DB connection.

add a tJavaFlex between tLoop1 and tJava2 like so

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; 


来源:https://stackoverflow.com/questions/53290472/how-to-implement-tloop-in-talend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!