码迷,mamicode.com
首页 > 其他好文 > 详细

RabbitMq-初步使用

时间:2017-12-19 15:21:20      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:creat   src   nec   void   declare   lis   eve   event   bsp   

技术分享图片

/// <summary>
/// 消息发送
/// </summary>
public static class Send
{
    public static void BasicPublish(string HostName = "localhost",string queue= "hello", string msgContent = "Hello World!",string exchange="", string routingKey = "hello")
    {
        try
        {
            var factory = new ConnectionFactory() { HostName = HostName };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    //创建一个名称为hello的消息队列
                    channel.QueueDeclare(queue: queue,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    //string msg = "Hello World!";//传递的消息内容
                    var body = Encoding.UTF8.GetBytes(msgContent);
                    //开始传递
                    channel.BasicPublish(exchange: exchange,
                                         routingKey: routingKey,
                                         basicProperties: null,
                                         body: body);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
/// <summary>
/// 消息接收
/// </summary>
public static class Receive
{
    public static string BasicConsume(string HostName = "localhost", string queue = "hello")
    {
        string result = "";
        try
        {
            var factory = new ConnectionFactory() { HostName = HostName };
            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: queue,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    var consumer = new EventingBasicConsumer(channel);

                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var msg = Encoding.UTF8.GetString(body);
                    };
                    result = channel.BasicConsume(queue: queue,
                                         autoAck: true,
                                         consumer: consumer);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return result;
    }
}

 

RabbitMQ.Send.BasicPublish(
    //HostName:"www.baidu.com",
    HostName: context.Request.Url.Authority,
    queue: "SmartGroup",
    msgContent: jsonTea.ToString(),
    exchange: "",
    routingKey: "");
string strJosn = RabbitMQ.Receive.BasicConsume(
    //HostName: "www.baidu.com",
    HostName: Request.Url.Authority,
    queue: "SmartGroup");

 

RabbitMq-初步使用

标签:creat   src   nec   void   declare   lis   eve   event   bsp   

原文地址:http://www.cnblogs.com/zhyue93/p/rabbitmq_1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!