Skip to content

Commit

Permalink
Java formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdongen committed Aug 27, 2024
1 parent 0e539bb commit 2ade447
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public void userEvent(ObjectContext ctx, String name) {
ctx.set(USER, profile);
// </mark_1>

ctx.get(USER).orElse(new UserProfile());
// <mark_3>
ProfileServiceClient.fromContext(ctx, ctx.key()).send(Duration.ofSeconds(1)).emit();
// </mark_3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,102 +22,3 @@ public void reserveProduct(String productId, String reservationId) {
// <end_here>

}


@Workflow
public class SignupWorkflow {

final DurablePromiseKey<String> EMAIL_CLICKED =
DurablePromiseKey.of("email_clicked", STRING);

@Workflow
public boolean run(WorkflowContext ctx, User user) {

// Persist progress: code doesn't get re-executed on retries
ctx.run(() -> createUserEntry(user));

String secret = ctx.random().nextUUID().toString();
ctx.run(() -> sendEmailWithLink(user.getEmail(), secret));

// Wait until user clicked email verification link
// Resolved or rejected by the other handlers
String clickSecret =
ctx.promise(EMAIL_CLICKED)
.awaitable()
.await();
return clickSecret.equals(secret);
}

@Shared
public void click(SharedWorkflowContext ctx, String secret) {
// Durable signals and query:
// Workflow interaction via callbacks, Kafka events,...
ctx.promiseHandle(EMAIL_CLICKED).resolve(secret);
}
}


@VirtualObject
public class UserUpdatesService {

@Handler
public void updateUser(ObjectContext ctx, UpdateEvent event) {
// Durable actions: result persisted and recovered on failures
String userId = ctx.run(STRING, () ->
updateUserProfile(event.getProfile()));

// Flexible control flow: no restrictions (e.g. loops, cycles)
// Each event crafts its own recovery log
while (userId.equals("NOT_READY")) {
// Delay execution
ctx.sleep(Duration.ofMillis(5000));
userId = ctx.run(STRING, () ->
updateUserProfile(event.getProfile()));
}

String roleId = ctx.run(STRING, () ->
setUserPermissions(userId, event.getPermissions()));
ctx.run(() -> provisionResources(
userId, roleId, event.getResources()));
}
}



@VirtualObject
public class ProfileService {

final StateKey<UserProfile> USER =
StateKey.of("user", UserSerde());

@Handler
public void userEvent(ObjectContext ctx, String name) {
// Durable actions: result persisted and recoverable
// Enrich events with data from multiple sources
UserProfile profile = ctx.run(() ->
createUserProfile(name));

// Persistent built-in K/V state
ctx.set(USER, profile);

// Delay execution or schedule async actions
// Restate triggers them when the time comes
ProfileServiceClient.fromContext(ctx, name)
.send(Seconds(1)).emit();
}

// The same functions can be called over RPC and Kafka
@Handler
public void featureEvent(ObjectContext ctx, String email) {
UserProfile user = ctx.get(USER);
user.setEmail(email);
ctx.set(USER, user);
}

@Handler
public void emit(ObjectContext ctx) {
UserProfile user = ctx.get(USER);
sendDownstream(user);
ctx.clearAll();
}
}

0 comments on commit 2ade447

Please sign in to comment.