java.lang.Object
com.siperf.commons.data.expirable.ExpirableObject
com.siperf.amistream.connection.both.transcation.Transaction
All Implemented Interfaces:
Expirable
Direct Known Subclasses:
ClientAmiTransaction, ServerAmiTransaction

public class Transaction extends ExpirableObject
Represents a transaction in the system that can be started, stopped, and tracked.

A transaction has a lifecycle with states such as started, stopped, and completed. It also has a defined expiration time after which it is considered expired if not completed.

See Also:
  • Field Details

    • log

      protected static final org.slf4j.Logger log
      Logger for logging transaction-related events
    • name

      protected String name
      Name of the transaction
    • startedAt

      protected volatile long startedAt
      Time when the transaction was started (in milliseconds)
    • finishedAt

      protected volatile long finishedAt
      Time when the transaction was finished (in milliseconds)
    • executionTimer

      protected volatile long executionTimer
      Timer tracking the execution time of the transaction
    • processor

      protected TransactionProcessor processor
      Processor associated with the transaction
  • Constructor Details

    • Transaction

      protected Transaction(Id id)
      Constructs a new Transaction with the specified unique identifier.

      The transaction is initialized with the current time and a default expiration time.

      Parameters:
      id - the unique identifier for this transaction
  • Method Details

    • getId

      public Id getId()
    • getName

      public String getName()
    • getStartedAt

      public long getStartedAt()
    • getFinishedAt

      public long getFinishedAt()
    • getExecutionTimer

      public long getExecutionTimer()
    • getFinalState

      public Transaction.FinalState getFinalState()
    • getProcessor

      public TransactionProcessor getProcessor()
    • start

      public void start()
      Starts the transaction. If the transaction is already started, a log is generated and the start attempt is ignored.

      The method also initializes the execution timer and invokes the TransactionProcessor.processStart() method.

    • stopOk

      public void stopOk()
      Stops the transaction with the final state set to Transaction.FinalState.OK.
    • stop

      public void stop(Transaction.FinalState finalState)
      Stops the transaction with the specified final state.

      The method ensures that the transaction can only be stopped once and that the final state is set accordingly. It also invokes the TransactionProcessor.onStop() method.

      Parameters:
      finalState - the final state of the transaction upon stopping
    • isActive

      public boolean isActive()
      Checks if the transaction is still active (i.e., it has started but not finished).
      Returns:
      true if the transaction is active, otherwise false
    • getExecutionTime

      public long getExecutionTime()
      Gets the execution time of the transaction.

      If the transaction has not started or has been finished, appropriate values are returned.

      Returns:
      the execution time in milliseconds, or 0 if the transaction has not started
    • isCompleted

      public boolean isCompleted()
      Checks if the transaction has been completed (i.e., it has both started and finished).
      Returns:
      true if the transaction is completed, otherwise false
    • isResultOK

      public boolean isResultOK()
      Checks if the transaction result is Transaction.FinalState.OK.
      Returns:
      true if the final state of the transaction is Transaction.FinalState.OK, otherwise false