Class AmqpMessagePublisher
- All Implemented Interfaces:
MessagePublisher
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'sexchange.nameusing the channel name (or the binding's explicitroutingKey) 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'squeue.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 Summary
ConstructorsConstructorDescriptionAmqpMessagePublisher(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.AmqpMessagePublisher(String amqpUri, AsyncApiSpec spec) Connect to an AMQP broker from a connection URI (e.g. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Release any resources held by this publisher (producer connections, etc.).voidPublish the given payload to the specified channel with no key or headers.voidpublish(String channelName, String payload, PublishOptions options) Publish a message to the AMQP destination derived from the channel's binding.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.mockserver.async.publish.MessagePublisher
publish
-
Constructor Details
-
AmqpMessagePublisher
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 URIspec- 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 hostport- 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
Description copied from interface:MessagePublisherPublish the given payload to the specified channel with no key or headers.- Specified by:
publishin interfaceMessagePublisher- Parameters:
channelName- the channel / topic namepayload- the message payload (typically JSON)
-
publish
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 Kafkakeyand MQTTqos/retainfields are ignored.- Specified by:
publishin interfaceMessagePublisher- Parameters:
channelName- the AsyncAPI channel namepayload- the message payload (typically JSON)options- per-message publish options (may be null)
-
close
public void close()Description copied from interface:MessagePublisherRelease any resources held by this publisher (producer connections, etc.).- Specified by:
closein interfaceMessagePublisher
-