Tuesday, June 11, 2013

Your Own Exception Handler for Standard Talend Components

Your Own Exception Handler for Standard Talend Components

Many Talend Open Studio (TOS) components will allow you to specify whether or not processing should stop when an error is encountered.  Some do not, and this can be problematic when an error case is to be expected.  This post describes a technique for adding a custom exception handler to TOS components missing the "Die on error" option.

Talend Open Studio (TOS) components like tMSSqlOutput allow you to specify whether an error is fatal -- causing processing to abort right away -- or non-fatal, allowing processing to continue.  This behavior is specified by the "Die on error" option.
Allow Non-Fatal Errors in a tMSSqlOutput
However, not all TOS components are built this way.  A failure in a tSSH will cause processing to stop, breaking out of any iterations.
Processing Stops During the First Iteration
The tForeach is configured with a list of three values.  The tJava_1 component prints out the current iteration value which is one of the three IPs.

tForeach with List of Three Values
The tSSH stops on the first iteration and does not continue with the remaining input.  This may not be desired.  For example, it may be normal for a host to be unreachable or a login to be bad.  Checking for such conditions may be the whole purpose of the job.  If this is the case, we need a way to continue processing.

Configuration of tSSH - Use Input as Target Host


Code Generation

When TOS generates Java classes that use an iteration connection, exceptions in components like tSSH break out of the iteration.  What we can do is work with the code generation to wrap a few Java keywords around the statements formed by tSSH.  This is done with a tJavaFlex.

Job Supporting Continuing
 In this job, the tJavaFlex provides start and end blocks that wraps around the code generated by the tSSH.  The code added to the tJavaFlex's start and end blocks is a try / catch which will prevent the exception generated by the SSH from leaving the iteration.

The Start and End Blocks Open a try / catch Block
The start and end blocks will add "try {" before the block of code that is generated by tSSH and a "} catch(Exception exc)" after.  This will catch any exceptions thrown by the tSSH (begin, end, main .javajets).

The result is a job that will continue processing over the entire input set.  This screenshot shows three tSSH attempts on the three input IP addresses.  All fail, but processing can continue.

Job That Performs tSSH on All Input (Continuing)
WARNING

If you use this in your jobs, you may find that it is incompatible one day.

It's a bad practice to code TOS jobs that rely on the operation of the code generator.  However, this is a solution that will work across multiple versions of TOS including the latest (5.1.0) and it is a clean way to work with tSSH or any other deficient (no "Die on error") components. 

tJavaFlex prepares start and end blocks that frame the main part of a tJavaFlex as well as downstream components.  This example allows a tSSH to continue processing in the event of an error.

No comments: