Defining a variable accessible from multiple Jobs

author
Shicong Hong
EnrichVersion
6.4
6.3
6.2
6.1
6.0
EnrichProdName
Talend Open Studio for Big Data
Talend Data Fabric
Talend Real-Time Big Data Platform
Talend Data Services Platform
Talend Open Studio for ESB
Talend Big Data Platform
Talend Big Data
Talend ESB
Talend Open Studio for MDM
Talend MDM Platform
Talend Open Studio for Data Integration
Talend Data Integration
Talend Data Management Platform
task
Design and Development > Designing Jobs
EnrichPlatform
Talend Studio

Creating a routine and declaring a static variable

In this example, you will create a routine with a static variable and add the setter/getter methods to make the variable accessible from multiple Jobs.

Procedure

  1. In the Repository tree view, expand Code to display the routines folder.
  2. Right-click Routines and select Create routine to create a new routine.
  3. Name the new routine MyRoutine.
  4. Declare a static variable in the routine and set its value as null, for example:
    private static String name = null;
                        
  5. Add the setter/getter methods for this variable in the routine and press Ctrl+S to save the routine.
    
    public static synchronized void setValue(String message) {
            name=message;
        }
                                    
    public static synchronized String getValue() {
            return name;
        }
                                
    Note: The complete Java code for this routine is as below:
    package routines;
    /*
    * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
    * it must be before the "{talendTypes}" key.
    *
    * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
    * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
    * Short
    *
    * 3. {Category} define a category for the Function. it is required. its value is user-defined .
    *
    * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
    *
    * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
    * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
    * added. you can have many parameters for the Function.
    *
    * 5. {example} gives a example for the Function. it is optional.
    */
    public class MyRoutine {
        private static String name = null;
        /**
        * helloExample: not return value, only print "hello" + message.
        *
        *
        * {talendTypes} String
        *
        * {Category} User Defined
        *
        * {param} string("world") input: The string need to be printed.
        *
        * {example} helloExemple("world") # hello world !.
        */
        public static void helloExample(String message) {
            if (message == null) {
                message = "World"; //$NON-NLS-1$
            }
            System.out.println("Hello " + message + " !"); //$NON-NLS-1$ //$NON-NLS-2$
         }
                                        
         public static synchronized void setValue(String message) {
             name=message;
         }
                                        
         public static synchronized String getValue() {
             return name;
        }
     }

Setting up the child Jobs

The example below shows how to share a value between different Jobs via the routine defined earlier.

Procedure

  1. Create a Job named childJob1, and add two components by typing their names on the design workspace or dropping them from the Palette to the design workspace:
    • A tFixedFlowInput to generate an input data flow
    • A tJavaRow to receive the data and in which the Job calls the setter method to give a new value to the variable
  2. Double-click the tFixedFlowInput component to open its Basic settings view.
  3. Click the [...] button next to Edit schema to open the [Schema] dialog box and define the schema of the input data. In this example, the schema has only one column name of the string type.
  4. In the Mode area, select Use Single Table option, and define the corresponding value for the message column in the Values table. In this example, the value is "Talend".
    Note: The tJava component is calling the getter method and assigning the return value to a string variable, then printing the variable value in the console.
  5. Double-click the tJavaRow component to open its Basic settings view.
  6. In the Code area, enter the following code to add the setter method.
    MyRoutine.setValue(input_row.name);
  7. Create a Job called childJob2 and then create a tJava component in this Job.
  8. Double-click the tJava component to open its Basic settings view.
  9. In the Code area, enter the following code to add the setter method.
    String name=MyRoutine.getValue();
    System.out.println(name);

Setting up the parent Job

Procedure

  1. Create a Job named parentJob and add two tRunJob components.
  2. Connect the second tRunjob component to a first tRunjob component with a OnSubjobOk link.
  3. Double-click on both tRunJob component to open its Basic settings view.
  4. Fill the Job field of tRunJob1 and tRunJob2 respectively with childJob1 and childJob2 to call the two child Jobs created.
    This approach of defining a variable in routine and sharing it across different Jobs does not work with the Use an independent process to run subjob feature on the tRunJobcomponent.

Executing the Job

Procedure

Execute the parentJob. You can see the following results in the console:
Starting job parentJob at 19:52 21/06/2013.
                    
[statistics] connecting to socket on port 3397
[statistics] connected
Talend
[statistics] disconnected
Job parentJob ended at 19:52 21/06/2013. [exit code=0]