Skip to content

support annotation-based approach #541

Open

Description

@zakaria-shahen

I don't like passing all the app logic as lambdas because it makes the codebase messy. It would be great if Restate used an annotation-based approach instead (or something else), which would mean moving the responsibility from the caller method to the called method

What if we created a @Run annotation instead of calling ctx.run()? For example:

  @Workflow
  public boolean run(WorkflowContext ctx, Email email) {
    String secret = ctx.random().nextUUID().toString();
    ctx.set(STATUS, "Generated secret");

    // ctx.run("send email", () -> sendEmailWithLink(email, secret));
    sendEmailWithLink(email, secret);
    ctx.set(STATUS, "Sent email");

    String clickSecret =
        ctx.promise(EMAIL_CLICKED)
            .future()
            .await();
    ctx.set(STATUS, "Clicked email");

    return clickSecret.equals(secret);
  }

  @Run("send email")
  public void sendEmailWithLink(Email email, String secret) {
       // ......
   }

That's not only for ctx.run(), it can be applied to a lot of methods.

Activity

  1. slinkydeveloper commented on Sep 15, 2025

    @slinkydeveloper
    Contributor

    Hi, the idea per se makes sense, but the way I see it the only approach where this would look good is through reflections, which is something I'm currently avoiding as much as possible in the SDK.

  2. zakaria-shahen commented on Sep 17, 2025

    @zakaria-shahen
    Author

    I agree with you about avoiding reflections as much as possible. However, is there any way to avoid forcing the client code to use lambdas excessively, at least in the caller code?

    Maybe like that:

      @Workflow
      public boolean run(WorkflowContext ctx, Email email) {
        String secret = ctx.random().nextUUID().toString();
        ctx.set(STATUS, "Generated secret");
    
        // ctx.run("send email", () -> sendEmailWithLink(email, secret));
        sendEmailWithLink(email, secret).run();
        ctx.set(STATUS, "Sent email");
    
        String clickSecret =
            ctx.promise(EMAIL_CLICKED)
                .future()
                .await();
        ctx.set(STATUS, "Clicked email");
    
        return clickSecret.equals(secret);
      }
    
      public Step<Void> sendEmailWithLink(Email email, String secret) {
            Context.getCurrent().run("send email", () -> {
                // ..
            });
       }
  3. slinkydeveloper commented on Sep 18, 2025

    @slinkydeveloper
    Contributor
      public DurableFuture<Void> sendEmailWithLink(Context ctx, Email email, String secret) {
            ctx.run("send email", () -> {
                // ..
            });
       }

    This should work fine

  4. zakaria-shahen commented on Sep 18, 2025

    @zakaria-shahen
    Author

    That's great, thank you! But is there any way to fetch the Context via a static method?

  5. ooraini commented on Dec 8, 2025

    @ooraini

    @slinkydeveloper An alternative programming model using protected methods + proxy generation would offer a better DX. As for context, if you are willing to require Java 25 in a future release then you can use ScopedValues.

    https://www.morling.dev/blog/building-durable-execution-engine-with-sqlite/

  6. slinkydeveloper commented on Jan 14, 2026

    @slinkydeveloper
    Contributor

    Hey all, i've been exploring a solution that uses proxy generation, like: you provide any class, and all public methods will become "ctx.run wrapped". Something along the lines of:

    var wrappedClient = Restate.runProxy(theClient[, retryOptions]); 
    
    wrappedClient.someMethod() // This becomes ctx.run("someMethod", () -> theClient.someMethod())
    

    ofc this would require the return value of someMethod to be jackson serializable (futures and co would not work correctly for example). WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions