Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions kotlin-java-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/*
.gradle/*
95 changes: 95 additions & 0 deletions kotlin-java-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# RabbitMQ Tutorials in Kotlin

This is a minimalistic Kotlin port of the [RabbitMQ tutorials in Java](https://www.rabbitmq.com/getstarted.html).
The port is admittedly quite close to Java in terms of code style.


## Compiling the Code

``` shell
gradle clean compileKotlin
```

## Running the Tutorials

### Tutorial 1

Execute the following command to start a Hello, world consumer

``` shell
gradle run -P main=Recv
```

Execute the following in a separate shell to publish a Hello, world messge:

``` shell
gradle run -P main=Send
```

### Tutorial 2

Send a task message. The task will be completed immediately

``` shell
gradle run -P main=NewTask
```

To start a worker (run in a separate shell):

``` shell
gradle run -P main=Worker
```

Send a task message. It will wait for 1 second for each dot in the payload.

``` shell
gradle run -P main=NewTask -P argv="rabbit1 ...."
```

Add more workers to the same queue, message will be distributed in the
round robin manner.

### Tutorial 3

``` shell
gradle run -P main=ReceiveLogs
```


``` shell
gradle run -P main=EmitLog -P argv="rabbit1, msg1"
```

### Tutorial 4

``` shell
gradle run -P main="ReceiveLogsDirect" -P argv="info,error"
```

``` shell
gradle run -P main=EmitLogDirect"
```

### Tutorial 5

``` shell
gradle run -P main=ReceiveLogsTopic -P argv="anonymous.*"
```

``` shell
gradle run -P main=EmitLogTopic -P argv="anonymous.info"
```

### Tutorial 6

In one shell:

``` shell
gradle run -P main=RPCServer
```

In another shell:

``` shell
gradle run -P main=RPCClient
```
File renamed without changes.
2 changes: 2 additions & 0 deletions kotlin-java-client/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
main =
argv =
File renamed without changes.
1 change: 1 addition & 0 deletions kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build/*
.gradle/*
.kotlin
141 changes: 81 additions & 60 deletions kotlin/README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,116 @@
# RabbitMQ Tutorials in Kotlin
# RabbitMQ Tutorials in Kotlin (JVM & Native)

This is a minimalistic Kotlin port of the [RabbitMQ tutorials in Java](https://www.rabbitmq.com/getstarted.html).
The port is admittedly quite close to Java in terms of code style.
This repository contains RabbitMQ tutorials implemented using [Kourier](https://kourier.dev), a pure Kotlin multiplatform AMQP client.

## About

## Compiling the Code
These tutorials demonstrate the core concepts of RabbitMQ using Kotlin and the Kourier AMQP client library. All examples work on JVM, macOS (ARM64), Linux (x64), and Windows (x64) platforms through Kotlin Multiplatform.

``` shell
gradle clean compileKotlin
## Prerequisites

- RabbitMQ server running on localhost (default port 5672)
- Kotlin 2.1.21
- Gradle

## Building

```bash
./gradlew build
```

## Running the Tutorials
## Tutorials

### Tutorial 1
### 1. Hello World (HelloWorld.kt)

Execute the following command to start a Hello, world consumer
The simplest thing that does something - sending and receiving messages from a named queue.

``` shell
gradle run -P main=Recv
```
Functions:
- `send()` - Sends "Hello World!" message to the queue
- `receive()` - Receives and prints messages from the queue

Execute the following in a separate shell to publish a Hello, world messge:
### 2. Work Queues (WorkQueues.kt)

``` shell
gradle run -P main=Send
```
Distributing time-consuming tasks among multiple workers.

### Tutorial 2
Functions:
- `newTask(message)` - Sends a task to the work queue (dots represent work time)
- `worker(workerName)` - Processes tasks with fair dispatch and manual acknowledgment

Send a task message. The task will be completed immediately
Key concepts:
- Message durability
- Fair dispatch with `basicQos`
- Manual acknowledgments

``` shell
gradle run -P main=NewTask
```
### 3. Publish/Subscribe (PublishSubscribe.kt)

To start a worker (run in a separate shell):
Sending messages to many consumers at once using fanout exchanges.

``` shell
gradle run -P main=Worker
```
Functions:
- `emitLog(message)` - Publishes log messages to all subscribers
- `receiveLogs(subscriberName)` - Subscribes to all log messages

Send a task message. It will wait for 1 second for each dot in the payload.
Key concepts:
- Fanout exchanges
- Temporary queues
- Broadcasting messages

``` shell
gradle run -P main=NewTask -P argv="rabbit1 ...."
```
### 4. Routing (Routing.kt)

Add more workers to the same queue, message will be distributed in the
round robin manner.
Receiving messages selectively using direct exchanges and routing keys.

### Tutorial 3
Functions:
- `emitLogDirect(severity, message)` - Publishes log with specific severity
- `receiveLogsDirect(subscriberName, severities)` - Subscribes to specific severities

``` shell
gradle run -P main=ReceiveLogs
```
Key concepts:
- Direct exchanges
- Routing keys
- Multiple bindings per queue

### 5. Topics (Topics.kt)

``` shell
gradle run -P main=EmitLog -P argv="rabbit1, msg1"
```
Receiving messages based on patterns using topic exchanges.

### Tutorial 4
Functions:
- `emitLogTopic(routingKey, message)` - Publishes with topic routing key
- `receiveLogsTopic(subscriberName, bindingKeys)` - Subscribes using patterns

``` shell
gradle run -P main="ReceiveLogsDirect" -P argv="info,error"
```
Key concepts:
- Topic exchanges
- Wildcard patterns (`*` = one word, `#` = zero or more words)
- Pattern-based routing

``` shell
gradle run -P main=EmitLogDirect"
```
### 6. RPC (RPC.kt)

### Tutorial 5
Request/reply pattern for remote procedure calls.

``` shell
gradle run -P main=ReceiveLogsTopic -P argv="anonymous.*"
```
Functions:
- `rpcServer()` - Processes Fibonacci number requests
- `rpcClient(n)` - Sends RPC request and waits for response

``` shell
gradle run -P main=EmitLogTopic -P argv="anonymous.info"
```
Key concepts:
- Callback queues
- Correlation IDs
- Reply-to pattern

### Tutorial 6
## Running Examples

In one shell:
These tutorials are designed as library functions. You can call them from your own code or tests. For example:

``` shell
gradle run -P main=RPCServer
```
```kotlin
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
// Example: Run work queue tutorial
launch { worker(this, "Worker-1") }
launch { worker(this, "Worker-2") }

In another shell:
delay(1000) // Give workers time to start

``` shell
gradle run -P main=RPCClient
newTask(this, "Task with work...")
}
```

## More Information

For detailed explanations of each tutorial, see the [Kourier documentation](https://kourier.dev/tutorials/).
34 changes: 34 additions & 0 deletions kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
plugins {
kotlin("multiplatform") version "2.1.21"
}

repositories {
mavenCentral()
}

kotlin {
// Examples can be executed with JVM or natively for macOS, Linux, or Windows
jvm()
macosArm64()
linuxX64()
mingwX64()

applyDefaultHierarchyTemplate()
sourceSets {
all {
languageSettings.apply {
optIn("kotlin.uuid.ExperimentalUuidApi")
}
}
val commonMain by getting {
dependencies {
api("dev.kourier:amqp-client:0.3.1")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
}
}
Binary file added kotlin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions kotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading