A site about Talend
The tJavaFlex component is part of the Custom Code family of components. It allows the execution of arbitrary Java code, and bridges the functionality of tJava and tJavaRow.
tJavaFlex differs from tJava in that it allows both input and output connectors, row and iterate.
tJavaFlex also provides 3 code-blocks, as detailed below: -
This component offers some interesting possibilities.
This is based on the example shown in the official Talend documentation, that is, to use this component as a row generator.
We can clearly see from this example that Main code contains the body of a loop and that each iteration of the loop will generate an output row.
System.out.println("tJavaFlex_1: Start code"); for(int i = 1; i < 4; i++) {
row1.newColumn = "row " + i; System.out.println("tJavaFlex_1: Main code: i=" + i);
} System.out.println("tJavaFlex_1: End code");
In this example, we'll use tJavaFlex to catch a Java Exception.
All code that is generated by Talend, including components, has the ability to throw an Exception. We'll look at Exception handling strategies in a later tutorial; however, for now, I'll show how tJavaFlex can play an important part in this strategy.
This example shows that we have used tJavaFlex to catch, and ignore, an Exception that has been thrown by a tJava component.
This tJava component does no more than to throw an Exception. Note the use of 1==1
. This is to prevent the Java compiler objecting to this statement generating unreachable code.
System.out.println("In tJava_1"); if(1 == 1) throw new Exception();
Here, we set-up our Exception handler.
System.out.println("tJavaFlex_1: Start code"); try {
Although we have no code to execute here (other than our progress message), this example gives an important insight in to some key functionality of tJavaFlex. Our tJavaFlex component has been connected to tJava using the iterate connector. This means that the code that we entered in tJava will be encapsulated within the Main code section of tJavaFlex. In otherwords, the Exception that we have thrown is within our Exception handler. This only happens with the iterate connector.
System.out.println("tJavaFlex_1: Main code");
Here, we catch any Exception that has been thrown, write a message to the console window and then throw away the Exception. Note that our Job completed with [exit code=0]
.
} catch(Exception e) { System.out.println("tJavaFlex_1: Exception " + e.toString() + " has been caught"); } System.out.println("tJavaFlex_1: End code");
In this final example, we'll connect two tJavaFlex components together using Row->Data. This goes some way to further explain why this component contains three code-sections and how components interact with each other. This is a good starter, for understanding component writing.
The interesting observation from this example, is the execution order of the code-sections. All Begin sections of the SubJob are executed first, then the Main sections and then the End sections. You'll also notice that the Begin section of tJavaFlex_2 has been executed before that of tJavaFlex_1.
Now may be a good opportunity to take a look at the Code tab of the Job design workspace, to understand the code that has been generated, the significance of the sequencing and how the code generated may affect variable scope. We'll revisit this in a later article.
comments powered by Disqus