Working with AMQP

This recipe demonstrates how to use Orchestrator as a producer and consumer of AMQP message queues, how to create a subscription, and how to use Orchestrator policies to react to messages in the queues.

Getting ready

If you are totally new to AMQP, I suggest you start reading the How it works... section first.

An AMQP broker such as RabbitMQ is required. You can download it from http://www.rabbitmq.com . You can find a fast and easy Windows installation and configuration for RabbitMQ in the There's more... section.

How to do it...

As with SOAP, REST, and a lot of other modules, we can actually use the provided workflows. So in the following sections, we will make use of them.

Adding an AMQP host

To start with AMQP, we first need to add an AMQP broker to Orchestrator:

  1. Start the workflow by navigating to Library | AMQP | Configuration | Add a broker.
  2. Give the connection a name.
  3. Add the broker IP or FQDN; the default port is TCP 5672.
  4. Virtual host (vhost) is always in a freshly installed AMQP broker.
  5. You can use SSL (and probably should) and then enter the username and password that are granted access to the virtual host.
  6. After you have submitted the workflow, check out the inventory. Your host should now be visible:

    Adding an AMQP host

Defining exchanges, queues, and binds

To work with AMQP, we need exchanges and queues, so we will define them now:

  1. Run the workflow by navigating to Library | AMQP | Declare an exchange.
  2. Select the broker for which you want to add the exchange.
  3. Give the exchange a name such as systemExchange (this is used by vCloud Director).
  4. Select a type of exchange (refer to the How it works... section).
  5. Durable means that messages will be kept in the broker even if it restarts.
  6. AutoDelete will delete the exchange as soon as there are no more queues bound to it.

Now we will create a queue:

  1. Run the workflow by navigating to Library | AMQP | Declare a queue.
  2. Select the broker.
  3. Name the queue, such as vco.
  4. Select Durable.
  5. Exclusive means that only one client is allowed for this queue.
  6. Auto Delete in a queue means that the queue itself will be deleted as soon as there are no more subscribers to it.

Now we will bind the queue to an exchange:

  1. Run the workflow by navigating to Library | AMQP | Bind.
  2. Select the broker.
  3. Select the queue name and the exchange you want to bind it to.
  4. Enter a routing key (# is a wildcard for everything).

Sending messages

We will now send a message to the exchange:

  1. Run the workflow by navigating to Library | AMQP | Send a text message.
  2. Select the broker.
  3. Enter the exchange as well as the routing key (at this stage, anything will do).
  4. Enter a text message.
  5. The message is now stored in the queue until it is read.

Receiving messages

We will now read the message we sent to the queue:

  1. Run the workflow by navigating to Library | AMQP | Send a text message.
  2. Select the broker.
  3. Enter the queue you would like to receive the message from.
  4. The message body is in the out-parameter body.

If you are new to AMQP, then I would suggest that you read the How it works... section at this stage, where an example of the value of all this is provided.

Subscribing to a queue

Subscribing to a queue means that Orchestrator can use a policy to monitor this queue continually for new messages:

  1. Run the workflow by navigating to Library | AMQP | Configuration | Subscribe to queues.
  2. Enter a name for the subscription by which you can later identify it in the policy.
  3. Select the broker.
  4. Select the queue(s) you would like to subscribe to, such as vco.
  5. Your subscription is now visible under the AMQP infrastructure.

Using a policy as trigger

You should be aware of how Orchestrator policies work; refer to the Working with policies section in Chapter 8, Better Workflows and Optimized Working.

  1. Switch Orchestrator to the Administer mode.
  2. Click on policy templates (the yellow page with the green border icon).
  3. Navigate to Library | AMQP | Subscription and select Apply policy.
  4. Give the new policy a name and description.
  5. Select the AMQP subscription you would like to use and click on Submit.
  6. Orchestrator automatically switches to the Run mode, and you are automatically presented with the policy you have just created in the Policies section. Select Edit (the pencil icon).
  7. In the Scripting tab, expand the subscription and click on OnMessage.
  8. In the Workflow tab, click on Choose a workflow (the magnifying glass icon) and select the workflow to execute when a new message arrives. You can choose the example workflow 10.06.1 AMQP Worker.
  9. Save and close.

    Using a policy as trigger

  10. Now, start the policy (the green play button).
  11. You can use the example workflow, 10.6.3 Fill, to fill up the queue with messages. Watch the logs in the policy to see the execution happening.

How it works...

A message bus such as RabbitMQ can be compared to a mail server that stores e-mails until they are taken off the server. The Advanced Message Queuing Protocol (AMQP) defines a publisher/producer as someone sending messages, a broker (server) as the storage and process host, and a client/consumer as someone who receives messages.

Any message that is sent to the broker will be put in an exchange. The exchange will use the routing key to route the message into a queue. A consumer will read messages from a queue.

AMQP uses a virtual host or vHost (nothing to do with virtualization), which defines authentications, which means that you can have different vHosts that provide access to different users, as you cannot give access rights to exchanges or queues.

Here is a simple example using Orchestrator (example workflows 10.06.1 AMQP Worker and 10.6.2 Fill up and work):

How it works...

The example workflow 10.6.2 Fill up and work will fill up the systemExchange exchange from an attribute (an array of strings) using the # routing key. The # routing key will just forward everything into the vco queue. It will then start the workflow example 10.06.1 AMQP Worker twice asynchronously. Each of the Worker workflows will go and get a message from the queue, check whether the body is defined (not Null), and then sleep one second for each letter in the message.

When you run the workflow, you will see how the two worker workflows grab the next message in the queue and work on it until the queue is empty.

To understand routing and exchanges, we need to explain the four different kinds of exchange:

Exchange type

Description

Direct

This exchange routes messages, depending on their routing key, to a specific queue that is specified by the routing key in the exchange-queue binding.

Fanout

All messages sent to this exchange will be forwarded into all queues that are bound to it.

Headers

This ignores the routing key. It routes messages depending on the sender (as in, mail headers).

Topics

Routing is done using wildcards. There are two wildcards that AMQP understands: * means exactly one word and # means none to many words.

There's more...

Let's take a quick look at how to install and get AMQP going using RabbitMQ.

Installing RabbitMQ

The following steps will install RabbitMQ on a Windows host and configure a user to connect from the outside. Please note that this is a quick-and-dirty installation and configuration that is only okay for labs; however, it gets beginners going:

  1. Download RabbitMQ from http://www.rabbitmq.com (in this example, we will use the Windows installation).
  2. Download OTP from http://www.erlang.org/download.html , as RabbitMQ for Windows, requires this package.

    Tip

    Perform all the following steps on the same host. This is important, as RabbitMQ is configured to only accept localhost connections by default.

  3. Make sure TCP 5672 and 15672 are open in and outgoing.
  4. Install OTP with the defaults.
  5. Install RabbitMq with the defaults.
  6. Run the program by navigating to Start | RabbitMQ Server | Command Prompt (sbin dir).
  7. In the console that opens, type rabbitmq-plugins enable rabbitmq_management.
  8. Open a browser and browse http://localhost:15672.
  9. The default user is guest and the password is guest.
  10. In the RabbitMQ management, click on Admin.
  11. Enter a new username (for example, testuser) as well as a password. Add the tag Admin and click on Add user.
  12. The user is created. Now, click on the user, as shown in the following screenshot.
  13. The details of the user are displayed. Click on Set permission.

You are now able to connect from the outside to RabbitMQ and use it:

Installing RabbitMQ

See also

Check out the AMQP example that comes with vRO7.

AMQP basics:

There's a worthwhile article on how to use AMQP, vCloud Director, and Orchestrator together at:

http://www.vcoteam.info/articles/learn-vco/179-configure-the-amqp-plug-in.html .

The example workflows are as follows:

  • 10.06.1 AMQP Worker
  • 10.06.2 Fill up and work
  • 10.06.3 Fill
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset