aleh-zhloba / postgresql-messaging

Lightweight pub/sub for PostgreSQL-backed JVM applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

postgresql-messaging

Maven Central Kotlin Apache License V.2

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

  1. "at-most-once" delivery
  2. preserves publisher notifications order

Features

  1. Async and reactive
  2. Spring integration with support of annotated listener methods
  3. Micro-batch publishing using artificial delay to reduce the number of database requests

1.x release plans

  1. Split codebase into core and spring modules
  2. Implement unsubscribe logic
  3. Allow to publish notifications in outer transactions

Quick start

Install

The library is available on maven central.

Gradle

implementation("io.github.aleh-zhloba:postgresql-messaging:0.5.0")

Maven

<dependency>
    <groupId>io.github.aleh-zhloba</groupId>
    <artifactId>postgresql-messaging</artifactId>
    <version>0.5.0</version>
</dependency>

Example of usage

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()

License

postgresql-messaging is released under version 2.0 of the Apache License.

Footnotes

  1. with no IO exceptions retries configured

About

Lightweight pub/sub for PostgreSQL-backed JVM applications

License:Apache License 2.0


Languages

Language:Kotlin 100.0%