Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[resolved] The correct way to assign the value of context variables in Java

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
Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

Hi 
Question1 & Question2: We only use the following format to assign a new value to context variable. 
context.varName=value
You can assign a new value to context variable for every row or iteration, the variable always has the new value as long as you assign a new value.
Question3:  context.synchronizeContext() is only needed for tContextDump.
Best regards
Shong

View solution in original post

19 Replies
Anonymous
Not applicable
Author

Hi 
Question1 & Question2: We only use the following format to assign a new value to context variable. 
context.varName=value
You can assign a new value to context variable for every row or iteration, the variable always has the new value as long as you assign a new value.
Question3:  context.synchronizeContext() is only needed for tContextDump.
Best regards
Shong
Anonymous
Not applicable
Author

Shong,
Brilliant mate.  Thanks heaps for clearing that up.  I think I will change the subject of this post to make it easier for others to find.
Great work mate!
Thanks for your time,
Scott
Anonymous
Not applicable
Author

Has there been a change since v5.5? There are occasions when using ....
context.myVar  = value;
....does not work very well at all. I have found situations where the above format resulted in returning null when calling the context variable in a subsequent tJava (linked by a RunIf), but when I used this method....
context.setProperty("myVar", value);
....it worked.
If a fix has been put in for this, great. If not, it should not be hidden as it can cause hours of debugging issues.
Anonymous
Not applicable
Author

rhall,
Thanks for your feedback mate.  Are you able to provide details for steps to replicate the issue?
It may have been a timing issue, depending on the way the components were connected, although strange that setProperty would work in that case....
Regards,
Scott
Anonymous
Not applicable
Author

rhall,
I just did a search of the Talend Bugtracker for the phrase "context variable" and found the following issue:
https://jira.talendforge.org/browse/TDI-14419
Have a look and see if it was like your scenario mate.
The Talend response was:
" This is a job design problem. We upload an example to help you to achieve your need."
Regards,
Scott
Anonymous
Not applicable
Author

I don't believe it was a timing issue since a simple change from one method to the other fixed it. To be honest, I would have to work at an example that shows it (and I don't really have a great deal of time at the moment), but *believe* one of the times I have experienced it was when writing this  tutorial. The tutorial is not really related to this problem. I now make sure that I use the setProperty method to be on the safe side. This problem can also be experienced when using ESB.
Anonymous
Not applicable
Author

rhall,
Thanks again for your feedback mate.  
Ok so let's put it out there.  
Can anyone give us replication steps to demonstrate a scenario where using the direct assignment a context variable does not work, and using setProperty does?
Direct Assignment:
context.myVar  = "value";

Set Property:
context.setProperty("myVar", "value");

Let's work it out once and for all 😉
Regards,
Scott
Anonymous
Not applicable
Author

I use the tContextLoad and tContextDump components in the tutorial. I suspect it may be to do with the synchronize code used with those since I can't recall having the problem without making use of those components. Anyway, it is something to be aware of.
Anonymous
Not applicable
Author

rhall,
Ok thanks for that mate.  Yes once I read that:
context.synchronizeContext();

was required for tContextDump to show current values I suspected that this, due to the fact it is not documented anywhere, could be the cause of people having this perception that setProperty was required, as they would have, like I did, used tDumpContext to check what the values were at a certain point.  That is why I started this thread 😉
Regards,
Scott