Contexts and Configuration Files
Talend provides a centralized mechanism for you to define and manage configurable variables in a Job
. It is referred to as a Job's Contexts
. They can be used to:
- set configuration values that would otherwise be hard-coded in a Job's components/code;
- share data between Job components within a Job where the flow of data between components may not naturally exist (eg.
Component Row
connections) or where it is undesirable to hard-code values withinComponent Trigger
connections (eg. withinRun If
snippets). - share data between Parent and Sub Jobs
- share data between Jobs and Code Routines
- inject values into a Job on startup (via a Job's deployed configuration file). This allows the same Job to be deployed to different environments, without any code changes.
Setup Job Contexts
Start with the following context variables in a Job, and add more as required.
name | type | default | description/purpose |
---|---|---|---|
authUrl | String | Formbird URL to authenticate against. For example: https://yoursubdomain.formbird.com/auth/login/local |
|
webserviceUrl | String | Formbird webservice URL. For example: https://yoursubdomain.formbird.com/api |
|
username | String | Formbird account name | |
password | String | Formbird account password | |
loggerName | String | Formbird logger name to use. Refer to \Runtime_ESBSE\container\etc\org.ops4j.pax.logging.cfg for available options |
|
runMode | boolean | false | A flag to identify whether a job is running within the Talend Studio (false) or running within the Runtime_ESBSE (true). Deployed configuration file must have this value set to true. This flag is helpful when it is necessary to perform different logic when running within the Studio vs Runtime_ESBSE. |
Using Job Contexts inside a Job
To get or set context variables within component fields and code snippets, prefix the variable name with context.
For example:
// Get myValue
String myValue = context.myValue;
// Set myValue
context.myValue = myValue;
Expanding the scope of Job Contexts
By default, a Contexts scope is limited to the Job it is defined in, however, it can be expanded through to any Sub Jobs and Code Routines called by a Parent Job, allowing it to become a very useful mechanism to pass data around.
Passing Context to Sub Jobs
Add a tRunJob
component to your Job and select the Sub Job to call. Under the tRunJob
component's Basic settings
, select the Transmit whole context
check box. This will pass the context to the Sub Job.
Passing Contexts to Code Routines
In order to access a Jobs Context from a Routine, first synchronize it, and then pass it as a parameter when calling the routine method.
Example Component Code calling a Routine:
// Initialise ftService (always synchronize context before passing it to Code Routine)
context.synchronizeContext();
FieldtecRoutine.setupWebClientConfig(context);
ftService = new FieldtecServiceImpl();
Example Routine method:
public static void setupWebClientConfig(java.util.Properties context) {
String method = "setupWebClientConfig";
logger.info(method + " setup webClientConfig");
WebclientConfig wc = WebclientConfig.getInstance();
String authUrl = (String) context.get("authUrl");
String webserviceUrl = (String) context.get("webserviceUrl");
String username = (String) context.get("username");
String password = (String) context.get("password");
String loggerName = (String) context.get("loggerName");
wc.setAuthUrl(authUrl);
wc.setWebserviceUrl(webserviceUrl);
wc.setUsername(username);
wc.setPassword(password);
wc.setLoggerName(loggerName);
}
Using Job Contexts outside a Job
There are occasions where you may want to modify a context value outside a Job, for example, in a Sub Job or Code Routine, and have it available in the Parent Job. This is only possible when working with a Context variable of type Object
. Keep this in mind when defining your Context variables and how you wish to interact with them.
Using configuration files
Job Contexts are also externally configurable. When deploying a Job with an identically named configuration file, Talend will implicitly set the Context values in the job with those provided in the configuration file. This allows you to set (or override default) values to cater for different deployment environments (eg. Development, Test, Production).
-
Text based configuration file with a file extension of
cfg
. -
Must be the same name as the Parent Job (or Service) in order for Talend to recognize and implicitly apply it for the Job, for example, if the Job name is HelloWorld, then the accompanying configuration will be
HelloWorld.cfg
. -
Configuration files are placed in the
etc
folder of the runtime container, ie.
C:\TalendStudio-621\Runtime_ESBSE\container\etc\
- Sample configuration file contents:
authUrl = https://yoursubdomain.formbird.com/auth/login/local
webserviceUrl = https://yoursubdomain.formbird.com/api
username = yourname@yourcompany.com
password = yourpassword
loggerName = yourloggername
runMode = true