RabbitMQ-基于Command实现

1.采用发布订阅模式

消息发布方:

public static void SendCommand_Fanout()
{
    while (true)
    {
        JObject joMessage = new JObject();
        joMessage.Add("to", "all");
        joMessage.Add("from", "zsw");
        joMessage.Add("type", "Fanout");
        joMessage.Add("message", "hello everyone!");
        joMessage.Add("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

        JObject jo = new JObject();
        jo.Add("command", "chat");
        jo.Add("data", JsonHelper.GetClearString(joMessage));

        string message = JsonHelper.GetClearString(jo);
        string exchange = "commandExchange";
        try
        {
            using (IConnection connection = CreateMQConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange, ExchangeType.Fanout);

                    channel.BasicPublish(exchange, "", null, Encoding.UTF8.GetBytes(message));
                    Console.WriteLine(string.Format("Fanout publish message:{0}", message));
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        Thread.Sleep(3000);
    }
}

消息接收方:

//订阅模式
static void Subscribe()
{
    string exchange = "commandExchange";

    using (IConnection connection = CreateMQConnection())
    {
        using (IModel channel = connection.CreateModel())
        {
            channel.ExchangeDeclare(exchange, ExchangeType.Fanout);
            var queueName = channel.QueueDeclare();
            IDictionary props = new Dictionary<string, string>();
            channel.QueueBind(queueName, exchange, "", props);

            QueueingBasicConsumer consumer = new QueueingBasicConsumer(channel);
            channel.BasicConsume(queueName, false, consumer);

            while (true)
            {
                try
                {
                    BasicDeliverEventArgs e = (BasicDeliverEventArgs) consumer.Queue.Dequeue();

                    var message = Encoding.UTF8.GetString(e.Body);

                    channel.BasicAck(e.DeliveryTag, false);//确认删除

                    ParaseMessage(message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    break;
                }
            }
        }
    }
}

使用上篇文章建立的配置信息:

private static IConnection CreateMQConnection()
{
    ConnectionFactory factory = new ConnectionFactory();
    factory.Uri =  "amqp://zsw:123456@localhost:5672/mq_swzhou";

    return factory.CreateConnection();
}

2. 采用Direct方式,并使用主动获取消费

消息发布方:

public static void SendCommand_Direct()
{
    while (true)
    {
        JObject joMessage = new JObject();
        joMessage.Add("to", "all");
        joMessage.Add("from", "zsw");
        joMessage.Add("message", "hello everyone!");
        joMessage.Add("type", "Direct");
        joMessage.Add("time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

        JObject jo = new JObject();
        jo.Add("command", "chat");
        jo.Add("data", JsonHelper.GetClearString(joMessage));

        string message = JsonHelper.GetClearString(jo);

        string _requestExchange = "e1";
        string route = "r1";
        string queue = "q1";

        try
        {
            using (IConnection connection = CreateMQConnection())
            {
                using (IModel channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(_requestExchange, "direct");
                    channel.QueueDeclare(queue, true, false, false, null);
                    channel.QueueBind(queue, _requestExchange, route);

                    channel.BasicPublish(_requestExchange, route, null, Encoding.UTF8.GetBytes(message));
                    Console.WriteLine(string.Format("Direct publish message:{0}", message));
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        Thread.Sleep(3000);
    }
}

消息接收方:

//主动获取模式
private static void InitiativeGet()
{
    using (IConnection conn = CreateMQConnection())
    {
        using (IModel ch = conn.CreateModel())
        {
            while (true)
            {
                BasicGetResult res = ch.BasicGet("q1", false/*noAck*/);
                if (res != null)
                {
                    try
                    {
                        string message = UTF8Encoding.UTF8.GetString(res.Body);
                        ParaseMessage(message);
                        ch.BasicAck(res.DeliveryTag, false);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                else
                    continue;
            }
        }
    }
}

 

posted @ 2013-08-02 13:55  swzhou  阅读(374)  评论(0编辑  收藏  举报