Hi there,
I can't see anywhere in the documentation here in any of the 4.4.x pages where it covers setting context variables from java code.
https://help.talend.com/search/all?query=Using+contexts+and+variables&content-lang=en
I have read it on this forum being said that setting context variables using tJava/tJavaRow using the following syntax is only temporary:
context.TestVar = "Test Value";
"
The location context.<var_name> is not permanent and it just holds the value for you to access it easily."
And that you should instead use the following syntax to assign values to context variables from java:
context.setProperty("TestVar ", "Test Value");
I have also read other posts like this one:
https://community.talend.com/t5/Design-and-Development/set-variable-in-context-with-tJava-fails-when...
Saying:
"
...context values are stored in more than one place and tContextDump picks up only those values defined when the job begins so if you set context values dynamically using tJava you need to follow with the following statement so that tContextDump will use the new values:"
context.synchronizeContext();
I can't see this topic discussed anywhere in the Talend documentation.
context.setProperty is a pain to use because it only accepts strings. So if you want to assigned values to context variables of any other type from tJava or tJavaRow you need to convert them. Especially painful for dates.
So can we please get an official word from Talend on this? (or anyone that can point out some documentation that gives a clear answer to the following questions)
QUESTION 1:
Has there been some confusion here in the developer community due to the undocumented need to call
context.synchronizeContext() to get updated context variable values to show in tContextDump that has made people thing that the use of context.setProperty is needed for the assignment of context variables and that values set in java are
not permanent?
Excluding the obvious nature of tJavaRow (eg; variables assigned in tJavaRow get overwritten for every iteration), is there an issue with just using:
context.TestDateVar = TalendDate.getCurrentDate();
to assign a variable?
QUESTION 2:
Is there a difference between using:
context.TestVar = "Test Value";
and
context.setProperty("TestVar ", "Test Value");
in regard to the scope and
permanency of the value assigned?
QUESTION 3:
If you are not using tContextDump but only using updated context variable values in other components, do you need to use
context.synchronizeContext(); to ensure these values will be available, or is this only needed for tContextDump?
-------------------------------------------------
For what it's worth, the Talend Open Studio Cookbook has a page on: "Setting the context and globalMap variables
using tJava". In their examples they set the value of context variables using the following:
context.testValue ="testValue is now initialized";
And make no mention of an issue with scope or
permanency of the value assigned.
https://www.packtpub.com/big-data-and-business-intelligence/talend-open-studio-cookbook
Thank you for your time,
Scott