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
- Setup a Main Job (Job 1) as a poller that calls a Sub Job (Job 2)
- Setup a Sub Job (Job 2) that queries Formbird and sends any results to a Sub Job (Job 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.
-
Setup your Job's
Context Variables -
Select the Jobs Context tab, and create the Context Variables as defined in
Contexts and Configuration Filessection of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio. -
Add a
tJavacomponent and copy the following code to it:
java
Logger logger = Logger.getLogger(context.loggerName);
String method = jobName + "." + currentComponent;
logger.info(method);
-
Add a long context variable named
loopWaitand set to 60000 (ie. 60 seconds). -
Add a
tInfiniteLoopcomponent and specify a wait time ofcontext.loopWait. -
Add a
tRunJobcomponent and select a sub Job to run. SelectTransmit whole contextto pass context values to the sub Job. DeselectDie on child error. -
Add a
tJavacomponent 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);
-
Connect the
tJavacomponent [step 2] to thetInfiniteLoopcomponent [step 4] using anOn Component Oktrigger. -
Connect the
tInfiniteLoopcomponent [step 4] to thetRunJobcomponent [step 5] using anIterateRow link. -
Connect the
tRunJobcomponent [step 5] to thetJavacomponent [step 6] using anOn Component Oktrigger.
Setup Formbird Query Sub Job
Create a new Job, and name it "ProcessUnsetCases_filter". This Job queries Formbird for Documents matching a given query.
-
Setup your Job's
Context Variables -
Select the Jobs Context tab, and create the Context Variables as defined in
Contexts and Configuration Filessection of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio. -
Add a boolean context variable named
proceedand set to false. -
Add an Object context variable named
documents. -
Add a
tJavacomponent to theJob, and copy the following code to it. -
Under the component's
Advanced settingspanel, 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; ```
-
Under the component's
Basic settingspanel, 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(); } } ```
-
Add a
tRunJobcomponent and select a sub Job to run. SelectTransmit whole contextto pass context values to the sub Job. -
Connect the
tJavacomponent [step 4 to thetRunJobcomponent [step 5] using aRun iftrigger.
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.
-
Setup your Job's
Context Variables -
Select the Jobs Context tab, and create the Context Variables as defined in
Contexts and Configuration Filessection of this documentation. Specify your actual Formbird URLs and credentials during setup in order to run the Job within Talend Studio. -
Add an Object context variable named
documents. -
Add a
tJavacomponent to theJob, and copy the following code to it. -
Under the component's
Advanced settingspanel, 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; ```
-
Under the component's
Basic settingspanel, 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
- Return to the Main Job.
- 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.