|
ORE Studio 0.0.4
|
Manages a dedicated PostgreSQL connection to listen for NOTIFY events. More...
#include <postgres_listener_service.hpp>

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_service & | operator= (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. | |
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.
| 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.
|
explicit |
Constructs a postgres_listener_service.
Creates a dedicated PostgreSQL connection for listening using the credentials from the provided database context.
| ctx | The database context containing connection credentials. |
| callback | The callback function to be invoked when a notification is received. |
Destroys the postgres_listener_service.
Stops the listener thread and closes the dedicated connection.
| 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.

| 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.
| channel_name | The name of the channel to listen on (e.g., "ores_currencies"). |

| void notify | ( | const std::string & | channel_name, |
| const std::string & | payload | ||
| ) |
Sends a NOTIFY on a PostgreSQL channel.
| channel_name | The name of the channel to notify. |
| payload | The payload string to send with the notification. |