1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /***************************************************************************************
- * IBM Confidential
- *
- * OCO Source Materials
- *
- * IBM Cognos Products: Moser
- *
- * (C) Copyright IBM Corp. 2020
- *
- * The source code for this program is not published or otherwise
- * divested of its trade secrets, irrespective of what has been
- * deposited with the U.S. Copyright Office.
- *
- ***************************************************************************************/
- package com.ibm.bi.platform.modeling.sdk.examples;
- import com.ibm.bi.platform.modeling.sdk.examples.internal.ModelingHelper;
- import com.ibm.bi.platform.moser.core.tasks.TaskState;
- import com.ibm.json.java.JSONObject;
- /**
- * Abstract class providing an ability to poll for asynchronous tasks execution
- *
- */
- public abstract class AsynchronousTasksRunner extends ModelingHelper {
- /**
- * @param origin
- */
- public AsynchronousTasksRunner(String origin) {
- super(origin);
- }
-
- /**
- * Poll for execution status of asynchronous task
- *
- * @param taskJson
- * json string representing asynchronous task, e.g.
- * {"href":"\/tasks\/task11588762627068","taskID":"task11588762627068"}
- * @return final response object after task execution is complete
- */
- protected JSONObject executeAsychronousTask(String taskJson) {
- try {
- JSONObject task = JSONObject.parse(taskJson);
- String asynchronousTaskId = task.get("taskID").toString();
- // start polling
- String url = getOrigin() + getTasksURL() + asynchronousTaskId;
- JSONObject response = stringToJson(executeGetRequest(url, Integer.valueOf(200), null));
- while (response != null && inProgress(response.get("state").toString())) {
- try {
- Thread.sleep(3000);
- } catch (Exception e) {
- }
-
- response = stringToJson(executeGetRequest(url, Integer.valueOf(200), null));
- }
- return response;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * @return url to obtain asynchronous task status
- */
- public abstract String getTasksURL();
- /**
- * @param state
- * asynchronous task state see <code>TaskState</code>
- * @return true if task is not finished
- */
- private static boolean inProgress(String state) {
- if(state == null){
- return false;
- }
- switch (state) {
- case TaskState.EXECUTING:
- case TaskState.PENDING:
- case TaskState.NOT_CANCELLABLE:
- return true;
- default:
- return false;
- }
- }
- }
|