-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
288 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package building_blocks; | ||
|
||
import dev.restate.sdk.Context; | ||
import dev.restate.sdk.JsonSerdes; | ||
import dev.restate.sdk.annotation.Handler; | ||
import dev.restate.sdk.annotation.Service; | ||
import dev.restate.sdk.http.vertx.RestateHttpEndpointBuilder; | ||
import utils.SubscriptionService; | ||
|
||
import java.time.Duration; | ||
import java.util.UUID; | ||
|
||
import static utils.ExampleStubs.chargeBankAccount; | ||
|
||
/* | ||
* RESTATE's DURABLE BUILDING BLOCKS | ||
* | ||
* Restate turns familiar programming constructs into recoverable, distributed building blocks. | ||
* They get persisted in Restate, survive failures, and can be revived on another process. | ||
* | ||
* No more need for retry/recovery logic, K/V stores, workflow orchestrators, | ||
* scheduler services, message queues, ... | ||
* | ||
* The run handler below shows a catalog of these building blocks. | ||
* Look at the other examples in this project to see how to use them in examples. | ||
*/ | ||
|
||
@Service | ||
public class MyService { | ||
|
||
// This handler can be called over HTTP at http://restate:8080/myService/handlerName | ||
// Use the context to access Restate's durable building blocks | ||
@Handler | ||
public void run(Context ctx) throws Exception { | ||
// 1. IDEMPOTENCY: Add an idempotency key to the header of your requests | ||
// Restate deduplicates calls automatically. Nothing to do here. | ||
|
||
// 2. DURABLE RPC: Call other services without manual retry and deduplication logic | ||
// Restate persists all requests and ensures execution till completion | ||
String result = SubscriptionServiceClient.fromContext(ctx, "my-sub-123").create("my-request").await(); | ||
|
||
// 3. DURABLE MESSAGING: send (delayed) messages to other services without deploying a message broker | ||
// Restate persists the timers and triggers execution | ||
SubscriptionServiceClient.fromContext(ctx, "my-sub-123").send().create("my-request"); | ||
|
||
// 4. DURABLE PROMISES: tracked by Restate, can be moved between processes and survive failures | ||
// Awakeables: block the workflow until notified by another handler | ||
var awakeable = ctx.awakeable(JsonSerdes.STRING); | ||
// Wait on the promise | ||
// If the process crashes while waiting, Restate will recover the promise somewhere else | ||
String greeting = awakeable.await(); | ||
// Another process can resolve the awakeable via its ID | ||
ctx.awakeableHandle(awakeable.id()).resolve(JsonSerdes.STRING, "hello"); | ||
|
||
// 5. DURABLE TIMERS: sleep or wait for a timeout, tracked by Restate and recoverable | ||
// When this runs on FaaS, the handler suspends and the timer is tracked by Restate | ||
// Example of durable recoverable sleep | ||
// If the service crashes two seconds later, Restate will invoke it after another 3 seconds | ||
ctx.sleep(Duration.ofSeconds(5)); | ||
// Example of waiting on a promise (call/awakeable/...) or a timeout | ||
awakeable.await(Duration.ofSeconds(5000)); | ||
// Example of scheduling a handler for later on | ||
SubscriptionServiceClient.fromContext(ctx, "my-sub-123").send(Duration.ofDays(1)).cancel(); | ||
|
||
// 7. PERSIST RESULTS: avoid re-execution of actions on retries | ||
// Use this for non-deterministic actions or interaction with APIs, DBs, ... | ||
// For example, generate idempotency keys that are stable across retries | ||
// Then use these to call other APIs and let them deduplicate | ||
String paymentDeduplicationID = UUID.randomUUID().toString(); | ||
ctx.run(() -> chargeBankAccount(paymentDeduplicationID, 100)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
RestateHttpEndpointBuilder.builder() | ||
.bind(new MyService()) | ||
.bind(new SubscriptionService()) | ||
.buildAndListen(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package utils; | ||
|
||
import dev.restate.sdk.ObjectContext; | ||
import dev.restate.sdk.annotation.Handler; | ||
import dev.restate.sdk.annotation.VirtualObject; | ||
|
||
@VirtualObject | ||
public class SubscriptionService { | ||
|
||
@Handler | ||
public String create(ObjectContext ctx, String userId) { | ||
// Implementation here | ||
return "SUCCESS"; | ||
} | ||
|
||
@Handler | ||
public void cancel(ObjectContext ctx) { | ||
System.out.println("Cancelling all subscriptions for user " + ctx.key()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.