Skip to content

Commit

Permalink
Merge pull request #44 from ba-st/fanout-binding
Browse files Browse the repository at this point in the history
Enhance RabbitMQ clients to support Fanout Exchanges
  • Loading branch information
jvanecek authored Oct 22, 2024
2 parents 28fe5ab + 5668c02 commit b9833b1
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 61 deletions.
42 changes: 42 additions & 0 deletions docs/tutorials/RabbitMQPublisher.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,45 @@ Accepts the following options:
| #enableDebuggingLogs | A boolean indicating whether to log debugging events | Optional | false |
| #extraClientProperties | A dictionary with keys and values to set the [client properties](https://www.rabbitmq.com/docs/connections#capabilities) |Optional | Empty |
| #retry | A block that can configure the internal `Retry` instance | Optional | `[]` |

## Usage

You need to instantiate the publisher with the options above and then send `#start` to ensure the [channel](https://www.rabbitmq.com/docs/channels) is open.

```smalltalk
| publisher |
publisher := RabbitMQPublisher configuredBy: [ :options |
options
at: #username put: 'guest';
at: #password put: 'guest';
at: #hostname put: 'localhost'
].
publisher start.
```

Once the publisher is started, you can use its protocol to send messages.

* Publish directly to a specific queue using the [default exchange](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-default).

```smalltalk
publisher publish: 'The message' to: 'the-queue'.
```

* Publish to a routing key through a [direct](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-direct) or [topic](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-topic) exchange.

```smalltalk
publisher publish: 'The message' to: 'a-routing-key' through: 'the-exchange'.
```

* Publish to all queues bound to a [fanout exchange](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-fanout).

```smalltalk
publisher broadcast: 'The message' toAllQueuesBindedTo: 'a-fanout-exchange'.
```

For proper shutdown and connection closure, you need to run:

```smalltalk
publisher stop.
```
35 changes: 35 additions & 0 deletions docs/tutorials/RabbitMQWorker.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,38 @@ Accepts the following options:
| #retry | A block that can configure the internal `Retry` instance | Optional | `[]` |
| #queueName | Queue name where to consume from | Mandatory | |
| #queueDurable | When false sets the [queue durability](https://www.rabbitmq.com/docs/queues#durability) to transient, otherwise will be durable | Optional | true |

## Usage

You need to instantiate the worker with the options above.

```smalltalk
| worker |
worker := RabbitMQWorker configuredBy: [ :options |
options
at: #hostname put: 'localhost';
at: #queueName put: aQueueName;
at: #extraClientProperties put: (
Dictionary new
at: 'process' put: 'aName';
yourself )
]
processingPayloadWith: [:message | message inspect ].
```

Before sending `#start`, you need to consider [bind the queue](https://www.rabbitmq.com/tutorials/tutorial-four-python#bindings) to an exchange if necessary.

```smalltalk
worker bindQueueTo: 'an-exchange-name' routedBy: 'a-routing-key'.
```

The #start method will block the socket, waiting for any new event, so it's recommended to fork the process and ensure that, before terminating, you unbind the queue and close the connection properly.

```smalltalk
workerProcess := [
[ worker start ] ensure: [
worker unbindQueueTo: 'an-exchange-name' routedBy: 'a-routing-key'.
worker stop
]
] fork
```
25 changes: 25 additions & 0 deletions source/Ansible-Deprecated-v3/RabbitMQPublisher.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Extension { #name : 'RabbitMQPublisher' }

{ #category : '*Ansible-Deprecated-v3' }
RabbitMQPublisher >> publish: aMessageCollection onQueueNamed: aQueueName [

self
deprecated: 'Use confirmPublicationWith:otherwise:'
transformWith:
'`@receiver publish: `@aMessageCollection onQueueNamed: `@aQueueName'
-> '`@receiver publishAll: `@aMessageCollection to: `@aQueueName'.

self publishAll: aMessageCollection to: aQueueName
]

{ #category : '*Ansible-Deprecated-v3' }
RabbitMQPublisher >> publishOnly: aMessage onQueueNamed: aQueueName [

self
deprecated: 'Use confirmPublicationWith:otherwise:'
transformWith:
'`@receiver publishOnly: `@aMessage onQueueNamed: `@aQueueName'
-> '`@receiver publish: `@aMessage to: `@aQueueName'.

self publish: aMessage to: aQueueName
]
Loading

0 comments on commit b9833b1

Please sign in to comment.