How to filter messages on AMQ .NET client?

Solution Verified - Updated -

Environment

  • Red Hat AMQ
    • 7.x

Issue

  • How to use application properties on AMQ .NET client
  • How to consume messages that match with a string using AMQ .NET client

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 is a producer that shows how to use application properties to filter messages on AMQ .NET client:

using Amqp;
using Amqp.Framing;
using Amqp.Types;
using System;

namespace Example
{
    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] : "exampleFilter";

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

            Message msg = new Message("I can match a filter");
            msg.ApplicationProperties = new ApplicationProperties();
            msg.ApplicationProperties["sn"] = 100;

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

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

        }
    }
}

Also, see a simple receiver consuming only messages that match with the sn=100 string using application property:

using Amqp;
using Amqp.Framing;
using Amqp.Types;
using System;

namespace Example
{
    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] : "exampleFilter";

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

            Map filters = new Map();
            filters.Add(new Symbol("f1"), new DescribedValue(new Symbol("apache.org:selector-filter:string"), "sn = 100"));

            ReceiverLink receiver = new ReceiverLink(session, "receiver-", new Source() { Address = source, FilterSet = filters }, null);
            Message msg = receiver.Receive();
            Console.WriteLine(msg.ApplicationProperties.ToString());
            receiver.Accept(msg);

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

        }
    }
}

Check the following documentation: Class ApplicationProperties for detailed information. To get more examples go to: AMQP.Net Lite Examples and find for TestMethod_ReceiveWithFilter() 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.