Lightweight Publish-Subscribe (pub/sub) layer for PostgreSQL-backed distributed JVM applications. Cheap inter-process communication without having to bring additional infrastructure.
Uses PostgreSQL LISTEN / NOTIFY
built-in asynchronous notifications.
Guarantees 1
- "at-most-once" delivery
- preserves publisher notifications order
- Async and reactive
- Spring integration with support of annotated listener methods
- Micro-batch publishing using artificial delay to reduce the number of database requests
- Split codebase into core and spring modules
- Implement unsubscribe logic
- Allow to publish notifications in outer transactions
The library is available on maven central.
implementation("io.github.aleh-zhloba:postgresql-messaging:0.5.0")
<dependency>
<groupId>io.github.aleh-zhloba</groupId>
<artifactId>postgresql-messaging</artifactId>
<version>0.5.0</version>
</dependency>
The library comes with the auto-configuration class, so if you have configured JDBC DataSource
or R2DBC ConnectionFactory
no additional steps required.
Listen notification messages with handler method:
@PostgresMessageListener(value = ["channel1", "channel2"], skipLocal = true)
fun handleNotification(notification: YourNotificationClass) {
// ...
}
Sending notification messages using PostgresMessagingTemplate
:
messagingTemplate.convertAndSend("channel1", YourNotificationClass())
Reactive API:
val pubSub: PostgresPubSub = R2dbcPostgresPubSub(connectionFactory)
pubSub.subscribe("channel1", "channel2")
.map { notification ->
// ...
}
.subscribe()
pubSub.publish("channel1", "payload")
.subscribe()
postgresql-messaging is released under version 2.0 of the Apache License.
Footnotes
-
with no IO exceptions retries configured ↩