Skip to content

Creating a Polling Job

Create a Job that polls Formbird for new Documents and process them to an external system.

Items used

Type Name
Routines FormbirdRoutine
Components tInfiniteLoop, tJava, tRunJob

Overview

  1. Setup a Main Job (Job 1) as a poller that calls a Sub Job (Job 2)
  2. Setup a Sub Job (Job 2) that queries Formbird and sends any results to a Sub Job (Job 3)
  3. Setup a Sub Job (Job 3) that processes the results to an external system.

‚Äč 1. Repeater (Main Job) ==> 2. Formbird Query (Sub Job) ==> 3. Process to External System (Sub Job)

Setup Main Job

Create a new Job, and name it "ProcessUnsetCases". This Job provides the repeater mechanism to trigger the Sub Jobs.

  1. Setup your Job's Context Variables

  2. Select the Jobs Context tab, and create the Context Variables as defined in Contexts and Configuration Files section of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio.

  3. Add a tJava component and copy the following code to it:

java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent; logger.info(method);

  1. Add a long context variable named loopWait and set to 60000 (ie. 60 seconds).

  2. Add a tInfiniteLoop component and specify a wait time of context.loopWait.

  3. Add a tRunJob component and select a sub Job to run. Select Transmit whole context to pass context values to the sub Job. Deselect Die on child error.

  4. Add a tJava component and copy the following code to it:

java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent; logger.info(method + " onSubjobOk"); logger.info(method + " next run in (ms): " + context.loopWait);

  1. Connect the tJava component [step 2] to the tInfiniteLoop component [step 4] using an On Component Ok trigger.

  2. Connect the tInfiniteLoop component [step 4] to the tRunJob component [step 5] using an Iterate Row link.

  3. Connect the tRunJob component [step 5] to the tJava component [step 6] using an On Component Ok trigger.

Setup Formbird Query Sub Job

Create a new Job, and name it "ProcessUnsetCases_filter". This Job queries Formbird for Documents matching a given query.

  1. Setup your Job's Context Variables

  2. Select the Jobs Context tab, and create the Context Variables as defined in Contexts and Configuration Files section of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio.

  3. Add a boolean context variable named proceed and set to false.

  4. Add an Object context variable named documents.

  5. Add a tJava component to the Job, and copy the following code to it.

  6. Under the component's Advanced settings panel, add:

    ```java import java.util.List;

    import org.apache.log4j.Logger;

    import com.fieldtec.webclient.config.WebclientConfig; import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.service.FieldtecService; import com.fieldtec.webclient.service.impl.FieldtecServiceImpl; import com.fieldtec.webclient.util.UtilJson; ```

  7. Under the component's Basic settings panel, add:

    ```java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent;

    FieldtecService ftService = null;

    try { context.proceed = false; context.documents = null;

    // Initialise ftService (always synchronize context and then setup webclientConfig)
    context.synchronizeContext();
    FormbirdRoutine.setupWebClientConfig(context);
    ftService = new FieldtecServiceImpl();
    
      String query = "{\"query\":{\"bool\":{\"must\":["
        + " {\"term\":{\"appTags\":\"case\"}}"
        + ",{\"term\":{\"sent\": false}}"
        + ",{\"term\":{\"" + FormbirdRoutine.KEY_SYSTEMHEADER_SYSTEM_TYPE + "\":\"" + FormbirdRoutine.SYSTEM_TYPE_DOCUMENT + "\"}}"
        + "]}}}";
    
    List<Document> foundDocuments = ftService.findAll(query);
    if (foundDocuments != null) {
        int size = foundDocuments.size();
        logger.info(method + " size: " + size);
    
        context.documents = foundDocuments;
        context.proceed = true;
    }
    

    } finally { if (ftService != null) { ftService.close(); } } ```

  8. Add a tRunJob component and select a sub Job to run. Select Transmit whole context to pass context values to the sub Job.

  9. Connect the tJava component [step 4 to the tRunJob component [step 5] using a Run if trigger.

Setup Process Sub Job

Create a new Job, and name it "ProcessUnsetCases_action". For the purposes of this example, this Job outputs the results (ie. Documents found) to the log. You can adjust this to suit your own processing requirements, for example: transform and send the results to an external system.

  1. Setup your Job's Context Variables

  2. Select the Jobs Context tab, and create the Context Variables as defined in Contexts and Configuration Files section of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio.

  3. Add an Object context variable named documents.

  4. Add a tJava component to the Job, and copy the following code to it.

  5. Under the component's Advanced settings panel, add:

    ```java import java.util.Iterator; import java.util.List;

    import org.apache.log4j.Logger;

    import com.fieldtec.webclient.config.WebclientConfig; import com.fieldtec.webclient.model.Document; import com.fieldtec.webclient.service.FieldtecService; import com.fieldtec.webclient.service.impl.FieldtecServiceImpl; import com.fieldtec.webclient.util.UtilJson; ```

  6. Under the component's Basic settings panel, add:

    ```java Logger logger = Logger.getLogger(context.loggerName); String method = jobName + "." + currentComponent;

    FieldtecService ftService = null; logger.info(method + " ZZZSub");

    try { // Initialise ftService (always synchronize context and then setup webclientConfig) context.synchronizeContext(); FormbirdRoutine.setupWebClientConfig(context); ftService = new FieldtecServiceImpl();

    List<Document> foundDocuments = (List<Document>) context.documents;
    if (foundDocuments != null) {
        int size = foundDocuments.size();
        logger.info(method + " size: " + size);
    
        Iterator<Document> iterator = foundDocuments.iterator();
        while(iterator.hasNext()) {
            Object object = iterator.next();
            if (object instanceof Document) {
                  Document doc = (Document) object;
    
                  // Log the complete document
                logger.info(UtilJson.prettyPrint(doc));
    
                  // Always remove systemHeader and _id before updating an existing document
                doc.remove(KEY_SYSTEMHEADER);
                doc.remove(KEY__ID);
    
                  // Set sent flag on Document to true.
                  doc.setValue("sent", true);
    
                // Update document
                Document responseDocument = ftService.update(doc);
    
                  // Log the response document
                logger.info(UtilJson.prettyPrint(responseDocument));
            }
        }
    } else {
        logger.info(method + "foundDocuments is null");
    }
    

    } finally { if (ftService != null) { ftService.close(); } } ```

Run the Main Job

  1. Return to the Main Job.
  2. Navigate to the Job's Run tab, and select Run. Make sure your Formbird URLs and credentials are correct and point to a running Formbird environment.

Congratulations! You have created a Job that polls Formbird and logs the results.