Skip to content

Commit 911cbf2

Browse files
authored
Update README.md
1 parent ca08ed9 commit 911cbf2

File tree

1 file changed

+44
-78
lines changed

1 file changed

+44
-78
lines changed

README.md

Lines changed: 44 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,49 @@
11
# RabbitMQ Kotlin
22
[![CI](https://github.com/viartemev/rabbitmq-kotlin/actions/workflows/gradle.yml/badge.svg?branch=master)](https://github.com/viartemev/rabbitmq-kotlin/actions/workflows/gradle.yml)
3-
[![Open Source Helpers](https://www.codetriage.com/viartemev/the-white-rabbit/badges/users.svg)](https://www.codetriage.com/viartemev/the-white-rabbit)
43
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5-
[![Gitter](https://badges.gitter.im/kotlin-the-white-rabbit/community.svg)](https://gitter.im/kotlin-the-white-rabbit/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
64

7-
The White Rabbit is a [fast](https://github.com/viartemev/the-white-rabbit/issues/88#issuecomment-470461937) and asynchronous RabbitMQ (AMQP) client library based on Kotlin coroutines. Currently the following features are supported:
8-
* Queue and exchange manipulations
9-
* Message publishing with confirmation
10-
* Message consuming with acknowledgment
11-
* Transactional publishing and consuming
12-
* RPC pattern
5+
The RabbitMQ Kotlin Coroutine Library is designed to provide Kotlin developers with an efficient, coroutine-based approach to interact with RabbitMQ.
6+
This library simplifies message queue operations by integrating seamlessly with Kotlin's coroutines, offering a modern and reactive way to handle asynchronous messaging in Kotlin applications.
7+
It supports a variety of advanced features including queue and exchange manipulations, message publishing with confirmation, message consuming with acknowledgment, transactional operations, and the Remote Procedure Call (RPC) pattern.
138

14-
## Adding to project
9+
## Features
1510

16-
## Usage notes and examples
11+
- **Queue and Exchange Manipulations**: Easily create, delete, and configure queues and exchanges. Supports all RabbitMQ exchange types (direct, topic, headers, fanout) and offers flexible options for queue bindings and attributes.
12+
- **Message Publishing with Confirmation**: Publish messages to queues with the option to receive confirmations, ensuring reliable delivery and handling of messages.
13+
- **Message Consuming with Acknowledgment**: Consume messages from queues with acknowledgment support, allowing for precise control over message processing and acknowledging.
14+
- **Transactional Publishing and Consuming**: Support for transactional operations, enabling the grouping of publish and consume actions into atomic units, ensuring data consistency and reliability.
15+
- **RPC Pattern Implementation**: Facilitates the implementation of the RPC pattern, allowing for easy setup of request-response message flows, suitable for service-oriented architectures.
1716

18-
Use one of the extension methods on `com.rabbitmq.client.Connection` to get a channel you need:
17+
## Getting Started
1918

20-
```kotlin
21-
connection.channel {
22-
/*
23-
The plain channel with consumer acknowledgments, supports:
24-
-- queue and exchange manipulations
25-
-- asynchronous consuming
26-
-- RPC pattern
27-
*/
28-
}
29-
30-
connection.confirmChannel { //
31-
/*
32-
Channel with publisher confirmations, additionally supports:
33-
-- asynchronous message publishing
34-
*/
35-
}
36-
37-
connection.txChannel { // transactional support
38-
/*
39-
Supports transactional publishing and consuming.
40-
*/
41-
}
42-
```
43-
44-
### Queue and exchange manipulations
45-
#### Asynchronous exchange declaration
46-
```kotlin
47-
connection.channel.declareExchange(ExchangeSpecification(EXCHANGE_NAME))
48-
```
49-
#### Asynchronous queue declaration
50-
```kotlin
51-
connection.channel.declareQueue(QueueSpecification(QUEUE_NAME))
52-
```
53-
#### Asynchronous queue binding to an exchange
54-
```kotlin
55-
connection.channel.bindQueue(BindQueueSpecification(EXCHANGE_NAME, QUEUE_NAME))
56-
```
19+
## Examples
20+
Full list of examples could be found [here](https://github.com/viartemev/rabbitmq-kotlin/tree/master/rabbitmq-kotlin-example/src/main)
5721

5822
### Asynchronous message publishing with confirmation
5923
```kotlin
60-
connection.confirmChannel {
61-
publish {
62-
val messages = (1..n).map { createMessage("Hello #$it") }
63-
publishWithConfirmAsync(coroutineContext, messages).awaitAll()
64-
}
65-
}
66-
```
67-
or
68-
```kotlin
69-
connection.confirmChannel {
70-
publish {
71-
coroutineScope {
72-
val messages = (1..n).map { createMessage("Hello #$it") }
73-
messages.map { async { publishWithConfirm(it) } }
24+
val connectionFactory = ConnectionFactory().apply { useNio() }
25+
connectionFactory.newConnection().use { connection ->
26+
connection.confirmChannel {
27+
declareQueue(QueueSpecification(PUBLISHER_QUEUE_NAME)).queue
28+
publish {
29+
(1..TIMES).map { createMessage("") }.map { async(Dispatchers.IO) { publishWithConfirm(it) } }.awaitAll()
30+
.forEach { println(it) }
31+
}
7432
}
7533
}
76-
}
7734
```
7835

7936
### Asynchronous message consuming with acknowledgement
8037
Consume only n-messages:
8138
```kotlin
82-
connection.channel {
83-
consume(QUEUE_NAME, PREFETCH_COUNT) {
84-
(1..n).map { async { consumeMessageWithConfirm({ println(it) }) } }.awaitAll()
39+
val connectionFactory = ConnectionFactory().apply { useNio() }
40+
connectionFactory.newConnection().use { connction ->
41+
connction.channel {
42+
consume(CONSUMER_QUEUE_NAME, 1) {
43+
(1..CONSUME_TIMES).map { async(Dispatchers.IO) { consumeMessageWithConfirm(handler) } }.awaitAll()
44+
}
45+
}
8546
}
86-
}
8747
```
8848

8949
### Transactional publishing and consuming
@@ -107,19 +67,25 @@ connection.txChannel {
10767

10868
### RPC pattern
10969
```kotlin
110-
connection.channel {
111-
val message = RabbitMqMessage(MessageProperties.PERSISTENT_BASIC, "Hello world".toByteArray())
112-
coroutineScope {
113-
(1..10).map {
114-
async {
115-
rpc {
116-
call(requestQueueName = "rpc_request", message = message)
117-
.also { println("Reply: ${String(it.body)}") }
118-
}
70+
ConnectionFactory().apply { useNio() }.newConnection().use { conn ->
71+
conn.channel {
72+
logger.info { "Asking for greeting request..." }
73+
val response = withTimeoutOrNull(1000) {
74+
async(Dispatchers.IO) {
75+
rpc {
76+
val result = call(message)
77+
logger.info { "Got a message: ${String(result.body)}" }
78+
result
79+
}
80+
}.await()
11981
}
120-
}.awaitAll()
82+
if (response == null) {
83+
logger.info { "Timeout is exeeded" }
84+
} else {
85+
logger.info { "Result: ${String(response.body)}" }
86+
}
87+
}
12188
}
122-
}
12389
```
12490

12591
## Links

0 commit comments

Comments
 (0)