Interface Context
- All Known Subinterfaces:
ObjectContext
,SharedObjectContext
,SharedWorkflowContext
,WorkflowContext
All methods of this interface, and related interfaces, throws either TerminalException
or AbortedExecutionException
, where the former can be caught and acted upon, while the
latter MUST NOT be caught, but simply propagated for clean up purposes.
NOTE: This interface MUST NOT be accessed concurrently since it can lead to different orderings of user actions, corrupting the execution of the invocation.
-
Method Summary
Modifier and TypeMethodDescription<T> Awakeable
<T> Create anAwakeable
, addressable throughAwakeable.id()
.Create a newAwakeableHandle
for the provided identifier.default Awaitable
<byte[]> Likecall(Target, Serde, Serde, Object)
with raw input/output.<T,
R> Awaitable <R> Invoke another Restate service method.random()
request()
default void
run
(ThrowingRunnable runnable) Likerun(String, ThrowingRunnable)
, but without a name.default void
run
(RetryPolicy retryPolicy, ThrowingRunnable runnable) Likerun(ThrowingRunnable)
, but using a custom retry policy.default <T> T
run
(Serde<T> serde, ThrowingSupplier<T> action) Likerun(String, Serde, ThrowingSupplier)
, but without a name.default <T> T
run
(Serde<T> serde, RetryPolicy retryPolicy, ThrowingSupplier<T> action) Likerun(Serde, ThrowingSupplier)
, but using a custom retry policy.default void
run
(String name, ThrowingRunnable runnable) Likerun(String, Serde, ThrowingSupplier)
, but without returning a value.default void
run
(String name, RetryPolicy retryPolicy, ThrowingRunnable runnable) Likerun(String, ThrowingRunnable)
, but using a custom retry policy.default <T> T
run
(String name, Serde<T> serde, ThrowingSupplier<T> action) Execute a non-deterministic closure, recording the result value in the journal.<T> T
run
(String name, Serde<T> serde, RetryPolicy retryPolicy, ThrowingSupplier<T> action) Likerun(String, Serde, ThrowingSupplier)
, but using a custom retry policy.default void
Likesend(Target, Serde, Object)
with bytes input.default void
Likesend(Target, Serde, Object, Duration)
with bytes input.<T> void
Invoke another Restate service without waiting for the response.<T> void
Invoke another Restate service without waiting for the response after the provideddelay
has elapsed.default void
Causes the current execution of the function invocation to sleep for the given duration.Causes the start of a timer for the given duration.
-
Method Details
-
request
Request request() -
call
Invoke another Restate service method.- Parameters:
target
- the address of the calleeinputSerde
- Input serdeoutputSerde
- Output serdeparameter
- the invocation request parameter.- Returns:
- an
Awaitable
that wraps the Restate service method result.
-
call
Likecall(Target, Serde, Serde, Object)
with raw input/output. -
send
Invoke another Restate service without waiting for the response.- Parameters:
target
- the address of the calleeinputSerde
- Input serdeparameter
- the invocation request parameter.
-
send
Likesend(Target, Serde, Object)
with bytes input. -
send
Invoke another Restate service without waiting for the response after the provideddelay
has elapsed.This method returns immediately, as the timer is executed and awaited on Restate.
- Parameters:
target
- the address of the calleeinputSerde
- Input serdeparameter
- the invocation request parameter.delay
- time to wait before executing the call.
-
send
Likesend(Target, Serde, Object, Duration)
with bytes input. -
sleep
Causes the current execution of the function invocation to sleep for the given duration.- Parameters:
duration
- for which to sleep.
-
timer
Causes the start of a timer for the given duration. You can await on the timer end by invokingAwaitable.await()
.- Parameters:
duration
- for which to sleep.
-
run
Execute a non-deterministic closure, recording the result value in the journal. The result value will be re-played in case of re-invocation (e.g. because of failure recovery or suspension point) without re-executing the closure. Use this feature if you want to perform non-deterministic operations.You can name this closure using the
name
parameter. This name will be available in the observability tools.The closure should tolerate retries, that is Restate might re-execute the closure multiple times until it records a result. You can control and limit the amount of retries using
run(String, Serde, RetryPolicy, ThrowingSupplier)
.Error handling: Errors occurring within this closure won't be propagated to the caller, unless they are
TerminalException
. Consider the following code:
To propagate run failures to the call-site, make sure to wrap them in// Bad usage of try-catch outside the run try { ctx.run(() -> { throw new IllegalStateException(); }); } catch (IllegalStateException e) { // This will never be executed, // but the error will be retried by Restate, // following the invocation retry policy. } // Good usage of try-catch outside the run try { ctx.run(() -> { throw new TerminalException("my error"); }); } catch (TerminalException e) { // This is invoked }
TerminalException
.- Type Parameters:
T
- type of the return value.- Parameters:
name
- name of the side effect.serde
- the type tag of the return value, used to serialize/deserialize it.action
- closure to execute.- Returns:
- value of the run operation.
- Throws:
TerminalException
-
run
<T> T run(String name, Serde<T> serde, RetryPolicy retryPolicy, ThrowingSupplier<T> action) throws TerminalException Likerun(String, Serde, ThrowingSupplier)
, but using a custom retry policy.When a retry policy is not specified, the
run
will be retried using the Restate invoker retry policy, which by default retries indefinitely.- Throws:
TerminalException
- See Also:
-
run
default void run(String name, RetryPolicy retryPolicy, ThrowingRunnable runnable) throws TerminalException Likerun(String, ThrowingRunnable)
, but using a custom retry policy.When a retry policy is not specified, the
run
will be retried using the Restate invoker retry policy, which by default retries indefinitely.- Throws:
TerminalException
- See Also:
-
run
default <T> T run(Serde<T> serde, RetryPolicy retryPolicy, ThrowingSupplier<T> action) throws TerminalException Likerun(Serde, ThrowingSupplier)
, but using a custom retry policy.When a retry policy is not specified, the
run
will be retried using the Restate invoker retry policy, which by default retries indefinitely.- Throws:
TerminalException
- See Also:
-
run
Likerun(ThrowingRunnable)
, but using a custom retry policy.When a retry policy is not specified, the
run
will be retried using the Restate invoker retry policy, which by default retries indefinitely.- Throws:
TerminalException
- See Also:
-
run
Likerun(String, Serde, ThrowingSupplier)
, but without returning a value.- Throws:
TerminalException
-
run
Likerun(String, Serde, ThrowingSupplier)
, but without a name.- Throws:
TerminalException
-
run
Likerun(String, ThrowingRunnable)
, but without a name.- Throws:
TerminalException
-
awakeable
Create anAwakeable
, addressable throughAwakeable.id()
.You can use this feature to implement external asynchronous systems interactions, for example you can send a Kafka record including the
Awakeable.id()
, and then let another service consume from Kafka the responses of given external system interaction by usingawakeableHandle(String)
. -
awakeableHandle
Create a newAwakeableHandle
for the provided identifier. You can use it toAwakeableHandle.resolve(Serde, Object)
orAwakeableHandle.reject(String)
the linkedAwakeable
.- See Also:
-
random
RestateRandom random()- See Also:
-