Skip to main content Skip to complementary content
  • New archived content: Talend MDM, Talend Data Catalog 8.0, and Talend 7.3 products reached their end of life in 2024. Their documentation was moved to the Talend Archive page and will no longer receive content updates.
Close announcements banner

Calling the MetaServlet REST Web service to execute a task

In the following scenario, a Job created in Talend Studio invokes the MetaServlet REST Web service to execute a specific task in Talend Administration Center.

Before you begin

Make sure an existing task is available in the Job Conductor page of Talend Administration Center.

Screenshot of the Job in the Designer.

Dropping and linking the components

Procedure

  1. Place the following components from the Palette onto the design workspace:
    • a tSetGlobalVar component,
    • a tREST component,
    • and a tLogRow component.
  2. Connect the tREST component to the tLogRow component using a Row > Main connection.
  3. Connect the tSetGlobalVar component to the tREST component using a Trigger > OnSubjobOK connection.

Configuring the context variables

You must define the variables that will be used to call the MetaServlet REST Web service.

Procedure

  1. In the Context view, click the [+] button four times to add four variables.
  2. Name these variables tac_url, tac_user, tac_pwd and task_id.
  3. In the Value field under the Default context, enter the variable values:
    • For the tac_url variable, type in the URL of the Talend Administration Center Web application, http://localhost:8080/org.talend.administrator for example.
    • For the tac_user variable, type in the administrator user name in Talend Administration Center Web application, admin@company.com for example.
    • For the tac_pwd variable, type in the administrator password in Talend Administration Center Web application, admin for example.
    • For the task_id variable, type in the ID of the task you want to generate, 1 for example.
  4. Press Ctrl+S to save your changes.

Configuring the Routine

You must define a user routine that encodes the MetaServlet parameters in base64 to be transmitted to the REST API. This routine will be called in the Job.

Procedure

  1. In the Repository tree view, expand Code to display the Routines folder.
  2. Right-click Routines and select Create routine.
  3. The New routine dialog box opens. Enter the information required to create the routine, then click Finish to proceed to the next step.

    The newly created routine appears in the Repository tree view, directly below the Routines node. The routine editor opens to reveal a model routine which contains a simple example, by default, comprising descriptive text in blue, followed by the corresponding code.

  4. At the beginning, right after the package routines line of code, add the following:

    import com.sun.org.apache.xml.internal.security.utils.Base64;

    To do so, start typing and press Ctrl+Space to open the list of templates, then select com.sun.org.apache.xml.internal.security.utils.*; then replace the * sign with Base64.

  5. Modify the model at the end with your own code:
    public static String base64Encode(String message) {
    
        	return message==null ? null : "" + Base64.encode(message.getBytes()).replace("\n", "");
        }
    public static void main(String[] args) {
        	String tmp = "{\"actionName\":\"runTask\",\"taskId\":\"1\",\"mode\":\"synchronous\",
    \"authPass\":\"talend\",\"authUser\":\"talend@talend.com\"}";
    
        	System.out.println("Base 64: " + base64Encode(tmp));
        }
    }

    This allows you to encode the MetaServlet action, runTask, in base64.

    For more information about the parameters and actions available in the MetaServlet, see Talend Administration Center MetaServlet API commands.

Configuring the components for calling the MetaServlet REST Web service to execute a task

Procedure

  1. Double-click the tSetGlobalVar component to display its Basic settings view in the Component tab.
  2. Click the [+] button to add a line in the Variables table:
    • In the Key field, type in jsonEncoded

    • In the Value fields, type in:

      MetaServlet.base64Encode("{\"actionName\":\"runTask\",\"taskId\":\"" + context.task_id + "\",\"mode\"
      :\"synchronous\",\"context\":{\"Default\":\"" + ((String)globalMap.get("tMsgBox_1_RESULT")) + "\"},
      \"authPass\":\"" + context.tac_pwd + "\",\"authUser\":\"" + context.tac_user + "\"}") 

      to call the routine you have previously created.

  3. Then double-click the tREST component to display its Basic settings view.
  4. Fill the URL field with the URL of the Web service you are going to invoke. For this use case, type in:
    context.tac_url + "/metaServlet?" +((String)globalMap.get("jsonEncoded"))

    to call the service and encode the MetaServlet parameters in a JSON format.

  5. From the HTTP Method list, select GET to send an HTTP request for generating a task.
    In this way, the MetaServlet is invoked using the REST API of Talend Administration Center with the relevant parameters.
  6. In the Basic settings view of the tLogRow component, select the Basic option to display the result in the Run console.
  7. Save your Job and press F6 to launch it.

Results

The console shows that the tREST component sends an HTTP request to the server end to run the specified task, and that the task has been executed without errors.

Screenshot of the console after successfully running the Job.

In the Job Conductor page of Talend Administration Center, the status of the task is ready to run.

Screenshot of the Job Conductor page.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!