Confusion on AsyncAPI AMQP binding for subscribe operation

867 views Asked by At

I have a server which publishes rabbitmq messages on a exchange, so I tried to create following async api specs for this -

asyncapi: 2.3.0
info:
  title: Hello World
  version: 1.0.0
  description: Get Hello World Messages
  contact: {}
servers:
  local:
    url: amqp://rabbitmq
    description: RabbitMQ
    protocol: amqp
    protocolVersion: 0.9.1
defaultContentType: application/json
channels:
  hellow_world:
    subscribe:
      operationId: HelloWorldSubscriber
      description: 
      message:
        $ref: '#/components/messages/HellowWorldEvent'
      bindings:
        amqp:
          ack: true
          cc: ["hello_world_routing_key"]
        bindingVersion: 0.2.0
    bindings:
      amqp:
        is: routingKey
        exchange:
          name: hello_world_exchange
          type: direct
          durable: true
          vhost: /
        bindingVersion: 0.2.0
components:
  messages: 
    HellowWorldEvent:
      payload:
        type: object
        properties: []

Based on my understanding what it means is that MyApp will publish helloworldevent message on hello_world_exchange exchange using routing key hello_world_routing_key

Question -

  • How can consumer/subscriber can define which queue he will be using for consuming this message ?
  • Do I need to define new schema for subscriber and define queue element there ?
  • I can define another queue.** elements in channel element, but that can only specify 1 queue element, what if there are more than 1 subscriber/consumer, so how we can specify different queues for them ?

Reference - https://github.com/asyncapi/bindings/tree/master/amqp

1

There are 1 answers

1
avolpe On BEST ANSWER

I see you have not yet approved any of the responses as a solution. Is this still an issue? Are you using the AsyncAPI generator to generate your code stubs?

If so the generator creates a consumer/subscriber. If you want different processing/business logic you would generate new stubs and configure the queues they listen from. The queue is an implementation detail. I had an issue with the node.js generator for AMQP and RabbitMQ and so I decided to test the spec against Python to see if it was me or the generator.

Try the generator and you can try my gist: https://gist.github.com/adrianvolpe/27e9f02187c5b31247aaf947fa4a7360. I did do this for version 2.2.0 so hopefully it works for you.

I also did a test with the Python pika library however I did not assign a binding to the queue.

I noticed in the above spec you are setting your exchange type to Direct. You can have the same binding with multiple consumers with both Direct and Topic exchanges however you may want Topic as quoted from the RabbitMQ docs:

https://www.rabbitmq.com/tutorials/tutorial-five-python.html

Topic exchange is powerful and can behave like other exchanges.

When a queue is bound with "#" (hash) binding key - it will receive all the messages, regardless of the routing key - like in fanout exchange.

When special characters "*" (star) and "#" (hash) aren't used in bindings, the topic exchange will behave just like a direct one.

Best of luck!