-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
245e7e6
commit 05eb970
Showing
10 changed files
with
282 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# pgmq-kotlin-jvm | ||
|
||
[![codecov](https://codecov.io/gh/vdsirotkin/pgmq-kotlin-jvm/graph/badge.svg?token=GAXNOBXHKX)](https://codecov.io/gh/vdsirotkin/pgmq-kotlin-jvm) | ||
|
||
A Kotlin client, based on pure JDBC, for [Postgres Message Queue](https://github.com/tembo-io/pgmq) (PGMQ). | ||
|
||
# Compatibility | ||
|
||
This library currently supports following PGMQ API: | ||
* `create` | ||
* `drop_queue` | ||
* `list_queues` | ||
* `send_batch` | ||
* `read` | ||
* `pop` | ||
* `archive` | ||
* `delete` | ||
* `purge_queue` | ||
|
||
# Usage | ||
|
||
First of all - you need to set up `PgmqConnectionFactory` and `PgmqSerializationProvider` | ||
|
||
### PgmqConnectionFactory | ||
|
||
You are free to implement it either way you wish :) | ||
|
||
Example implementation for Spring, which acquires current connection inside of `@Transactional` method (may be useful if you need PGMQ to be transational): | ||
|
||
```kotlin | ||
@Bean | ||
fun pgmqConnectionFactory(dataSource: DataSource) = PgmqConnectionFactory { | ||
DataSourceUtils.getConnection(dataSource) | ||
} | ||
``` | ||
|
||
Please refer to Spring's [DataSourceUtils](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/datasource/DataSourceUtils.html#getConnection(javax.sql.DataSource)) docs for explanations | ||
|
||
### PgmqSerializationProvider | ||
|
||
This library provides 2 implementations - [GsonPgmqSerializationProvider](src%2Fmain%2Fkotlin%2Fcom%2Fvdsirotkin%2Fpgmq%2Fserialization%2FGsonPgmqSerializationProvider.kt) and [JacksonPgmqSerializationProvider](src%2Fmain%2Fkotlin%2Fcom%2Fvdsirotkin%2Fpgmq%2Fserialization%2FJacksonPgmqSerializationProvider.kt) | ||
|
||
If you use some other serialization library - feel free to implement [PgmqSerializationProvider](src%2Fmain%2Fkotlin%2Fcom%2Fvdsirotkin%2Fpgmq%2Fserialization%2FPgmqSerializationProvider.kt) interface, it's simple! :) | ||
|
||
### PgmqConfiguration | ||
|
||
Next step should be defining [PgmqConfiguration](src%2Fmain%2Fkotlin%2Fcom%2Fvdsirotkin%2Fpgmq%2Fconfig%2FPgmqConfiguration.kt). You can also implement it either way you want. | ||
|
||
Here's an example for Spring's Configuration Properties: | ||
|
||
```kotlin | ||
@ConfigurationProperties(prefix = "pgmq") | ||
data class PgmqConfigurationProps( | ||
override val defaultVisibilityTimeout: java.time.Duration = 30.seconds.toJavaDuration() | ||
) : PgmqConfiguration | ||
``` | ||
|
||
### Building client | ||
|
||
If you have set up connection factory and serializer - you're free to build the client! | ||
|
||
Here's complete example of Spring setup for PgmqClient - [SpringPgmqApplication.kt](src%2Ftest%2Fkotlin%2Fcom%2Fvdsirotkin%2Fpgmq%2Fspring%2FSpringPgmqApplication.kt). | ||
|
||
# Contributions | ||
|
||
Contributions are always welcomed! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
src/main/kotlin/com/vdsirotkin/pgmq/config/PgmqConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.vdsirotkin.pgmq.config | ||
|
||
import kotlin.time.Duration | ||
|
||
interface PgmqConfiguration { | ||
val defaultVisibilityTimeout: Duration | ||
val defaultVisibilityTimeout: java.time.Duration | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/kotlin/com/vdsirotkin/pgmq/spring/SpringPgmqApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.vdsirotkin.pgmq.spring | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.vdsirotkin.pgmq.PgmqClient | ||
import com.vdsirotkin.pgmq.config.PgmqConfiguration | ||
import com.vdsirotkin.pgmq.config.PgmqConnectionFactory | ||
import com.vdsirotkin.pgmq.objectMapper | ||
import com.vdsirotkin.pgmq.serialization.JacksonPgmqSerializationProvider | ||
import com.vdsirotkin.pgmq.serialization.PgmqSerializationProvider | ||
import org.springframework.boot.autoconfigure.SpringBootApplication | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.jdbc.datasource.DataSourceUtils | ||
import javax.sql.DataSource | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlin.time.toJavaDuration | ||
|
||
@SpringBootApplication | ||
@EnableConfigurationProperties(PgmqConfigurationProps::class) | ||
open class SpringPgmqApplication { | ||
@Configuration(proxyBeanMethods = false) | ||
open class PgmqConfig { | ||
@Bean | ||
open fun objectMapper() = objectMapper | ||
|
||
@Bean | ||
open fun pgmqConnectionFactory(dataSource: DataSource) = PgmqConnectionFactory { | ||
DataSourceUtils.getConnection(dataSource) | ||
} | ||
|
||
@Bean | ||
open fun pgmqSerializer(objectMapper: ObjectMapper) = JacksonPgmqSerializationProvider(objectMapper) | ||
|
||
@Bean | ||
open fun pgmqClient( | ||
pgmqConnectionFactory: PgmqConnectionFactory, | ||
pgmqSerializationProvider: PgmqSerializationProvider, | ||
pgmqConfiguration: PgmqConfiguration | ||
) = PgmqClient(pgmqConnectionFactory, pgmqSerializationProvider, pgmqConfiguration) | ||
} | ||
} | ||
|
||
@ConfigurationProperties(prefix = "pgmq") | ||
data class PgmqConfigurationProps( | ||
override val defaultVisibilityTimeout: java.time.Duration = 30.seconds.toJavaDuration() | ||
) : PgmqConfiguration |
Oops, something went wrong.