Record Class SagaStep<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.event.saga.SagaStep<T>
Type Parameters:
T - the context type - 上下文类型
Record Components:
name - the step name - 步骤名称
action - the action to execute - 要执行的操作
compensation - the compensation action for rollback - 用于回滚的补偿操作
timeout - the timeout for this step - 此步骤的超时
maxRetries - the maximum number of retries - 最大重试次数

Features | 主要功能:

  • Individual saga step definition - 单个saga步骤定义
  • Action and compensation pairing - 操作和补偿配对

Usage Examples | 使用示例:

SagaStep<OrderContext> step = SagaStep.of(
    "reserve-inventory",
    ctx -> inventoryService.reserve(ctx),
    ctx -> inventoryService.release(ctx)
);

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)

public record SagaStep<T>(String name, Consumer<T> action, Consumer<T> compensation, Duration timeout, int maxRetries) extends Record
Saga Step - A single step in a saga transaction Saga 步骤 - saga 事务中的单个步骤
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • SagaStep

      public SagaStep(String name, Consumer<T> action, Consumer<T> compensation, Duration timeout, int maxRetries)
      Creates an instance of a SagaStep record class.
      Parameters:
      name - the value for the name record component
      action - the value for the action record component
      compensation - the value for the compensation record component
      timeout - the value for the timeout record component
      maxRetries - the value for the maxRetries record component
  • Method Details

    • of

      public static <T> SagaStep<T> of(String name, Consumer<T> action)
      Creates a simple step with only an action. 创建只有操作的简单步骤。
      Type Parameters:
      T - the context type - 上下文类型
      Parameters:
      name - the step name - 步骤名称
      action - the action - 操作
      Returns:
      the step - 步骤
    • of

      public static <T> SagaStep<T> of(String name, Consumer<T> action, Consumer<T> compensation)
      Creates a step with action and compensation. 创建带有操作和补偿的步骤。
      Type Parameters:
      T - the context type - 上下文类型
      Parameters:
      name - the step name - 步骤名称
      action - the action - 操作
      compensation - the compensation - 补偿
      Returns:
      the step - 步骤
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • action

      public Consumer<T> action()
      Returns the value of the action record component.
      Returns:
      the value of the action record component
    • compensation

      public Consumer<T> compensation()
      Returns the value of the compensation record component.
      Returns:
      the value of the compensation record component
    • timeout

      public Duration timeout()
      Returns the value of the timeout record component.
      Returns:
      the value of the timeout record component
    • maxRetries

      public int maxRetries()
      Returns the value of the maxRetries record component.
      Returns:
      the value of the maxRetries record component