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
Creating a loop with tJava
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;