Running tasks with Java

You can run a digital worker task by using Java™.

Before you begin

See Running digital worker tasks to know how to get the access token from UMS.

About this task

In the following command lines, replace <task_integration_details> with the URL of the task that you want to run, for example:
https://9.20.193.65:30600/api/v1/tasks/5d9ae9b4db85c90049d2f877/runs

To find this URL, open your list of tasks in Digital Worker. Next to the name of the task, click the overflow menu and select Integration details.

You can get the integration details and run a task only when this task is activated.

Certificate validation: The following examples of commands skip the certificate validation, but you can adapt these commands to work with your own certificate.

Procedure

  1. Trigger the run by executing the following code:
    public static String runtask() throws Exception {
        String data = "";
        URL url = new URL("<task_integration_details>");
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
      
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Authorization",  "Bearer <accessToken>");
        con.setDoOutput(true);
        con.getOutputStream().write(data.getBytes("UTF-8"));
    
    
        InputStream inputStream = null;
        if (con.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
            inputStream = con.getInputStream();
        } else {
            inputStream = con.getErrorStream();
        }
    
    
        return new String(IOUtils.readFully(inputStream, Integer.MAX_VALUE, false), StandardCharsets.UTF_8);
    }

    After executing, you get a response that contains the runId corresponding to the ID of the run and a runURL corresponding to the path that is used to get the result of the run.

    runUrl corresponds to <task_integration_details>/<run_ID>.

  2. Get the runId from the POST query, for example by executing the runtask function. You can use the following code:
    String runTriggerOutput = runtask().replace("\"", "");
    String[] arr = runTriggerOutput.substring(1, runTriggerOutput.length()-1).split(",");
    String runId = arr[0].split(":")[1];
  3. You can use this function in a loop to fetch the run result in a separate thread:
    public static Pair<Integer, String> getresult(String runId) throws Exception {
        URL url = new URL("<task_integration_details>/" + runId);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    
    
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Authorization", "Bearer <accessToken>");
    
    
        BufferedReader br;
        if (con.getResponseCode() == 200) {
            br = new BufferedReader(new BufferedReader(new InputStreamReader((con.getInputStream()))));
        } else {
            br = new BufferedReader(new BufferedReader(new InputStreamReader((con.getErrorStream()))));
        }
    
    
        String buffer;
        StringBuilder sb = new StringBuilder();
        while ((buffer = br.readLine()) != null) {
            sb.append(buffer);
        }
    
    
        return new Pair<>(con.getResponseCode(), sb.toString());
    }
    Note:

    This function returns a Pair in which the key is the request returns code and the value is the content of the request.