ORE Studio 0.0.4
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
postgres_listener_service Class Referencefinal

Manages a dedicated PostgreSQL connection to listen for NOTIFY events. More...

#include <postgres_listener_service.hpp>

Collaboration diagram for postgres_listener_service:
Collaboration graph

Public Types

using notification_callback_t = std::function< void(const domain::entity_change_event &)>
 Type alias for the notification callback function.
 

Public Member Functions

 postgres_listener_service (database::context ctx, notification_callback_t callback)
 Constructs a postgres_listener_service.
 
 ~postgres_listener_service ()
 Destroys the postgres_listener_service.
 
 postgres_listener_service (const postgres_listener_service &)=delete
 
postgres_listener_serviceoperator= (const postgres_listener_service &)=delete
 
void start ()
 Starts the listener thread and begins listening for notifications.
 
void stop ()
 Stops the listener thread and waits for it to join.
 
void subscribe (const std::string &channel_name)
 Subscribes to a PostgreSQL NOTIFY channel.
 
void notify (const std::string &channel_name, const std::string &payload)
 Sends a NOTIFY on a PostgreSQL channel.
 

Detailed Description

Manages a dedicated PostgreSQL connection to listen for NOTIFY events.

This service runs a separate thread to continuously listen for asynchronous notifications on configured channels. When a notification is received, it parses the payload into a domain::entity_change_event object and dispatches it via a callback.

The service maintains its own dedicated connection separate from any connection pool, as LISTEN/NOTIFY requires a persistent connection.

Member Typedef Documentation

◆ notification_callback_t

using notification_callback_t = std::function<void(const domain::entity_change_event&)>

Type alias for the notification callback function.

The callback is invoked with the parsed domain::entity_change_event object. Note: The callback is invoked from the listener thread, so implementations should be thread-safe and non-blocking.

Constructor & Destructor Documentation

◆ postgres_listener_service()

Constructs a postgres_listener_service.

Creates a dedicated PostgreSQL connection for listening using the credentials from the provided database context.

Parameters
ctxThe database context containing connection credentials.
callbackThe callback function to be invoked when a notification is received.

◆ ~postgres_listener_service()

Destroys the postgres_listener_service.

Stops the listener thread and closes the dedicated connection.

Member Function Documentation

◆ start()

void start ( )

Starts the listener thread and begins listening for notifications.

Any channels subscribed via subscribe() before calling start() will have LISTEN commands issued when the thread starts.

Here is the caller graph for this function:

◆ subscribe()

void subscribe ( const std::string &  channel_name)

Subscribes to a PostgreSQL NOTIFY channel.

Can be called before or after start(). If called before start(), the LISTEN command will be issued when the listener thread starts. If called after start(), the LISTEN command is issued immediately on the dedicated connection.

Parameters
channel_nameThe name of the channel to listen on (e.g., "ores_currencies").
Here is the caller graph for this function:

◆ notify()

void notify ( const std::string &  channel_name,
const std::string &  payload 
)

Sends a NOTIFY on a PostgreSQL channel.

Parameters
channel_nameThe name of the channel to notify.
payloadThe payload string to send with the notification.