How to dynamically define an anycast routing type for an address when using AMQ .NET clients

Solution Verified - Updated -

Environment

  • Red Hat AMQ
    • 7.x

Issue

  • Setting anycast routing type for an address on .NET AMQ clients
  • Dynamically creating a single queue using .NET AMQ clients
  • Automatically creating a queue setting an anycast routing type

Resolution

Open the broker.xml file and set the anycastPrefix on the acceptor which the client is using as the example below shows:

<acceptor name="amqp">tcp://0.0.0.0:5672?protocols=AMQP;anycastPrefix=anycast://</acceptor>

Afterward, use an anycast prefix in the client before specifying the address name:

anycast://example

The example above will automatically create a single queue within the matching address example, in a point-to-point manner.

See a C# example code creating a producer and connecting to a broker to dynamically define an anycast routing type:

using System;
using Amqp;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {


           string    url = (args.Length > 0) ? args[0] :                       
                "amqp://guest:guest@127.0.0.1:5672";
            string target = (args.Length > 1) ? args[1] : "anycast://example";           
            int     count = (args.Length > 2) ? Convert.ToInt32(args[2]) : 10;  

            Address      peerAddr = new Address(url);                           
            Connection connection = new Connection(peerAddr);                   
            Session       session = new Session(connection);
            SenderLink     sender = new SenderLink(session, "send-1", target);  

            for (int i = 0; i < count; i++)
            {

                Message msg = new Message("Test " + i);                       
                sender.Send(msg);                                               
                Console.WriteLine("Test: " + msg.Body.ToString());
            }

            sender.Close();                                                     
            session.Close();
            connection.Close();
        }
    }
}

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments