RabbitMQWorker

March 24, 2026 ยท View on GitHub

This object will connect to an AMQP channel and knows how to consume messages from a specified queue for processing.

Accepts the following options:

Attribute nameDescriptionOptional/MandatoryDefault value
#hostnameHostname of the RabbitMQ brokerOptionallocalhost
#portPort number of the RabbitMQ brokerOptional5672
#usernameUsername of the RabbitMQ brokerOptionalguest
#passwordUsername of the RabbitMQ brokerOptionalguest
#maximumConnectionAttempsAmount of retries when connecting to the broker failsOptional3
#timeSlotBetweenConnectionRetriesInMsTime duration between retry attempts determined by using the exponential back off algorithmOptional3000
#enableDebuggingLogsA boolean indicating whether to log debugging eventsOptionalfalse
#extraClientPropertiesA dictionary with keys and values to set the client propertiesOptionalEmpty
#retryA block that can configure the internal Retry instanceOptional[]
#queueNameQueue name where to consume fromMandatory
#queueDurableWhen false sets the queue durability to transient, otherwise will be durableOptionaltrue

Usage

You need to instantiate the worker with the options above.

| 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 to an exchange if necessary.

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.

workerProcess := [
    [ worker start ] ensure: [
        worker unbindQueueTo: 'an-exchange-name' routedBy: 'a-routing-key'.
        worker stop
        ]
] fork