1+ // Copyright (c) Microsoft Corporation. All rights reserved.
2+ // Licensed under the MIT License.
3+ package com .microsoft .durabletask ;
4+
5+ import javax .annotation .Nonnull ;
6+ import javax .annotation .Nullable ;
7+ import java .util .List ;
8+
9+ /**
10+ * Feature for interacting with durable entities from an orchestration.
11+ * <p>
12+ * This mirrors the .NET SDK's {@code TaskOrchestrationContext.Entities} shape, adapted to Java.
13+ */
14+ public abstract class TaskOrchestrationEntityFeature {
15+
16+ /**
17+ * Calls an operation on an entity and waits for it to complete.
18+ *
19+ * @param entityId the target entity
20+ * @param operationName the name of the operation
21+ * @param input the operation input, or {@code null}
22+ * @param returnType the expected return type
23+ * @param <TResult> the result type
24+ * @return a task that completes with the operation result
25+ */
26+ public abstract <TResult > Task <TResult > callEntity (
27+ @ Nonnull EntityInstanceId entityId ,
28+ @ Nonnull String operationName ,
29+ @ Nullable Object input ,
30+ @ Nonnull Class <TResult > returnType );
31+
32+ /**
33+ * Calls an operation on an entity and waits for it to complete, with options.
34+ *
35+ * @param entityId the target entity
36+ * @param operationName the name of the operation
37+ * @param input the operation input, or {@code null}
38+ * @param returnType the expected return type
39+ * @param options the call options, or {@code null}
40+ * @param <TResult> the result type
41+ * @return a task that completes with the operation result
42+ */
43+ public abstract <TResult > Task <TResult > callEntity (
44+ @ Nonnull EntityInstanceId entityId ,
45+ @ Nonnull String operationName ,
46+ @ Nullable Object input ,
47+ @ Nonnull Class <TResult > returnType ,
48+ @ Nullable CallEntityOptions options );
49+
50+ /**
51+ * Calls an operation on an entity and waits for it to complete.
52+ *
53+ * @param entityId the target entity
54+ * @param operationName the name of the operation
55+ * @param <TResult> the result type
56+ * @return a task that completes with the operation result
57+ */
58+ public <TResult > Task <TResult > callEntity (
59+ @ Nonnull EntityInstanceId entityId ,
60+ @ Nonnull String operationName ,
61+ @ Nonnull Class <TResult > returnType ) {
62+ return this .callEntity (entityId , operationName , null , returnType , null );
63+ }
64+
65+ /**
66+ * Signals an entity operation without waiting for completion.
67+ *
68+ * @param entityId the target entity
69+ * @param operationName the operation name
70+ * @param input the operation input, or {@code null}
71+ * @param options signal options, or {@code null}
72+ */
73+ public abstract void signalEntity (
74+ @ Nonnull EntityInstanceId entityId ,
75+ @ Nonnull String operationName ,
76+ @ Nullable Object input ,
77+ @ Nullable SignalEntityOptions options );
78+
79+ /**
80+ * Signals an entity operation without waiting for completion.
81+ *
82+ * @param entityId the target entity
83+ * @param operationName the operation name
84+ */
85+ public void signalEntity (@ Nonnull EntityInstanceId entityId , @ Nonnull String operationName ) {
86+ this .signalEntity (entityId , operationName , null , null );
87+ }
88+
89+ /**
90+ * Signals an entity operation without waiting for completion.
91+ *
92+ * @param entityId the target entity
93+ * @param operationName the operation name
94+ * @param input the operation input, or {@code null}
95+ */
96+ public void signalEntity (
97+ @ Nonnull EntityInstanceId entityId ,
98+ @ Nonnull String operationName ,
99+ @ Nullable Object input ) {
100+ this .signalEntity (entityId , operationName , input , null );
101+ }
102+
103+ /**
104+ * Acquires one or more entity locks.
105+ *
106+ * @param entityIds the entity IDs to lock
107+ * @return a task that completes with an {@link AutoCloseable} used to release locks
108+ */
109+ public abstract Task <AutoCloseable > lockEntities (@ Nonnull List <EntityInstanceId > entityIds );
110+
111+ /**
112+ * Acquires one or more entity locks.
113+ *
114+ * @param entityIds the entity IDs to lock
115+ * @return a task that completes with an {@link AutoCloseable} used to release locks
116+ */
117+ public abstract Task <AutoCloseable > lockEntities (@ Nonnull EntityInstanceId ... entityIds );
118+
119+ /**
120+ * Gets whether this orchestration is currently inside a critical section.
121+ *
122+ * @return {@code true} if inside a critical section, otherwise {@code false}
123+ */
124+ public abstract boolean isInCriticalSection ();
125+
126+ /**
127+ * Gets the currently locked entity IDs.
128+ *
129+ * @return the list of currently locked entities, or an empty list
130+ */
131+ public abstract List <EntityInstanceId > getLockedEntities ();
132+ }
133+
134+ final class ContextBackedTaskOrchestrationEntityFeature extends TaskOrchestrationEntityFeature {
135+ private final TaskOrchestrationContext context ;
136+
137+ ContextBackedTaskOrchestrationEntityFeature (TaskOrchestrationContext context ) {
138+ this .context = context ;
139+ }
140+
141+ @ Override
142+ public <TResult > Task <TResult > callEntity (
143+ @ Nonnull EntityInstanceId entityId ,
144+ @ Nonnull String operationName ,
145+ @ Nullable Object input ,
146+ @ Nonnull Class <TResult > returnType ) {
147+ return this .context .callEntity (entityId , operationName , input , returnType );
148+ }
149+
150+ @ Override
151+ public <TResult > Task <TResult > callEntity (
152+ @ Nonnull EntityInstanceId entityId ,
153+ @ Nonnull String operationName ,
154+ @ Nullable Object input ,
155+ @ Nonnull Class <TResult > returnType ,
156+ @ Nullable CallEntityOptions options ) {
157+ return this .context .callEntity (entityId , operationName , input , returnType , options );
158+ }
159+
160+ @ Override
161+ public void signalEntity (
162+ @ Nonnull EntityInstanceId entityId ,
163+ @ Nonnull String operationName ,
164+ @ Nullable Object input ,
165+ @ Nullable SignalEntityOptions options ) {
166+ this .context .signalEntity (entityId , operationName , input , options );
167+ }
168+
169+ @ Override
170+ public Task <AutoCloseable > lockEntities (@ Nonnull List <EntityInstanceId > entityIds ) {
171+ return this .context .lockEntities (entityIds );
172+ }
173+
174+ @ Override
175+ public Task <AutoCloseable > lockEntities (@ Nonnull EntityInstanceId ... entityIds ) {
176+ return this .context .lockEntities (entityIds );
177+ }
178+
179+ @ Override
180+ public boolean isInCriticalSection () {
181+ return this .context .isInCriticalSection ();
182+ }
183+
184+ @ Override
185+ public List <EntityInstanceId > getLockedEntities () {
186+ return this .context .getLockedEntities ();
187+ }
188+ }
0 commit comments