|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +package com.microsoft.durabletask; |
| 4 | + |
| 5 | +import javax.annotation.Nullable; |
| 6 | + |
| 7 | +/** |
| 8 | + * Client for interacting with durable entities. |
| 9 | + * <p> |
| 10 | + * This class provides operations for signaling entities, querying entity metadata, |
| 11 | + * and performing entity storage maintenance. Instances are obtained from |
| 12 | + * {@link DurableTaskClient#getEntities()}. |
| 13 | + * <p> |
| 14 | + * This design mirrors the .NET SDK's {@code DurableEntityClient} which is accessed |
| 15 | + * via the {@code DurableTaskClient.Entities} property. |
| 16 | + */ |
| 17 | +public abstract class DurableEntityClient { |
| 18 | + |
| 19 | + private final String name; |
| 20 | + |
| 21 | + /** |
| 22 | + * Creates a new {@code DurableEntityClient} instance. |
| 23 | + * |
| 24 | + * @param name the name of the client |
| 25 | + */ |
| 26 | + protected DurableEntityClient(String name) { |
| 27 | + this.name = name; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Gets the name of this client. |
| 32 | + * |
| 33 | + * @return the client name |
| 34 | + */ |
| 35 | + public String getName() { |
| 36 | + return this.name; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Sends a signal to a durable entity instance without waiting for a response. |
| 41 | + * <p> |
| 42 | + * If the target entity does not exist, it will be created automatically when it receives the signal. |
| 43 | + * |
| 44 | + * @param entityId the target entity's instance ID |
| 45 | + * @param operationName the name of the operation to invoke on the entity |
| 46 | + */ |
| 47 | + public void signalEntity(EntityInstanceId entityId, String operationName) { |
| 48 | + this.signalEntity(entityId, operationName, null, null); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Sends a signal with input to a durable entity instance without waiting for a response. |
| 53 | + * <p> |
| 54 | + * If the target entity does not exist, it will be created automatically when it receives the signal. |
| 55 | + * |
| 56 | + * @param entityId the target entity's instance ID |
| 57 | + * @param operationName the name of the operation to invoke on the entity |
| 58 | + * @param input the serializable input for the operation, or {@code null} |
| 59 | + */ |
| 60 | + public void signalEntity(EntityInstanceId entityId, String operationName, @Nullable Object input) { |
| 61 | + this.signalEntity(entityId, operationName, input, null); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sends a signal with input and options to a durable entity instance without waiting for a response. |
| 66 | + * <p> |
| 67 | + * If the target entity does not exist, it will be created automatically when it receives the signal. |
| 68 | + * Use {@link SignalEntityOptions#setScheduledTime(java.time.Instant)} to schedule the signal for |
| 69 | + * delivery at a future time. |
| 70 | + * |
| 71 | + * @param entityId the target entity's instance ID |
| 72 | + * @param operationName the name of the operation to invoke on the entity |
| 73 | + * @param input the serializable input for the operation, or {@code null} |
| 74 | + * @param options additional options for the signal, or {@code null} |
| 75 | + */ |
| 76 | + public abstract void signalEntity( |
| 77 | + EntityInstanceId entityId, |
| 78 | + String operationName, |
| 79 | + @Nullable Object input, |
| 80 | + @Nullable SignalEntityOptions options); |
| 81 | + |
| 82 | + /** |
| 83 | + * Fetches the metadata for a durable entity instance, excluding its state. |
| 84 | + * |
| 85 | + * @param entityId the entity instance ID to query |
| 86 | + * @return the entity metadata, or {@code null} if the entity does not exist |
| 87 | + */ |
| 88 | + @Nullable |
| 89 | + public EntityMetadata getEntityMetadata(EntityInstanceId entityId) { |
| 90 | + return this.getEntityMetadata(entityId, false); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Fetches the metadata for a durable entity instance, optionally including its state. |
| 95 | + * |
| 96 | + * @param entityId the entity instance ID to query |
| 97 | + * @param includeState {@code true} to include the entity's serialized state in the result |
| 98 | + * @return the entity metadata, or {@code null} if the entity does not exist |
| 99 | + */ |
| 100 | + @Nullable |
| 101 | + public abstract EntityMetadata getEntityMetadata(EntityInstanceId entityId, boolean includeState); |
| 102 | + |
| 103 | + /** |
| 104 | + * Queries the durable store for entity instances matching the specified filter criteria. |
| 105 | + * |
| 106 | + * @param query the query filter criteria |
| 107 | + * @return the query result containing matching entities and an optional continuation token |
| 108 | + */ |
| 109 | + public abstract EntityQueryResult queryEntities(EntityQuery query); |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns an auto-paginating iterable over entity instances matching the specified filter criteria. |
| 113 | + * <p> |
| 114 | + * This method automatically handles pagination when iterating over results. It fetches pages |
| 115 | + * from the store on demand, making it convenient when you want to process all matching entities |
| 116 | + * without manually managing continuation tokens. |
| 117 | + * <p> |
| 118 | + * You can iterate over individual items: |
| 119 | + * <pre>{@code |
| 120 | + * for (EntityMetadata entity : client.getEntities().getAllEntities(query)) { |
| 121 | + * System.out.println(entity.getEntityInstanceId()); |
| 122 | + * } |
| 123 | + * }</pre> |
| 124 | + * <p> |
| 125 | + * Or iterate page by page for more control: |
| 126 | + * <pre>{@code |
| 127 | + * for (EntityQueryResult page : client.getEntities().getAllEntities(query).byPage()) { |
| 128 | + * for (EntityMetadata entity : page.getEntities()) { |
| 129 | + * System.out.println(entity.getEntityInstanceId()); |
| 130 | + * } |
| 131 | + * } |
| 132 | + * }</pre> |
| 133 | + * |
| 134 | + * @param query the query filter criteria |
| 135 | + * @return a pageable iterable over all matching entities |
| 136 | + */ |
| 137 | + public EntityQueryPageable getAllEntities(EntityQuery query) { |
| 138 | + return new EntityQueryPageable(query, this::queryEntities); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns an auto-paginating iterable over all entity instances. |
| 143 | + * <p> |
| 144 | + * This is a convenience overload equivalent to {@code getAllEntities(new EntityQuery())}. |
| 145 | + * |
| 146 | + * @return a pageable iterable over all entities |
| 147 | + */ |
| 148 | + public EntityQueryPageable getAllEntities() { |
| 149 | + return getAllEntities(new EntityQuery()); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Cleans up entity storage by removing empty entities and/or releasing orphaned locks. |
| 154 | + * <p> |
| 155 | + * This is an administrative operation that can be used to reclaim storage space and fix |
| 156 | + * entity state inconsistencies. |
| 157 | + * |
| 158 | + * @param request the clean storage request specifying what to clean |
| 159 | + * @return the result of the clean operation, including counts of removed entities and released locks |
| 160 | + */ |
| 161 | + public abstract CleanEntityStorageResult cleanEntityStorage(CleanEntityStorageRequest request); |
| 162 | +} |
0 commit comments