Beispiel #1
0
        public static void SendEmailExcuteService(SysConsumer sysConsumer)
        {
            using (SqlConnection connection = DBConnectionMgr.GetUserConnection())
            {
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        //如果消息记录状态显示已经消费,跳出函数
                        if (sysConsumer.Status == SysConsumerService.SysMQ_CostStatus_Consumed)
                        {
                            return;
                        }

                        //业务执行消费
                        SysUserWelfare userWelware = new SysUserWelfare()
                        {
                            UserID     = sysConsumer.MsgCont,
                            WelfareNum = 10
                        };
                        SysUserWelfareDbSvc dbSvc = new SysUserWelfareDbSvc(connection);
                        dbSvc.Insert(userWelware, transaction);

                        //修改数据库消费状态为已消费。
                        SysConsumerDbSvc consumberDbSvc = new SysConsumerDbSvc(connection);
                        consumberDbSvc.UpdateStatus(sysConsumer.Id, SysConsumerService.SysMQ_CostStatus_Consumed, transaction);
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
Beispiel #2
0
        public static void RegUserServer(string userID, string topicID, string producerID)
        {
            SysUser user = new SysUser();

            user.Id     = userID;
            user.Status = 1;

            using (SqlConnection connection = DBConnectionMgr.GetUserConnection())
            {
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    new SysUserDbSvc(connection).Insert(user, transaction);
                    SysMessageQueue sysMQ = new SysMessageQueue()
                    {
                        Id         = Guid.NewGuid().ToString(),
                        Keys       = Guid.NewGuid().ToString(),
                        MsgCont    = userID,
                        Status     = OnsMQDefine.SysMessage_Status_UnSend,
                        Tags       = OnsMQDefine.MessageTag_TestLog,//tag 看业务需要,可以不传入,为查询方便,建议传入
                        ProducerID = producerID,
                        TopicID    = topicID
                    };
                    new SysMessageQueueDbSvc(connection).Insert(sysMQ, transaction);
                    transaction.Commit();
                }
            }
        }
Beispiel #3
0
 /// <summary>
 /// 发送消息到 aliMQ,同时修改系统发送状态
 /// </summary>
 /// <param name="sysMQ">系统MQ消息记录</param>
 public void SendMessage(SysMessageQueue sysMQ)
 {
     using (SqlConnection connection = DBConnectionMgr.GetUserConnection())
     {
         using (SqlTransaction transaction = connection.BeginTransaction())
         {
             SysMessageQueueDbSvc sysMQDbSvc = new SysMessageQueueDbSvc(connection);
             sysMQ.Status = OnsMQDefine.SysMessage_Status_Sent;
             sysMQDbSvc.UpdateStatus(sysMQ, transaction);
             //根据 Key 查询,如果OnsMQ服务器上补存在消息,重发
             //if (string.IsNullOrEmpty(GetMessageByKey(sysMQ.Keys)))
             //{
             //发送消息
             if (SendMessageToAliMQ(sysMQ.MsgCont, sysMQ.Tags, sysMQ.Keys))
             {
                 transaction.Commit();
             }
             else
             {
                 transaction.Rollback();
             }
             //}
             //else { transaction.Commit(); }
         }
     }
 }
Beispiel #4
0
        public void PullMessage()
        {
            List <OnsConsumerResult> list = PullMessageFromAli();

            while (list.Count > 0)
            {
                Dictionary <string, SysMessageQueue> sysMQDict       = new SysMessageQueueDbSvc().ListByKeyList(list.Select(t => t.key).ToList()).ToDictionary(t => t.Keys);
                Dictionary <string, SysConsumer>     sysConsumerDict = new SysConsumerDbSvc().ListByKeyList(ConsumerID, list.Select(t => t.key).ToList()).ToDictionary(t => t.Keys);

                foreach (var msg in list)
                {
                    string msgKey = msg.key;
                    //发送消息表,一定存在这条数据,如果不存在一定是非法添加消息
                    SysMessageQueue sysMQ;
                    if (!sysMQDict.TryGetValue(msgKey, out sysMQ))
                    {
                        continue;//非法消息
                    }

                    SysConsumer consumer;
                    //如果数据库记录已经消费,则执行消费阿里云 MQ 消费,删除消息。
                    if (sysConsumerDict.TryGetValue(msgKey, out consumer))
                    {
                        DeleteMessage(msg.msgHandle);
                        continue;
                    }

                    //客户消费消息,写入数据库记录
                    using (SqlConnection connection = DBConnectionMgr.GetUserConnection())
                    {
                        using (SqlTransaction transaction = connection.BeginTransaction())
                        {
                            try
                            {
                                consumer = new SysConsumer()
                                {
                                    SysMsgID   = sysMQ.Id,
                                    Keys       = msgKey,
                                    MQMsgID    = msg.msgId,
                                    MsgCont    = sysMQ.MsgCont,
                                    ConsumerID = ConsumerID
                                };

                                SysConsumerDbSvc dbSvc = new SysConsumerDbSvc(connection);
                                dbSvc.Insert(consumer, transaction);
                                transaction.Commit();

                                if (DeleteMessage(msg.msgHandle))
                                {
                                    Console.WriteLine(consumer.ConsumerID + sysMQ.MsgCont);
                                }
                            }
                            catch (SqlException ex)
                            {
                                //如果数据库已经存在,抛出重复插入异常
                                if (ex.ErrorCode == ErrorCode_Duplicate)
                                {
                                    DeleteMessage(msg.msgHandle);
                                }
                            }
                        }
                    }
                }
                //批处理,一次读取32
                list = PullMessageFromAli();
            }
        }