Search code examples
javaspringactiviti

Activiti User interface Spring App integration


I have the following Activiti 6 applications running from the official provided .WAR files. Have succesfully deployed these to my localhost

So far I can use activiti-app to produce BPMN files and start up applications using the interface. So far so good.

However what im looking to do is write my own Spring Apps but be able to view them running using the activiti UI apps.

So looking at the baeldung-activiti tutorial. You can start the application.

@GetMapping("/start-process")
public String startProcess() {
    runtimeService.startProcessInstanceByKey("my-process");
    return "Process started. Number of currently running process instances = " + runtimeService.createProcessInstanceQuery().count();
}

The above returns an incremented value everytime the endpoint is hit.

My questions is this.

Using the activiti tools (running on localhost:8008) how can view the processes. How do I link the standalone java application . (running on localhost:8081) with the Activiti ui interfaces?


Solution

  • That's pretty easy if you have the activity-rest configured and running. The REST API is documented here.

    So you just need to do a Web Service call to the correct API endpoint. For example to list all of the processes you need to do a GET request to the repository/process-definitions endpoint.

    Note: The Rest API uses Basic Auth.

    public void loadProcesses(){
        // the username and password to access the rest API (same as for UI)
        String plainCreds = "username:p@ssword";
        byte[] plainCredsBytes = plainCreds.getBytes();
        byte[] base64CredsBytes = Base64.getEncoder().encode(plainCredsBytes);
        String base64Creds = new String(base64CredsBytes);
    
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + base64Creds);
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> request = new HttpEntity<>(headers);
        ResponseEntity<String> responseAsJson = restTemplate.exchange("http://localhost:8080/activiti-rest/repository/process-definitions", HttpMethod.GET, request, String.class);
    }
    

    The response for the following API call will be JSON like

    {
      "data": [
      {
        "id" : "oneTaskProcess:1:4",
        "url" : "http://localhost:8182/repository/process-definitions/oneTaskProcess%3A1%3A4",
        "version" : 1,
        "key" : "oneTaskProcess",
        "category" : "Examples",
        "suspended" : false,
        "name" : "The One Task Process",
        "description" : "This is a process for testing purposes",
        "deploymentId" : "2",
        "deploymentUrl" : "http://localhost:8081/repository/deployments/2",
        "graphicalNotationDefined" : true,
        "resource" : "http://localhost:8182/repository/deployments/2/resources/testProcess.xml",
        "diagramResource" : "http://localhost:8182/repository/deployments/2/resources/testProcess.png",
        "startFormDefined" : false
      }
      ],
      "total": 1,
      "start": 0,
      "sort": "name",
      "order": "asc",
      "size": 1
     }