Class AmqpMessagePublisher

java.lang.Object
org.mockserver.async.publish.AmqpMessagePublisher
All Implemented Interfaces:
MessagePublisher

public class AmqpMessagePublisher extends Object implements MessagePublisher
A MessagePublisher that delivers messages to an AMQP 0.9.1 broker (RabbitMQ) using the official com.rabbitmq:amqp-client library.

The AsyncAPI channel name does not map directly to an AMQP destination the way it does for Kafka topics or MQTT topics. Instead, the destination is derived from the channel's AmqpBinding (channels.<name>.bindings.amqp):

  • is: routingKey (default) — publish to the binding's exchange.name using the channel name (or the binding's explicit routingKey) as the AMQP routing key. When no exchange is named, the default exchange ("") is used with the channel name as the routing key (i.e. direct-to-queue by name).
  • is: queue — publish to the default exchange ("") using the binding's queue.name (or the channel name) as the routing key, which RabbitMQ routes to the like-named queue.

When a channel has no AMQP binding at all, the publisher falls back to publishing to the default exchange with the channel name as the routing key — mirroring the topic-name-as-destination behaviour of the Kafka/MQTT publishers.

For exchange-based channels, the exchange is declared (idempotently, once per publisher lifetime) before the first publish using the binding's exchange type (default direct) and durability. For queue-based channels the named queue is declared once. This makes the mock self-sufficient against a fresh broker.

Supported binding fields

is, exchange.name, exchange.type, exchange.durable, queue.name, queue.durable, and an explicit routingKey.

Deferred

exchange.autoDelete/vhost, queue.exclusive/autoDelete/vhost, and operation/message-level AMQP bindings (cc, bcc, deliveryMode, mandatory, priority, expiration, …). See AmqpBinding.
  • Constructor Details

    • AmqpMessagePublisher

      public AmqpMessagePublisher(String amqpUri, AsyncApiSpec spec)
      Connect to an AMQP broker from a connection URI (e.g. amqp://user:pass@host:5672/vhost) and resolve per-channel destinations from the given spec.
      Parameters:
      amqpUri - the AMQP connection URI
      spec - the loaded AsyncAPI spec (used to map channel names to AMQP bindings); may be null
    • AmqpMessagePublisher

      public AmqpMessagePublisher(String host, int port, String vhost, String username, String password, AsyncApiSpec spec)
      Connect to an AMQP broker from explicit connection parameters and resolve per-channel destinations from the given spec.
      Parameters:
      host - broker host
      port - broker port (5672 for plaintext)
      vhost - AMQP virtual host (e.g. /)
      username - broker username (may be null for the guest default)
      password - broker password (may be null for the guest default)
      spec - the loaded AsyncAPI spec; may be null
  • Method Details

    • publish

      public void publish(String channelName, String payload)
      Description copied from interface: MessagePublisher
      Publish the given payload to the specified channel with no key or headers.
      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channelName - the channel / topic name
      payload - the message payload (typically JSON)
    • publish

      public void publish(String channelName, String payload, PublishOptions options)
      Publish a message to the AMQP destination derived from the channel's binding.

      PublishOptions.getHeaders() (e.g. correlation-ID headers) are emitted as AMQP message headers. The Kafka key and MQTT qos/retain fields are ignored.

      Specified by:
      publish in interface MessagePublisher
      Parameters:
      channelName - the AsyncAPI channel name
      payload - the message payload (typically JSON)
      options - per-message publish options (may be null)
    • close

      public void close()
      Description copied from interface: MessagePublisher
      Release any resources held by this publisher (producer connections, etc.).
      Specified by:
      close in interface MessagePublisher