[rabbitmq-discuss] RabbitMQ C# client

Michel Polder michel.polder at quality-it.com
Mon Oct 6 14:14:18 BST 2008


I have a RabbitMQ client that connects to an OpenAMQ server and sends a
message using the BasicPublish function. The problem is that my RabbitMQ
client connects sucessfully but is disconnected from the OpenAMQ server when
sending a message. 
The client that listens for the message is written in C using the WireAPI
also disconnects on the amq_content_basic_get_body function call. I've attached
my test code below.

any idea what's going on?

thanks,

Michel

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
C listener
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include "base.h"
#include "amq_client_connection.h"
#include "amq_client_session.h"

int main (int argc, char *argv [])
{
        amq_client_connection_t *connection;
        amq_client_session_t    *session;
        icl_longstr_t           *auth_data;
        amq_content_basic_t             *content;
        char                    message_text [1024];
        size_t                  message_size;

        icl_system_initialise(0, NULL);

        auth_data = amq_client_connection_auth_plain("guest", "guest");
        connection = amq_client_connection_new("127.0.0.1:5672", "/", 
            auth_data, "testserver", 3, 30000);

        icl_longstr_destroy(&auth_data);

        session = amq_client_session_new(connection);

        //  Create a private queue
        amq_client_session_queue_declare (
                session,                        //  session
                0,                              //  ticket
                "listener",                     //  queue name
                FALSE,                          //  passive
                FALSE,                          //  durable
                FALSE,                          //  exclusive
                FALSE,                          //  auto-delete
                NULL);                          //  arguments

        //  Bind the queue to the exchange
        amq_client_session_queue_bind (
                session,                        //  session
                0,                              //  ticket
                "listener",                     //  queue
                "amq.direct",                   //  exchange
                "TEST",                         //  routing-key
                NULL);                          //  arguments

        //  Consume from the queue
        amq_client_session_basic_consume (
                session,                        //  session
                0,                              //  ticket
                "listener",                     //  queue
                NULL,                           //  consumer-tag
                TRUE,                           //  no-local
                TRUE,                           //  no-ack
                TRUE,                           //  exclusive
                NULL);                          //  arguments

        while (1)
        {
                while (1)
                {
                        //  Get next message
                        content = amq_client_session_basic_arrived(session);
                        if      (!content)
                        {
                                break;
                        }

                        //  Get the message body and write it to stdout
                        message_size = amq_content_basic_get_body(content,
                             (byte*) message_text, sizeof (message_text));

                        if (message_size)
                        {
                                message_text[message_size] = 0;
                                fputs (message_text, stdout);
                        }

                        //  Destroy the message
                        amq_content_basic_unlink (&content);
        }
        //  Wait while next message arrives
        amq_client_session_wait (session, 0);

        //  Exit the loop if Ctrl+C is encountered
        if      (!connection->alive)
        {
                break;
        }
    }

    //  Close the channel
    amq_client_session_destroy (&session);

    //  Close the connection
    amq_client_connection_destroy (&connection);

    //  Uninitialise system
    icl_system_terminate ();

    return 0;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
C# client
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
using System;
using System.IO;
using System.Text;

using RabbitMQ.Client;
using RabbitMQ.Client.Content;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.MessagePatterns;
using RabbitMQ.Util;

namespace RabbitMQ.Client.Examples {
    public class LogTail {
        public static int Main(string[] args) {
            try {
                string serverAddress = args[0];

                ConnectionFactory factory = new ConnectionFactory();
                ConnectionParameters connparams = factory.Parameters;
                connparams.AccessRequestConfig = AccessRequestConfig.Suppress;
                IProtocol protocol = Protocols.AMQP_0_9;               
                using (IConnection conn = factory.CreateConnection(protocol,
                    serverAddress))
                {
                    using (IModel ch = conn.CreateModel()) {
                        ushort ticket = ch.AccessRequest("/");
                        
                        byte[] messageBodyBytes = 
                            Encoding.UTF8.GetBytes("test message");
                        IBasicProperties props = ch.CreateBasicProperties();
                        props.ContentType = "text/plain";
                        props.DeliveryMode = 2;
                        ch.BasicPublish(ticket,
                                        "amq.direct",
                                        "TEST",
                                        props,
                                        messageBodyBytes);
                        return 0;
                    }
                }
            } catch (Exception e) {
                Console.Error.WriteLine(e);
                return 2;
            }
        }
    }
}






More information about the rabbitmq-discuss mailing list