-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOperationHandler.java
More file actions
64 lines (60 loc) · 3.01 KB
/
OperationHandler.java
File metadata and controls
64 lines (60 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package io.nexusrpc.handler;
import io.nexusrpc.OperationException;
import org.jspecify.annotations.Nullable;
/**
* Handler for an operation.
*
* <p>An instance of this must be returned from an {@link OperationImpl} annotated method in a
* {@link ServiceImpl}. Simple synchronous operations can use {@link #sync}, more complex or
* asynchronous operations can manually implement this interface.
*
* @param <T> The parameter type of the operation. This can be {@link Void} for no parameter.
* @param <R> the return type of the operation. This can be {@link Void} for no return.
*/
public interface OperationHandler<T, R> {
/**
* Create an operation handler for a synchronous operation backed by the given function. This
* function will be called for each operation invocation. All other calls to the operation are not
* supported.
*/
static <T, R> OperationHandler<T, R> sync(SynchronousOperationFunction<T, R> func) {
return new SynchronousOperationHandler<>(func);
}
/**
* Handle the start of an operation.
*
* <p>The operation can be synchronous or asynchronous. Synchronous operations can return the
* result via {@link OperationStartResult#sync}. Asynchronous operations can return the started
* operation token via {@link OperationStartResult#async}.
*
* @param context Context for the call.
* @param details Details for the call.
* @param param Parameter for the operation. This may be null if the parameter was not given.
* @return The start result which is a synchronous value or an operation token representing an
* asynchronously running operation.
* @throws OperationException If thrown, can have failure details and state such as saying the
* operation was cancelled.
* @throws HandlerException Unexpected failures while running the handler.
* @throws RuntimeException Any other exception, will be converted to an {@link HandlerException}
* of type {@link HandlerException.ErrorType#INTERNAL}.
*/
OperationStartResult<R> start(
OperationContext context, OperationStartDetails details, @Nullable T param)
throws OperationException, HandlerException;
/**
* Cancel the asynchronously started operation.
*
* <p>This does not need to wait for cancellation to be processed, simply that cancellation is
* delivered. Duplicate cancellation requests for an operation or cancellation requests for an
* operation not running should just be ignored.
*
* @param context Context for the call.
* @param details Details for the call including the operation token.
* @throws HandlerException Unexpected failures while running the handler. This should be thrown
* with a type of {@link HandlerException.ErrorType#NOT_FOUND} if the operation token is not
* found.
* @throws RuntimeException Any other exception, will be converted to an {@link HandlerException}
* of type {@link HandlerException.ErrorType#INTERNAL}.
*/
void cancel(OperationContext context, OperationCancelDetails details) throws HandlerException;
}