Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Unclear how to keep the consumer open after an exception is thrownΒ #177

@jndietz

Description

@jndietz

Documentation Issue

I think it would be good to document how to keep a consumer alive, or handle exceptions gracefully within a reactive stream. I came up with a solution but it doesn't seem correct.

Improvement Suggestion

All of the example I've found online show the receiver being closed after it has consumed events. I think a lot of people have use cases where there is a listener constantly running, and handles messages as they come in as opposed to just shutting down after they've all been consumed.

Additional context

    Disposable processAuditEvents() {
        final String AUDIT_QUEUE_NAME = "audit"

        receiver
                .consumeAutoAck(AUDIT_QUEUE_NAME)
                .subscribe(m -> {
                    log.debug("Received message {}", new String(m.body))
                    def wrapper = objectMapper.readValue(m.body, WrapperObject)
                    strategies
                            .find { it.handles(wrapper.payload) }
                            .handleEvent(wrapper.payload)
                }, { ex ->
                    log.error("Something bad happened: ${ex.message}")
                    processAuditEvents() // πŸ‘ˆ just re-register the consumer on error πŸ€·β€β™‚οΈ
                })
    }

Instead of using a Receiver, I ended up falling back to the annotation-driven style of listening, and this appears to work well enough for me.

    @RabbitListener(queues = "audit", ackMode = "MANUAL")
    Mono<Void> listen(CustomerActivityEventWrapper wrapper) {
        log.debug("Received message {}", wrapper.toString())
        strategies
                .find { it.handles(wrapper.payload) }
                .handleEvent(wrapper.payload)
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions