• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #120

pending completion
#120

push

github-actions

web-flow
Rename sdk-features to features (#1599)

16189 of 20022 relevant lines covered (80.86%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/temporal-sdk/src/main/java/io/temporal/client/ListWorkflowExecutionIterator.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5
 *
6
 * Modifications copyright (C) 2017 Uber Technologies, Inc.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.client;
22

23
import com.google.protobuf.ByteString;
24
import io.temporal.api.workflow.v1.WorkflowExecutionInfo;
25
import io.temporal.api.workflowservice.v1.ListWorkflowExecutionsRequest;
26
import io.temporal.api.workflowservice.v1.ListWorkflowExecutionsResponse;
27
import io.temporal.internal.client.external.GenericWorkflowClient;
28
import java.util.Iterator;
29
import java.util.NoSuchElementException;
30
import java.util.Objects;
31
import java.util.concurrent.CompletableFuture;
32
import java.util.concurrent.ExecutionException;
33
import javax.annotation.Nonnull;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

37
class ListWorkflowExecutionIterator implements Iterator<WorkflowExecutionInfo> {
38
  private static final Logger log = LoggerFactory.getLogger(ListWorkflowExecutionIterator.class);
×
39

40
  private final @Nonnull String query;
41
  private final @Nonnull String namespace;
42
  private final int pageSize;
43
  private final @Nonnull GenericWorkflowClient genericClient;
44
  private ListWorkflowExecutionsResponse activeResponse;
45
  private int nextActiveResponseIndex;
46
  private CompletableFuture<ListWorkflowExecutionsResponse> nextResponse;
47

48
  ListWorkflowExecutionIterator(
49
      @Nonnull String query,
50
      @Nonnull String namespace,
51
      int pageSize,
52
      @Nonnull GenericWorkflowClient genericClient) {
×
53
    this.query = Objects.requireNonNull(query, "query");
×
54
    this.namespace = Objects.requireNonNull(namespace, "namespace");
×
55
    this.pageSize = pageSize;
×
56
    this.genericClient = Objects.requireNonNull(genericClient, "genericClient");
×
57
  }
×
58

59
  @Override
60
  public boolean hasNext() {
61
    if (nextActiveResponseIndex < activeResponse.getExecutionsCount()) {
×
62
      return true;
×
63
    }
64
    fetch();
×
65
    return nextActiveResponseIndex < activeResponse.getExecutionsCount();
×
66
  }
67

68
  @Override
69
  public WorkflowExecutionInfo next() {
70
    if (hasNext()) {
×
71
      return activeResponse.getExecutions(nextActiveResponseIndex++);
×
72
    } else {
73
      throw new NoSuchElementException();
×
74
    }
75
  }
76

77
  void fetch() {
78
    if (nextResponse == null) {
×
79
      // if nextResponse is null, it's the end of the iteration through the pages
80
      return;
×
81
    }
82

83
    ListWorkflowExecutionsResponse response;
84
    try {
85
      response = this.nextResponse.get();
×
86
    } catch (InterruptedException e) {
×
87
      Thread.currentThread().interrupt();
×
88
      throw new RuntimeException(e);
×
89
    } catch (ExecutionException e) {
×
90
      throw new RuntimeException(e.getCause());
×
91
    }
×
92

93
    ByteString nextPageToken = response.getNextPageToken();
×
94
    if (!nextPageToken.isEmpty()) {
×
95
      ListWorkflowExecutionsRequest request =
96
          ListWorkflowExecutionsRequest.newBuilder()
×
97
              .setNamespace(namespace)
×
98
              .setQuery(query)
×
99
              .setPageSize(pageSize)
×
100
              .setNextPageToken(nextPageToken)
×
101
              .build();
×
102
      this.nextResponse = genericClient.listWorkflowExecutionsAsync(request);
×
103
    } else {
×
104
      this.nextResponse = null;
×
105
    }
106

107
    if (response.getExecutionsCount() == 0 && nextResponse != null) {
×
108
      log.warn(
×
109
          "[BUG] listWorkflowExecutions received an empty collection with a non-empty nextPageToken");
110
      // shouldn't be happening, but we want to tolerate it if it does, so we just effectively
111
      // skip the empty response and wait for the next one in a blocking manner.
112
      // If this actually ever happens as a normal scenario, this skipping should be reworked to
113
      // be done asynchronously on a completion of nextResponse future.
114
      fetch();
×
115
      return;
×
116
    }
117

118
    activeResponse = response;
×
119
    nextActiveResponseIndex = 0;
×
120
  }
×
121

122
  public void init() {
123
    ListWorkflowExecutionsRequest request =
124
        ListWorkflowExecutionsRequest.newBuilder()
×
125
            .setNamespace(namespace)
×
126
            .setQuery(query)
×
127
            .setPageSize(pageSize)
×
128
            .build();
×
129
    nextResponse = new CompletableFuture<>();
×
130
    nextResponse.complete(genericClient.listWorkflowExecutions(request));
×
131
    fetch();
×
132
  }
×
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc