AMQP and messaging
A REST call needs the other side to be up and listening at that moment. A message queue doesn’t: the workflow hands a message to a broker and carries on, and whoever reads the queue picks it up when it’s ready, even hours later. That makes queues a good fit for telling other systems that something happened, such as a VM built or a request approved, and for taking work from systems that can’t call Orchestrator’s API themselves.
The AMQP plug-in talks to brokers that speak AMQP 0-9-1, which in practice means RabbitMQ. In this recipe you register a broker, set up an exchange and a queue with the built-in workflows, publish VM facts as JSON from an action, and read them back. There’s more shows how a message can start a workflow.
Getting ready
- A RabbitMQ server that Orchestrator can reach. This recipe uses
mq01.vmw.labon port5672, the virtual host/, and a RabbitMQ uservcfguidethat may configure, write, and read there. - The action
vmFactsToJsonfrom recipe 5.6, and the vCentervc01.vmw.lab. - A login with administrator rights and the action module
com.vcfguide.chapter5.
How to do it
Register the broker
- Go to Library > Workflows, search with the tags
amqpandconfiguration, and run Add a broker. - On the AMQP broker properties tab, enter the Name
mq01. - On the AMQP connection properties tab, enter the Host
mq01.vmw.lab, leave Port at5672and Virtual host at/, leave Use SSL off, and enter the uservcfguideand its password. Click Run. - Run Validate a broker and select
mq01. The run completes without an error. If it fails, fix the setting with Update a broker. - Go to Administration > Inventory > AMQP. The broker
mq01is listed.
Run dialog of Add a broker, AMQP connection properties tab: Host mq01.vmw.lab, Port 5672, Virtual host /, Use SSL off, User name vcfguide, password hidden.
Declare an exchange, a queue, and a binding
- Search with the tag
amqpand run Declare an exchange. Selectmq01, enter the namevcfguide.events, choose the type topic, set Durable to Yes, and leave automatic deletion at No. - Run Declare a queue. Select
mq01, enter the namevcfguide.vm-facts, set Durable to Yes, and leave Exclusive and Auto-delete at No. - Run Bind. Select
mq01, and enter the queuevcfguide.vm-facts, the exchangevcfguide.events, and the routing keyvm.facts.#. - In the RabbitMQ management UI, open the exchange
vcfguide.events. Its Bindings list shows the queue with the keyvm.facts.#.
Try it without code
- Run Send a text message. Select
mq01, enter the exchangevcfguide.events, the routing keyvm.facts.test, and the texthello from vco01. - Run Receive a text message. Select
mq01and enter the queuevcfguide.vm-facts. The run’s output ishello from vco01, and the queue is empty again.
Run of Receive a text message on mq01, queue vcfguide.vm-facts, Variables tab with the output hello from vco01.
Publish from an action
- In Library > Actions, create the action
getBrokerincom.vcfguide.chapter5, with the inputnameof typestringand the Return typeAMQP:Broker:
// Returns the AMQP broker registered under this name.var brokers = Server.findAllForType("AMQP:Broker", null);for (var i = 0; i < brokers.length; i++) { if (brokers[i].name === name) { return brokers[i]; }}throw "getBroker: no AMQP broker named " + name;- Create the action
publishVmFacts, with the inputsbrokerNameof typestringandvmsof typeArray/VC:VirtualMachine, and the Return typevoid:
// Publishes the facts of the VMs as one JSON message to vcfguide.events.var chapter5 = System.getModule("com.vcfguide.chapter5");var broker = chapter5.getBroker(brokerName);
var message = new AMQPMessage();message.bodyAsText = chapter5.vmFactsToJson(vms);
var headers = new Properties();headers.put("content_type", "application/json");headers.put("source", "vco01.vmw.lab");message.headers = headers;
broker.send("vcfguide.events", "vm.facts.report", message);System.log("Published facts of " + vms.length + " VMs to vcfguide.events on " + brokerName);- Run
publishVmFactswithmq01and two VMs. In the RabbitMQ management UI, the queuevcfguide.vm-factsnow holds one message.
Read from an action
- Create the action
receiveVmFacts, with the inputbrokerNameof typestringand the Return typeArray/Properties:
// Takes one message from vcfguide.vm-facts and returns the VMs in it,// or an empty array if the queue is empty.var chapter5 = System.getModule("com.vcfguide.chapter5");var broker = chapter5.getBroker(brokerName);
var message = broker.receive("vcfguide.vm-facts");if (!message) { System.log("No message waiting in vcfguide.vm-facts"); return [];}return chapter5.parseVmFactsJson(message.bodyAsText);- Run it with
mq01. The result is the two VMs from step 14. Run it again: the log says No message waiting.
Run of action com.vcfguide.chapter5/receiveVmFacts with mq01: result array of two Properties; beside it, the RabbitMQ management UI for vcfguide.vm-facts with the message rate graph showing one message in and one out.
Checked on 9.1.1 without a broker: AMQP:Broker is a known type for Server.findAllForType (an unknown type throws), and new AMQPMessage() takes bodyAsText and a Properties object as headers. The API Explorer lists send(exchange, routingKey, message) and receive(queue) with these arguments, and also sendAndReceive(exchange, routingKey, message, timeoutSeconds) for request and reply. AMQPMessage has no contentType property; besides headers, it has properties (a Properties object for the AMQP message properties), encoding, and body as bytes. The content_type header in step 13 is an ordinary header that your consumer reads, not the AMQP content type.
receive returns for an empty queue, and which key in message.properties sets the AMQP content type.How it works
Exchanges, queues, and bindings. In AMQP 0-9-1, a sender never writes to a queue. It sends a message to an exchange with a routing key, and the exchange copies it into every queue whose binding matches the key. A topic exchange matches keys made of words separated by dots: * stands for one word and # for any number of words, so vm.facts.# catches vm.facts.report and vm.facts.test. Other systems can bind their own queues to vcfguide.events with other keys, and each gets its own copy, without any change to the workflow.
Durable. A durable exchange or queue survives a restart of the broker. Messages survive too only if they’re sent as persistent; check the delivery mode your broker and readers expect before you rely on it.
The broker object. Add a broker stores the connection in Orchestrator and makes the broker an inventory object, so scripts find it by name with Server.findAllForType and never see the password. The broker object can also declare exchanges and queues itself, with declareExchange, declareQueue, and bind, which is how a workflow can set up what it needs on a fresh broker.
Receive takes one message. receive(queue) takes the next message off the queue, or nothing if it’s empty, and returns at once. It’s for a workflow that checks a queue now and then. To react to each message as it arrives, use a subscription, below.
There's more
Start a workflow when a message arrives. Run Subscribe to queues (tags amqp and configuration), enter the name vcfguide-vm-facts, select mq01, and select the queue vcfguide.vm-facts. The subscription appears under the broker in the inventory. Then create a workflow, Handle VM facts message, with one input, subscription of type AMQP:Subscription, and a scriptable task:
// Called by the policy for each message on the subscription's queues.var trigger = subscription.retrieveLastOnMessageTrigger();var message = subscription.retrieveMessage(trigger);var vms = System.getModule("com.vcfguide.chapter5").parseVmFactsJson(message.bodyAsText);System.log("Message with " + vms.length + " VMs received on " + subscription.name);Go to Library > Policies and create the policy VCF Guide VM facts. On the Definition tab, add a policy element for the subscription vcfguide-vm-facts, add its OnMessage event, and point the event at Handle VM facts message, with the policy element bound to subscription. Run the policy. Each message now starts a run of the workflow, which you’ll find under Activity > Policy Runs.
AMQP:Subscription. Check that retrieveLastOnMessageTrigger and retrieveMessage work as shown, and which user the policy’s runs use in 9.x (KB 446432).TLS. Set Use SSL to Yes and use RabbitMQ’s TLS port, usually 5671. Import the broker’s certificate first with Import a certificate from URL, rather than choosing Accept all certificates, which turns off the check. If RabbitMQ allows only TLS 1.3, connections fail with protocol_version until you run vracli vro properties set -k o11n.plugin.amqp.ssl-context-protocol -v TLSv1.3 on the appliance (KB 427111, recipe 2.8).
mq01.vmw.lab:5671, and that a broker with Use SSL and without Accept all certificates connects once it’s imported.Request and reply. broker.sendAndReceive(exchange, routingKey, message, timeoutSeconds) sends a message and waits for the answer, for systems that reply on a queue. Keep the timeout short: the workflow run holds a thread while it waits.
Kafka and other brokers. Orchestrator 9.x has no Kafka plug-in, and the AMQP plug-in speaks only AMQP 0-9-1, not AMQP 1.0. For Kafka, call a Kafka REST proxy with the HTTP-REST plug-in (recipe 5.2), or use a Kafka client library from a Node.js or Python action (Chapter 4).
See also
- 5.6 XML and JSON, for
vmFactsToJsonandparseVmFactsJson. - 3.7 Waiting tasks and custom events, for other ways to make a workflow wait for something outside.
- 2.8 System properties, for
vracli vro properties set. - Broadcom TechDocs (VCF 9.0 and 9.1): Using the AMQP plug-in, Configuring the AMQP plug-in, Add a broker (9.1), Subscribe to queues, Using the AMQP plug-in standard workflows, Managing policies, and Policy elements.
- Broadcom TechDocs (Aria Automation 8.12): Declare an exchange and Declare a queue.
- Broadcom KB 427111: RabbitMQ with TLS 1.3. KB 446432: policies after an upgrade to VCF 9.
- VMware’s TypeScript definitions for the AMQP plug-in, in build-tools-for-vmware-aria.
- RabbitMQ documentation: AMQP 0-9-1 model explained.
