[rabbitmq-discuss] Messaging and Serialization in RabbitMQ?

Demiss Zike habtdemis at gmail.com
Tue Jul 5 22:15:23 BST 2011


Hello,

I'm using c#. I have a serializable class called Measurement and want to
send an object of it. I serialized an object of Measurement and captured the
serialized object as byte[] and published it. My question is how can I
capture the published message (so that I can deserialize and use it)?

I've included codes on both ends:

public void publish(byte[] _serializedMsg)
{
// publisher code
    ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "localhost";
            using (IConnection connection = factory.CreateConnection())
            using (IModel channel = connection.CreateModel()) {
            channel.ExchangeDeclare("logs", "fanout");
            channel.BasicPublish("logs", queue, null, _serializedMsg);
            Console.WriteLine(" Message sent ");
        }
}



public void subscribe(string queue)
{
// subscriber code
ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "localhost";
            using (IConnection connection = factory.CreateConnection())
            using (IModel channel = connection.CreateModel()) {
            channel.ExchangeDeclare("logs", "fanout");

    string queue1 = channel.QueueDeclare();
            channel.QueueBind(queue1, "logs", "");
            QueueingBasicConsumer consumer = new
QueueingBasicConsumer(channel);
            channel.BasicConsume(queue1, true, consumer);

            Console.WriteLine(" Waiting for messages. To exit press
CTRL+C");
            while(true) {
                BasicDeliverEventArgs ea =
                    (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                byte[] body = ea.Body;
 Measurement m2 = new Measurement(20, 23323, 3.3);
m2 = BinarySerialization.BinaryDeSerialize( body );

                Console.WriteLine(" Message received: ");
Console.WriteLine("\nMeasurement ID: {0}", m2.id);
Console.WriteLine("Measurement Time: {0}", m2.time);
Console.WriteLine("Measurement Value: {0}", m2.value);
            }
        }
}


Thanks!
Demi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20110705/8a5220dc/attachment.htm>


More information about the rabbitmq-discuss mailing list