static void Main(string[] args) { using (var channel = MqHelper.GetConnection().CreateModel()) // 创建通道 { //定义队列 channel.QueueDeclare(queue: "NET", durable: true, exclusive: false, autoDelete: false, arguments: null); //公平分配(在处理并确认前一条消息之前,不要向队列发送新消息) channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); //事件基本消费者 var consumber = new EventingBasicConsumer(channel); consumber.Received += (sender, e) => { try { var user = JsonConvert.DeserializeObject <User>(Encoding.UTF8.GetString(e.Body)); //Incr var database = RedisManager.Instance.GetDatabase(); var flag = database.StringIncrement(user.Id.ToString()); if (flag == 1) { //用户的第一次请求,为有效请求 //下面开始入库,这里使用List做为模拟 Console.WriteLine($"{user.Id}标识为{flag} {user.Name}"); DapperHelper.Insert(new Person() { Id2 = user.Id.ToString(), Name = user.Name }); //添加入库标识 database.StringIncrement($"{user.Id.ToString()}入库"); Console.WriteLine("入库成功"); } //用户的N次请求,为无效请求,消息被消费 channel.BasicAck(e.DeliveryTag, false); } catch (Exception ex) { Console.WriteLine(ex.Message); } }; Console.WriteLine("开始工作"); //启动消费者 设置为手动应答消息 channel.BasicConsume("NET", false, consumber); Console.ReadKey(); } }
/// <summary> /// 消费队列 /// </summary> public static void ConsumeQueue() { using (var channel = MqHelper.GetConnection().CreateModel()) { channel.QueueDeclare("NET", true, false, false, null); var consumber = new EventingBasicConsumer(channel); channel.BasicQos(0, 1, false); consumber.Received += (sender, e) => { try { var user = JsonConvert.DeserializeObject <User>(Encoding.UTF8.GetString(e.Body)); var flag = RedisHelper.GetRedisClient().Incr(user.Id.ToString()); if (flag == 1) { //File.AppendAllText($"{System.AppDomain.CurrentDomain.BaseDirectory}/bin/info.txt", $"有效!{user.Id}的标识为{flag},姓名为{user.Name}\r\n"); //用户的第一次请求,为有效请求 //下面开始入库,这里使用List做为模拟 var dbContext = Model1.GetDbContext(); dbContext.Person.Add(new Person() { Id2 = user.Id.ToString(), Name = user.Name }); Task ts = dbContext.SaveChangesAsync(); ts.Wait(); //添加入库标识 RedisHelper.GetRedisClient().Incr($"{user.Id.ToString()}入库"); } //else // File.AppendAllText($"{System.AppDomain.CurrentDomain.BaseDirectory}/bin/info.txt", $"无效!{user.Id}的标识为{flag},姓名为{user.Name}\r\n"); //用户的N次请求,为无效请求 channel.BasicAck(e.DeliveryTag, false); } catch (Exception ex) { File.AppendAllText($"{System.AppDomain.CurrentDomain.BaseDirectory}/bin/log.txt", ex.Message); } }; channel.BasicConsume("NET", false, consumber); //停在这里 while (true) { } } // ReSharper disable once FunctionNeverReturns }
static void Main(string[] args) { using (var channel = MqHelper.GetConnection().CreateModel()) { channel.QueueDeclare("NET", true, false, false, null); var consumber = new EventingBasicConsumer(channel); channel.BasicQos(0, 1, false); consumber.Received += (sender, e) => { try { var user = JsonConvert.DeserializeObject <User>(Encoding.UTF8.GetString(e.Body)); var flag = RedisHelper.GetRedisClient().Incr(user.Id.ToString()); if (flag == 1) { //用户的第一次请求,为有效请求 //下面开始入库,这里使用List做为模拟 Console.WriteLine($"{user.Id}标识为{flag} {user.Name}"); var dbContext = new Model1(); dbContext.Person.Add(new Person() { Id2 = user.Id.ToString(), Name = user.Name }); Task ts = dbContext.SaveChangesAsync(); ts.Wait(); //添加入库标识 RedisHelper.GetRedisClient().Incr($"{user.Id.ToString()}入库"); Console.WriteLine("入库成功"); } //用户的N次请求,为无效请求 channel.BasicAck(e.DeliveryTag, false); } catch (Exception ex) { File.AppendAllText($"{System.AppDomain.CurrentDomain.BaseDirectory}/bin/log.txt", ex.Message); } }; Console.WriteLine("开始工作"); channel.BasicConsume("NET", false, consumber); Console.ReadKey(); } }
/// <summary> /// 处理队列预约消息 /// </summary> public void ExcuteCustomerApt() { var log = new Log("ExcuteCustomerApt"); var aptQueue = _configuration["AptQueue"]; try { using (var channel = _mqHelper.GetConnection().CreateModel()) { channel.QueueDeclare(aptQueue, true, false, false, null); consumber = new EventingBasicConsumer(channel); channel.BasicQos(0, 1, false); consumber.Received += (sender, c) => { try { var apt = JsonConvert.DeserializeObject <AptInfo>(Encoding.UTF8.GetString(c.Body)); var config = _wxHelper.GetBasConfig(apt.BG_NO); var redisNum = Convert.ToInt32(config.REDIS_NUM); var key = apt.orgNo + "-" + apt.APT_CLASS + "-" + apt.CUS_PHONE_NO + "-" + apt.APT_DATE.ToString("yyyy-MM-dd") + "-" + apt.APT_TIMESPAN; var redis = _redisHelper.GetRedisClient(redisNum); var flag = _redisHelper.GetRedisClient(redisNum).Incr(key); log = new Log("ExcuteCustomerApt/" + apt.BG_NO + ""); log.Write("开始处理队列消息"); redis.Expire(key, 120); if (flag == 1)//用户的第一次请求,为有效请求 { var aptNum = Convert.ToInt32(redis.GetValue(apt.orgNo + "-" + apt.APT_CLASS + "-" + apt.APT_DATE.ToString("yyyy-MM-dd") + "-" + apt.APT_TIMESPAN)); if (aptNum > 0 || apt.aptConfigNum == 0) { log.Write("客户预约手机号:" + apt.CUS_PHONE_NO + ""); var ret = InsertCRM_APT_Info(apt, log); if (!ret) { log.Write("客户:" + apt.CUS_PHONE_NO + ",预约失败"); } else { log.Write("客户:" + apt.CUS_PHONE_NO + ",预约成功"); if (aptNum != 0) { aptNum = aptNum - 1; redis.Set(apt.orgNo + "-" + apt.APT_CLASS + "-" + apt.APT_DATE.ToString("yyyy-MM-dd") + "-" + apt.APT_TIMESPAN, aptNum); log.Write("更改预约设置人数:" + aptNum + ""); } } } else { var ret = WxToErrMessage(apt, log); if (!ret) { log.Write("客户:" + apt.CUS_PHONE_NO + ",消息推送失败"); } log.Write("预约人数已满"); } } //用户的N次请求,为无效请求 channel.BasicAck(c.DeliveryTag, false); } catch (Exception ex) { log.Write(ex.Message); } }; channel.BasicConsume(aptQueue, false, consumber); System.Threading.Thread.Sleep(1000); } } catch (Exception ex) { log.Write("队列初始化异常:" + ex.Message + ""); } }