Tctl workflow show equivalent in Java

Hi,

Is it possible to retrieve the information available using tctl workflow show command using Java API?

Kind Regards,
Jay.

tctl wf show -w <workflowId> -r <runid>

You can use WorkflowExecutionHistory, for example:

public static void printWorkflowExecutionHistory(WorkflowClient client, String workflowId, String runId, WorkflowStub wfStub, ByteString token) {
        if(wfStub == null) {
            wfStub = client.newUntypedWorkflowStub(workflowId, Optional.of(runId), Optional.empty());
        }

        GetWorkflowExecutionHistoryRequest workflowExecutionHistoryRequest;
        if(token == null) {
            workflowExecutionHistoryRequest =
                    GetWorkflowExecutionHistoryRequest.newBuilder()
                            .setNamespace(client.getOptions().getNamespace())
                            .setExecution(wfStub.getExecution())
                            .build();
        } else {
            workflowExecutionHistoryRequest =
                    GetWorkflowExecutionHistoryRequest.newBuilder()
                            .setNamespace(client.getOptions().getNamespace())
                            .setExecution(wfStub.getExecution())
                            .setNextPageToken(token)
                            .build();
        }

        GetWorkflowExecutionHistoryResponse workflowExecutionHistoryResponse =
                service.blockingStub().getWorkflowExecutionHistory(workflowExecutionHistoryRequest);
        for(HistoryEvent historyEvent : workflowExecutionHistoryResponse.getHistory().getEventsList()) {
            System.out.println(historyEvent.getEventId() + " - " + historyEvent.getEventType());
        }
        if(workflowExecutionHistoryResponse.getNextPageToken() != null && workflowExecutionHistoryResponse.getNextPageToken().size() > 0) {
            printWorkflowExecutionHistory(client, workflowId, runId, wfStub, workflowExecutionHistoryResponse.getNextPageToken());
        }
    }

The initial call to this method would pass null for wfstub and token, for example:

printWorkflowExecutionHistory(client, myWfId, myWfRunId, null, null);

1 Like

Don’t forget that history is paginated. So you have to call getWorkflowExecutionHistory more than once if the page token in the response is not emptyh.

Thanks @tihomir, @maxim.

This is really useful.

If I wanted the equivalent of tctl wf describe command in Java, would I use DescribeWorkflowExecutionRequest and DescribeWorkflowExecutionResponse classes?

Kind Regards,
Jay.

@maxim - is it possible to get all the data in one go without pagination?

Thanks,
Jay.

is it possible to get all the data in one go without pagination?

Very good point by @maxim, thank you. Here is the updated version that also works for large wf histories (using tokens):

    public static void printWorkflowExecutionHistory(WorkflowClient client, String workflowId, String runId, WorkflowStub wfStub, ByteString token) {
        if(wfStub == null) {
            wfStub = client.newUntypedWorkflowStub(workflowId, Optional.of(runId), Optional.empty());
        }

        GetWorkflowExecutionHistoryRequest workflowExecutionHistoryRequest;
        if(token == null) {
            workflowExecutionHistoryRequest =
                    GetWorkflowExecutionHistoryRequest.newBuilder()
                            .setNamespace(client.getOptions().getNamespace())
                            .setExecution(wfStub.getExecution())
                            .build();
        } else {
            workflowExecutionHistoryRequest =
                    GetWorkflowExecutionHistoryRequest.newBuilder()
                            .setNamespace(client.getOptions().getNamespace())
                            .setExecution(wfStub.getExecution())
                            .setNextPageToken(token)
                            .build();
        }

        GetWorkflowExecutionHistoryResponse workflowExecutionHistoryResponse =
                service.blockingStub().getWorkflowExecutionHistory(workflowExecutionHistoryRequest);
        for(HistoryEvent historyEvent : workflowExecutionHistoryResponse.getHistory().getEventsList()) {
            System.out.println(historyEvent.getEventId() + " - " + historyEvent.getEventType());
        }
        if(workflowExecutionHistoryResponse.getNextPageToken() != null && workflowExecutionHistoryResponse.getNextPageToken().size() > 0) {
            printWorkflowExecutionHistory(client, workflowId, runId, wfStub, workflowExecutionHistoryResponse.getNextPageToken());
        }
    }

The initial call to this method would pass null for wfstub and token, for example:

printWorkflowExecutionHistory(client, myWfId, myWfRunId, null, null);

Note, I have updated the initial response (one marked as solution) to include this updated code.

1 Like

You use both, request is the request sent to server, and response includes the response info. Here is a method that you could use:

public static void printDescribeWorkflowExecution(WorkflowClient client, String workflowId, String runId) {
        WorkflowStub wfStub = client.newUntypedWorkflowStub(workflowId, Optional.of(runId), Optional.empty());
        DescribeWorkflowExecutionRequest request = DescribeWorkflowExecutionRequest.newBuilder()
                .setNamespace(client.getOptions().getNamespace())
                .setExecution(wfStub.getExecution()).build();
        DescribeWorkflowExecutionResponse response = service.blockingStub().describeWorkflowExecution(request);
        // response includes info like workflow exec info, pending activities (and their count),
        // pending child wfs, etc
    }
1 Like