Referencing context variables in a tJava component

One Star

Referencing context variables in a tJava component

I did not see an easier way to share context variables throughout a job including custom Java code. Please post if you have a better solution. I load my context variables from a properties file, but had trouble getting those values from inside my tJava component, so this is how I resolved it:
Insert a tJavaRow component between the tFileInputProperties file which reads the properties file, and the tContextLoad component which creates the context and loads it with values from the properties file.
In the tJavaRow component, the snippet below loads values by key to the global map for use later by other tJava components:
//sets output row for consumption by tContextLoad
output_row.key=input_row.key;
output_row.value=input_row.value;
//creates key/value pair for use later in Java components
globalMap.put(input_row.key, input_row.value);
I hope this helps someone
--mike.blizman@housolutions.net
Community Manager

Re: Referencing context variables in a tJava component

Hi
You have loaded the context value with tContextLoad component, and then you are able to use the context variable later in any other components. I don't think you need to put it into a global variable. For example:
tFileInputProperties--main--tContextload
|
OnSubjobOK
|
other subjob (such as tJava)
on Other subjob, you can use context variable and get its value by: context.varName; For example, a snippet code on tJava:
String name=context.name;
System.out.println(name);
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business