Here is my attempt at a simpler C# client class (I normally develop Java, so
please excuse any missing C-sharpisms).  The point here is to propose my
idea, rather than providing a full-fledged client (which would require
greater understanding of qpid than I currently posses):

This is how a user to could subscribe to a topic:

QpidClient qpid = new QpidClient("<host>");
qpid.subscribe("md.lsps").onMessage += (m) => { Console.WriteLine("===" +
m["symbol"] + "==="); };

Here is the code that implements this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.apache.qpid.client;
using org.apache.qpid.transport;

namespace SharpOMS
{
    //Author: Shahbaz Chaudhary
    //License: Do what you like
    class QpidClient
    {
        public string host { get; private set; }
        public int port { get; private set; }

        private ClientSession session;

        public QpidClient(string host) : this(host, 5672) { }
        public QpidClient(string host, int port) : this(host, port,
"test","guest","guest"){}

        public QpidClient(string host, int port, string virtualhost, string
username, string password)
        {
            this.host = host;
            this.port = port;

            Client connection = new Client();
            connection.connect(host, port, virtualhost, username, password);
            session = connection.createSession(60000);//exipration?
        }

        public QpidEvent subscribe(string routingKey)
        {
            //somehow generate a random queue name
            string generatedQueueName = DateTime.Now.Ticks + "" + (new
Random()).Next(100000);
            return subscribe(generatedQueueName, routingKey);
        }
        
        public QpidEvent subscribe(string queueName, string routingKey)
        {
            return subscribe(queueName, routingKey, Option.AUTO_DELETE);
        }

        public QpidEvent subscribe(string queue , string routingKey, params
Option[] options)
        {
            QpidEvent qe = new QpidEvent();
            session.queueDeclare(queue, options);
            session.exchangeBind(queue, "amq.topic", routingKey);
            session.attachMessageListener(qe,queue);
            session.messageSubscribe(queue);
            return qe;
        }
    }

    public class QpidEvent : IMessageListener
    {
        public event Action<Dictionary<String, Object>> onMessage;

        public void messageTransfer(IMessage m)
        {
            if (onMessage != null) onMessage(m.ApplicationHeaders);
        }
    }

}

-- 
View this message in context: 
http://n2.nabble.com/c--message-retreival-without-resorting-to-encodings-and-bytes-readers--tp2222774p2223169.html
Sent from the Apache Qpid users mailing list archive at Nabble.com.


---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to