How to implement Request Reply using AMQ .NET client?

Solution Verified - Updated -

Environment

  • Red Hat AMQ
    • 7.x

Issue

  • How to implement Request Response using AMQ .NET client
  • How to use ReplyTo properties on AMQ .NET client
  • Setting ReplyTo properties on a message object

Resolution

Disclaimer: Links contained herein to external website(s) are provided for convenience only. Red Hat has not reviewed the links and is not responsible for the content or its availability. The inclusion of any link to an external website does not imply endorsement by Red Hat of the website or their entities, products or services. You agree that Red Hat is not responsible or liable for any loss or expenses that may result due to your use of (or reliance on) the external site or content.

This example shows how to handle a request message and receive a reply using ReplyTo properties on a message object:

using System;
using Amqp;
using Amqp.Framing;

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] : "request";

            Address peerAddr = new Address(url);
            Connection connection = new Connection(peerAddr);
            Session session = new Session(connection);

            Message msg = new Message("Request Response");
            msg.Properties = new Properties();
            msg.Properties.ReplyTo = "response";

            SenderLink sender = new SenderLink(session, "send-1", target);
            sender.Send(msg);
            sender.Close();

            session.Close();
            connection.Close();

        }
    }
}

As well a simple receiver getting the message from request address, displaying ReplyTo message property and sending the message to the response address:

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 source = (args.Length > 1) ? args[1] : "request";

            Address      peerAddr = new Address(url);
            Connection connection = new Connection(peerAddr);
            Session       session = new Session(connection);
            ReceiverLink receiver = new ReceiverLink(session, "recv-1", source);

            Message msg = receiver.Receive();
            Console.WriteLine("Received: " + msg.Properties.ReplyTo.ToString());
            receiver.Close();

            SenderLink sender = new SenderLink(session, "send-1", msg.Properties.ReplyTo);
            sender.Send(msg);
            sender.Close();

            session.Close();
            connection.Close();

        }
    }
}

For more details about message properties see the following documentation: Class Message. To get more examples go to: AMQP.Net Lite Examples and find for TestMethod_RequestResponse() method.

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