Skip to content

Commit

Permalink
Added README
Browse files Browse the repository at this point in the history
  • Loading branch information
vooft committed Aug 3, 2024
1 parent 43f0bf7 commit 06d78c1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
![Build and test](https://github.com/vooft/pg-kueue/actions/workflows/build.yml/badge.svg?branch=main)
![Releases](https://img.shields.io/github/v/release/vooft/pg-kueue)
![Maven Central](https://img.shields.io/maven-central/v/io.github.vooft/pg-kueue-core)
![License](https://img.shields.io/github/license/vooft/pg-kueue)

# pg-kueue
Kotlin Coroutines PostgresSQL-based message queue using LISTEN/NOTIFY

Everything is String-based and for now just follows normal LISTEN/NOTIFY rules.

# Supported database SPIs
This library was designed to be used with different SPIs, providing similar coroutines-based interface.

Currently, only JDBC-based SPI is implemented + helper functions for jOOQ.

## JDBC
pg-kueue uses virtual threads to make any calls to the database in a non-blocking fashion.

It works with any `java.sql.DataSource` implementation, reserving one connection for listening to notifications.

# JDBC usage

## Gradle
```kotlin
repositories {
mavenCentral()
}

dependencies {
implementation("io.github.vooft:pg-kueue-jdbc:<version>")
}
```

## Simple usage
```kotlin
val dataSource = createMyDataSource()

val kueue = Kueue.jdbc(dataSource)
val subscription = kueue.subscribe(KueueTopic("my_topic")) { message: String ->
println("Received message: $message")
}

kueue.send(KueueTopic("my_topic"), "Hello, world!")
// will print after a tiny delay: "Received message: Hello, world!"
```

You can close subscription, if you would like to stop a particular listener:
```kotlin
subscription.close()
```

But it is not necessary if the the subscription should exist for the whole Kueue lifecycle.

All subscriptions will be closed automatically when Kueue is closed:
```kotlin
kueue.close()
```

## Transactional usage
To send a message using existing transaction, you should provide the transactional connection.

Normally, API accepts a instance of a wrapped connection `KueueConnection`, there is a helper method to create it:
```kotlin
val transactionalConnection = myBeginTransaction()
kueue.send(KueueTopic("my_topic"), "Hello, world!", kueue.wrap(transactionalConnection))
```

There is also an extension function for a specific library to simplify transactional sending:
```kotlin
val transactionalConnection = myBeginTransaction()
kueue.send(KueueTopic("my_topic"), "Hello, world!", transactionalConnection) // an extension function must be imported explicitly
```

# Additional modules
## jOOQ JDBC
There is a module that accepts a jOOQ `DSLContext` and provides a similar interface to the JDBC module.

### Gradle
```kotlin
repositories {
mavenCentral()
}

dependencies {
implementation("io.github.vooft:pg-kueue-jooq-jdbc:<version>")
}
```

### Usage
```kotlin
val dslContext = createMyDslContext()

// there is a helper method to create a Kueue instance using, for example, a non-transactional DSLContext
val kueue = Kueue.jooq(dslContext)

// also there is an extension method that accepts a transactional DSLContext to send notification within a transaction
kueue.send(KueueTopic("my_topic"), "Hello, world!", myTransactionalDslContext)
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.vooft.kueue.jdbc

import io.github.vooft.kueue.Kueue
import io.github.vooft.kueue.KueueTopic
import io.github.vooft.kueue.impl.KueueImpl
import java.sql.Connection
import javax.sql.DataSource
Expand All @@ -9,3 +10,7 @@ fun Kueue.Companion.jdbc(dataSource: DataSource): Kueue<Connection, JdbcKueueCon
connectionProvider = DataSourceKueueConnectionProvider(dataSource),
pubSub = JdbcKueueConnectionPubSub()
)

suspend fun Kueue<Connection, JdbcKueueConnection>.send(topic: KueueTopic, message: String, transactionalConnection: Connection) {
send(topic, message, wrap(transactionalConnection))
}

0 comments on commit 06d78c1

Please sign in to comment.