falconair wrote:
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"] + "==="); };

Looks good to me.
Don't you want to let the user access the message body? Maybe we can bind the message body with the key "messageBody"?

Arnaud


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);
        }
    }

}



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

Reply via email to