Evento Framework
  • Introduction
  • Architecture Overview
    • Distributed Systems & Microservices
    • Recative Manifesto & Reactive Principles
    • State-of-the-art Patterns
      • DDD (Domain Driven Design)
      • CQRS (Command Query Responsability Separation)
      • Event Sourcing
      • Messaging
      • Saga
  • GETTING STARTED
    • Quick Start
    • TodoList - RECQ Tutorial
      • Problem Description and Requirement Gathering
      • RECQ Payload Design
      • RECQ Components Design
      • Set up your Development Environment
      • RECQ Payload Evento Implementation
        • Domain Commands
        • Domain Events
        • Views
        • Queries
      • RECQ Components Evento Implementation with Spring Data
        • TodoListAggregate
        • TodoList Model with Spring Data
        • TodoListProjector
        • TodoListProjection
        • TodoList Invoker
      • Expose the RECQ architecture with Spring Web
      • Test Your App
    • Extend TodoList - Handle Complexity Tutorial
      • Unique identifier generation
      • Extends behaviors with Observer and Services
      • Cross Domain Consistency with Sagas
      • Handle Real time data updates with MQTT and Save-Notify Pattern
  • RECQ Patterns
    • RECQ Patterns
    • RECQ System Pattern
      • Component
      • Message Gateway
      • System State Store
    • RECQ Communication Pattern
      • Component to Component
      • Component to System State Store
      • System State Store to Component
    • RECQ Component Pattern
      • Aggregate
      • Projector
      • Projection
      • Service
      • Invoker
      • Saga
      • Observer
  • Evento Framework
    • Evento Framework Introcution
    • Payload and Messages
      • Command
        • Domain Command
        • Service Command
      • Event
        • Domain and Service Event
      • Query and View
    • @Component
      • @Aggregate
        • Aggregate State
        • @AggregateCommandHandler
        • @EventSourcingHandler
      • @Projector
        • Projector @EventHandler
      • @Projection
        • @QueryHandler
      • @Service
        • @CommandHandler
      • @Invoker
      • @Saga
        • SagaState
        • @SagaEventHandler
      • @Observer
    • Dead Event Queues
    • EventoBundle
      • EventoServerMessageBusConfiguration
      • ConsumerStateStore
        • InMemoryConsumerStateStore
        • PostgresConsumerStateStore
        • MysqlConsumerStateStore
      • Context
      • TracingAgend and @Track
        • SentryTracingAgent
      • Autoscaling Protocol
        • ThreadCountAutoscalingProtocol
      • Injector and @Component
  • Evento Server
    • Evento Server Introduction
    • SetUp Evento Server
      • Advanced Options
      • Evento Event Store Modes: APES and CPES
    • Evento Server Cluster
    • Bundle Deploy Script
  • EVENTO GUI
    • Explore RECQ Systems Visually
    • GUI Auth
    • Payload Catalog
    • Component Catalog
    • Bundle Catalog
    • Cluster Status (Experimental)
    • Flows
      • Performance Evaluation
    • Application Graph
    • System State Store
  • Evento CLI
    • Evento CLI Introduction
    • Update Version
    • Publish
Powered by GitBook
On this page
  1. Evento Framework
  2. EventoBundle
  3. ConsumerStateStore

PostgresConsumerStateStore

The PostgresConsumerStateStore class within the Evento Framework extends ConsumerStateStore and provides a robust solution for storing and managing consumer state information in a PostgreSQL database. This chapter explores the functionalities and usage of PostgresConsumerStateStore for persistent state handling within Evento applications.

Persistent Storage:

  • Leverages PostgreSQL, a relational database management system, to store consumer state data.

  • This enables data persistence across application restarts, ensuring that saga state and consumer progress are preserved.

  • Ideal for production environments where data loss prevention is critical.

Key Methods:

  • Constructors:

    • The primary constructor accepts an EventoServer instance, a PerformanceService instance, a Connection object for connecting to the PostgreSQL database, an ObjectMapper for serialization/deserialization, and an Executor for handling observer execution concurrently.

  • init(): This method is crucial and should be called once before using the consumer state store. It executes Data Definition Language (DDL) statements to create the necessary tables (evento__consumer_state and evento__saga_state) within the PostgreSQL database if they don't already exist.

  • removeSagaState(Long sagaId): Deletes the state associated with a specific saga identified by its ID.

  • leaveExclusiveZone(String consumerId): Releases the advisory lock on the consumer, signifying it has exited the exclusive zone.

  • enterExclusiveZone(String consumerId): Attempts to acquire an advisory lock for the consumer using PostgreSQL's pg_advisory_lock function. This ensures exclusive access to the consumer's state during processing.

  • getLastEventSequenceNumber(String consumerId): Retrieves the last processed event sequence number for a consumer from the evento__consumer_state table.

  • setLastEventSequenceNumber(String consumerId, Long eventSequenceNumber): Updates the last processed event sequence number for a consumer in the evento__consumer_state table.

  • getSagaState(String sagaName, String associationProperty, String associationValue): Retrieves the stored state for a saga identified by its name, association property, and association value. It utilizes a JSON path query within the PostgreSQL database to efficiently locate the relevant saga state.

  • setSagaState(Long id, String sagaName, SagaState sagaState): Sets the state for a saga. If an ID is provided (indicating an existing saga), it updates the state for that specific saga. Otherwise, it inserts a new entry for the saga into the evento__saga_state table.

Usage Example:

The provided code snippet demonstrates how to configure PostgresConsumerStateStore within an Evento application using a lambda expression within the setConsumerStateStoreBuilder method:

EventoBundle.Builder.builder()
    .setBasePackage(DemoSagaApplication.class.getPackage())
    .setConsumerStateStoreBuilder(((eventoServer, performanceService) -> {
        return new PostgresConsumerStateStore(
                eventoServer,
                performanceService,
                connection); // Connection Object
    }))
    ...
    .start();

In this example, a Connection object (representing the connection to the PostgreSQL database) is provided during the consumer state store creation. This approach offers flexibility in managing database connections.

Important Considerations:

  • PostgresConsumerStateStore relies on a PostgreSQL database for storage. Ensure you have a properly configured PostgreSQL instance accessible to your Evento application.

  • Remember to call the init() method to create the necessary tables before using the consumer state store.

  • PostgreSQL offers strong consistency guarantees for data persistence.

By utilizing PostgresConsumerStateStore, you can ensure that your Evento application maintains a persistent record of consumer state and saga data across restarts. This is essential for production environments where data integrity and reliability are paramount.

PreviousInMemoryConsumerStateStoreNextMysqlConsumerStateStore

Last updated 12 months ago