示例#1
0
 private void PublishMongoMessage(Captcha captcha, byte[] binary, string code)
 {
     Task.Run(() => {
         try
         {
             string captchaId = captcha.Ticket;
             if (code.Contains("Ticket") && code.Contains("Id") && code.Contains("Result"))
             {
                 string nCode = code;
                 captchaId    = JsonConvert.DeserializeObject <SharedResult>(nCode).Id;
                 code         = JsonConvert.DeserializeObject <SharedResult>(nCode).Result;
             }
             MongoDBHelper <Samples> mongoDBHelper = new MongoDBHelper <Samples>();
             Samples entity = new Samples()
             {
                 channel    = captcha.Channel,
                 ticket     = captcha.Ticket,
                 captchaId  = captchaId,
                 code       = code.ToUpper(),
                 image      = captcha.Binary,
                 md5        = Md5Util.GetMD5Hash(binary),
                 isError    = false,
                 createTime = DateTime.Now.AddHours(8)
             };
             mongoDBHelper.Insert(entity);
         }
         catch (Exception e)
         {
             if (e.Message.Contains("duplicate key error"))
             {
                 Console.WriteLine("Ignore current captcha, duplicate key error: " + code);
                 return;
             }
             else if (e.Message.Contains("System.Net.Sockets.SocketException"))
             {
                 Console.WriteLine("MongoDB connect timeout");
             }
             else
             {
                 Console.WriteLine("MongoDB exception: " + e.Message);
             }
         }
     });
 }
示例#2
0
        private static void BindQueue(string queue, Action <EventingBasicConsumer, IModel, string, Action <bool> > action)
        {
            try
            {
                tags         = new Dictionary <string, int>();
                receiveModel = Connections.First().CreateModel();
                sendModel    = Connections.First().CreateModel();

                receiveModel.ExchangeDeclare(exchange: MultiQueue.Exchange, type: "topic", durable: true, autoDelete: false, arguments: null);
                receiveModel.BasicQos(0, 1, false);
                receiveModel.QueueDeclare(queue: queue,
                                          durable: true,
                                          exclusive: false,
                                          autoDelete: false,
                                          arguments: null);
                receiveModel.QueueBind(queue, MultiQueue.Exchange, queue);
                var consumer = new EventingBasicConsumer(receiveModel);
                consumer.Received += (model, ea) =>
                {
                    string hash = string.Empty;
                    try
                    {
                        Console.WriteLine("Received message");

                        var _body    = ea.Body;
                        var _message = Encoding.UTF8.GetString(_body);

                        hash = Md5Util.GetMD5Hash(_body);

                        Stopwatch stopwatch = new Stopwatch();
                        stopwatch.Start();
                        action(((EventingBasicConsumer)model), sendModel, _message, (res) =>
                        {
                            try
                            {
                                IModel cm = ((EventingBasicConsumer)model).Model;
                                if (res)
                                {
                                    // 正常消息,确认消息
                                    cm.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                                    Console.WriteLine($"{ea.DeliveryTag} message is BasicAck");
                                }
                                else
                                {
                                    // 消费异常,抛弃消息
                                    //cm.BasicReject(ea.DeliveryTag, true);
                                    cm.BasicReject(ea.DeliveryTag, false);
                                    Console.WriteLine("Cureent message is BasicReject");
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("EventingBasicConsumer exception:" + e.Message);

                                if (IsOutMaxRetryCount(hash))
                                {
                                    Console.WriteLine("OutMaxRetryCount: BasicAck");
                                    ((EventingBasicConsumer)model).Model.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                                }
                            }
                            stopwatch.Stop();
                            Console.WriteLine($"{ea.DeliveryTag} Stopwatch is {stopwatch.Elapsed.TotalSeconds}/s");
                        });
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Received exception:" + e.ToString());
                    }
                };
                receiveModel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
            }
            catch (Exception e)
            {
                Console.WriteLine("BindQueue exception:" + e.ToString());
            }
        }