IPWorks IoT 2020 Kotlin Edition

Questions / Feedback?

PublishMessage Method

Publishes a message.

Syntax

public fun publishMessage(channelName: String?, exchangeName: String?, routingKey: String?, mandatory: Boolean, immediate: Boolean)

Remarks

This method is used to publish the message specified by the Message property to the exchange named ExchangeName over the channel specified by ChannelName.

When this method is called, the message to publish is immediately added to the OutgoingMessages collection, and the MessageOut event fires once it has been sent (or, if the specified channel is in "publish confirmations" mode, after the server has acknowledged it).

Note that all AMQP 0.9.1 servers automatically bind all queues to their default exchange (which is always a direct exchange with no name) using each queue's name as the binding's routing key. This makes it easy to send a message to a specific queue without having to declare bindings; just call PublishMessage, pass empty string for ExchangeName, and the name of the desired queue for RoutingKey.

Note that messages published over channels which are in either transaction or "publish confirmations" mode may be handled differently than they would be on a channel in normal mode. Refer to the EnableTransactionMode and EnablePublishConfirms methods for more information about what each mode entails.

The server's default exchange may be specified by passing empty string for ExchangeName. Otherwise, ExchangeName must be a non-empty string consisting only of letters, digits, hyphens, underscores, periods, and colons. It must be no longer than 255 characters.

The RoutingKey parameter specifies the message's routing key. Whether this parameter needs to be non-empty, and what format it should have if so, depends on the type of exchange it is being sent to. Some exchange types may use information included with the message, such as its Headers. Refer to BindQueue for more information about how routing keys are used, and to your server's documentation for information on what it expects.

The Mandatory parameter controls what the server should do if the message can't be routed to any queue (either because it isn't eligible for any of the queues bound to the specified exchange because of how their bindings are configured, or because no queues are bound to the exchange in the first place). If True, the server will return the message, at which point the MessageReturned event will be fired. If False, the server will drop the message.

The Immediate parameter controls what the server should do if the message can't be immediately delivered to any consumer (either because it cannot be routed to a queue, or because the queues it can be routed to have no active consumers attached to them). If True, the server will return the message, at which point the MessageReturned event will be fired. If False, the server will queue the message if possible, or drop it if not.

An exception is thrown if no channel with the given ChannelName exists, or if the server returns an error because:

  • The value passed for ExchangeName fails one or more of the constraints described above.
  • No exchange named ExchangeName exists.
  • The message is rejected for some reason.

Note that in AMQP, server errors are grouped into "connection errors" and "channel errors", and both are fatal. That is, if the server returns a channel error, it will then close the channel which caused the error; and if it returns a connection error, it will then close the connection. The AMQPClassic component's Error Codes page includes AMQP's various connection and channel errors.

Publishing a Message

amqpc1.Message.Body = "Hello, world!";

// Publish a message to the server's default (no-name) exchange, using the name of a specific queue as the routing key.
amqpc1.PublishMessage("channel", "", "MyQueue", false, false);

// Publish a message to the "MyExchange" exchange, using the routing key "MyRoutingKey".
amqpc1.PublishMessage("channel", "MyExchange", "MyRoutingKey", false, false);

Copyright (c) 2021 /n software inc. - All rights reserved.
IPWorks IoT 2020 Kotlin Edition - Version 20.0 [Build 7941]