forlayo
forlayo

Reputation: 1668

RabbitMQ parsing "client_properties" header from c#

I'm listening for connection changes through events pluging ("amq.rabbitmq.event", "connection.#").

It works properly so I'm adding at java side two additional parameters as clientproperties, to get the identity of the user that connects or disconnect.

However at c# side I can only access these properties as a list of byte[], and not sure on how to convert it to a Dictionary or so..

If I print all entries

if (args.BasicProperties.Headers.TryGetValue("client_properties", out object value))
            {
                var items = value as List<object>;
                foreach(var item in items)
                {
                    Console.WriteLine($"{item.GetType().ToString()}");
                    var bytes = item as byte[];
                    result.Add(Encoding.UTF8.GetString(bytes));
                }
            }

I can see this:

{<<"platform">>,longstr,<<"Java">>}
{<<"capabilities">>,table,[{<<"connection.blocked">>,bool,true},{<<"basic.nack">>,bool,true},{<<"exchange_exchange_bindings">>,bool,true},{<<"authentication_failure_close">>,bool,true},{<<"publisher_confirms">>,bool,true},{<<"consumer_cancel_notify">>,bool,true}]}
{<<"groupId">>,longstr,<<"1e6e935f0d4d9ec446d67dadc85cbafd10d1a095">>}
{<<"information">>,longstr,<<"Licensed under the MPL. See http://www.rabbitmq.com/">>}
{<<"version">>,longstr,<<"4.8.1">>}
{<<"copyright">>,longstr,<<"Copyright (c) 2007-2018 Pivotal Software, Inc.">>}
{<<"product">>,longstr,<<"RabbitMQ">>}

What kind of object format is and how can I parse this?:

{<<id>>,type,<<value>>}

Upvotes: 1

Views: 2199

Answers (1)

forlayo
forlayo

Reputation: 1668

Apparently ( as for an answer I got on Rabbit client google group for this questions ), client_properties is something that's not created to being read by the receiving party..

However is a really good way to have something like LWT ( Last Will and Testament ), then I am using it at the minute doing the parse by myself.

if (args.BasicProperties.Headers.TryGetValue("client_properties", out object value))
{
    var items = value as List<object>;
    foreach (var item in items)
    {
        var bytes = item as byte[];
        //{<<id>>, type, <<value>>}
        String itemStr = Encoding.UTF8.GetString(bytes);
        var parts = itemStr.Split(",");
        var key = CleanErlangString(parts[0]);
        var value = CleanErlangString(parts[2]);

        // Do things with key/value

    }
}

ClearErlangFunction

private static string CleanErlangString(string toClean)
{
    return toClean
        .Replace("{", "").Replace("}", "")
        .Replace("\"", "")
        .Replace("<<", "").Replace(">>", "");
}

What I am doing to use it as LWT, is setting a custom property on client side and then obtaining it while reading events at "amq.rabbitmq.event", "connection.#". With that I know who have disconnected and even process something as LWT with my core server.

I hope this helps someone :)

Upvotes: 1

Related Questions