Exemplo n.º 1
0
 /// <summary>
 /// 执行消息队列SQL语句
 /// </summary>
 /// <param name="messageQueue">消息队列对象</param>
 public bool DoSqlExecute(SqlMessageQueue messageQueue, int reDoingCount)
 {
     if (messageQueue == null || messageQueue.paramters == null) return false;
     try
     {
         SqlParameter[] paramsAction = new SqlParameter[messageQueue.paramters.Length];
         for (int i = 0; i < messageQueue.paramters.Length; i++)
         {
             if (messageQueue.paramters[i] == null)
             {
                 continue;
             }
             SqlDbType dbtype = (SqlDbType)Enum.Parse(typeof(SqlDbType), messageQueue.paramters[i].iDBTypeValue.ToString(), true);
             paramsAction[i] = SqlParamHelper.MakeInParam(messageQueue.paramters[i].paramName, dbtype, messageQueue.paramters[i].isize, messageQueue.paramters[i].ivalue);
         }
         int result = 0;
         if (string.IsNullOrEmpty(messageQueue.ConnectionString))
         {
             result = SqlHelper.ExecuteNonQuery(ConfigContext.GetInstance().ConnctionString, messageQueue.commandType, messageQueue.commandText, paramsAction);
         }
         else
         {
             result = SqlHelper.ExecuteNonQuery(messageQueue.ConnectionString, messageQueue.commandType, messageQueue.commandText, paramsAction);
         }
         if (result > 1)
         {
             LogHelper.WriteDebug(string.Format("Message queue [{0} times] perform impact {1} line Sql:{2}", reDoingCount, result, messageQueue.commandText + ":parameter:" + messageQueue.commandType + GetMessagQueueParamterString(messageQueue)));
         }
         return true;
     }
     catch (SqlException sqlex)
     {
         LogHelper.WriteException(string.Format("Message queue [{0} times] to execute SQL statements SQLEX:{1};{2}", reDoingCount, sqlex.Message, messageQueue.commandText + ":parameter:" + messageQueue.commandType + GetMessagQueueParamterString(messageQueue)), sqlex);
     }
     catch (Exception ex)
     {
         LogHelper.WriteException(string.Format("Message queue [{0} times] SQL statement error:{1};{2}", reDoingCount, ex.Message, messageQueue.commandText + ":parameter:" + messageQueue.commandType + GetMessagQueueParamterString(messageQueue)), ex);
     }
     return false;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 写入异常处理队列
        /// </summary>
        private void WriteErrorMSMQ(MessageConfig msgConfig, SqlMessageQueue messageQueue)
        {
            try
            {
                if (msgConfig.IsErrorQueue)
                {
                    if (messageQueue != null)
                    {
                        SaveToLog(messageQueue);
                    }
                    return;
                }
                MessageQueue mq = CreateErrorMQ(msgConfig);

                Message message = new Message();
                message.Label = Convert.ToString(MSMQCmd.SendSQLCmd);
                message.Formatter = new XmlMessageFormatter(new Type[] { typeof(SqlMessageQueue) });
                message.Body = messageQueue;
                mq.Send(message, MessageQueueTransactionType.Automatic);

            }
            catch (Exception ex)
            {
                LogHelper.WriteException(string.Format("Write error exception handling queue:{0}", ex.Message), ex);
            }
        }
Exemplo n.º 3
0
        private void SaveToLog(SqlMessageQueue messageQueue)
        {
            string dirPath = messageQueue.DataBase;
            string jsonStr = ZyGames.Base.Common.JsonSerializer.Serialize(messageQueue);
            string root = @"D:\NLog\ErrorMSMQLog";
            try
            {
                root = AppDomain.CurrentDomain.BaseDirectory;
            }
            catch (Exception ex)
            {
            }
            string path = Path.Combine(root, "MSMQLog");
            if (!string.IsNullOrEmpty(dirPath)) path = Path.Combine(path, dirPath);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, string.Format("{0}_{1}.txt", messageQueue.CommandTable, Guid.NewGuid().ToString()));
            using (StreamWriter stream = File.CreateText(path))
            {
                stream.Write(jsonStr);
                stream.Flush();
            }
        }
Exemplo n.º 4
0
        private void ReDoSqlExecute(MSMQContext context, SqlMessageQueue messageQueue, int runTimes)
        {
            try
            {
                for (int i = 0; i < ConfigContext.GetInstance().ReDoExecuteCount; i++)
                {
                    runTimes++;
                    if (DoSqlExecute(messageQueue, runTimes))
                    {
                        LogHelper.WriteDebug(string.Format("Message queue [{0} times] re-execute Sql: {1} successfully!", runTimes, messageQueue.commandText));
                        return;
                    }
                }

                WriteErrorMSMQ(context.MsgConfig, messageQueue);
            }
            catch (Exception ex)
            {
                LogHelper.WriteException(string.Format("Queue reexecute {0} error:", context.MsgConfig.ManagerMessagePath), ex);
            }
        }
Exemplo n.º 5
0
        private void PutQueue(SqlMessageQueue sqlmq)
        {
            if (sqlmq == null) return;
            if (_distribeCollection.Count > 0)
            {
                int index = sqlmq.IdentityID % _distribeCollection.Count;
                SafedQueue<SqlMessageQueue> msQueue = GetQueue(index);

                while (msQueue != null && !msQueue.TryEnqueue(sqlmq))
                {
                    Thread.Sleep(SleepSeconds);
                }
                SetQueue(index, msQueue);
            }
            Interlocked.Exchange(ref writeCount, writeCount + 1);
            //TraceWriteLine(string.Format("写入ID:{0}队列数{1}", sqlmq.IdentityID, writeCount));
        }
Exemplo n.º 6
0
 private static string GetMessagQueueParamterString(SqlMessageQueue messageQueue)
 {
     StringBuilder sb = new StringBuilder();
     if (messageQueue == null) return sb.ToString();
     if (messageQueue.paramters == null) return sb.ToString();
     for (int i = 0; i < messageQueue.paramters.Length; i++)
     {
         if (messageQueue.paramters[i] == null)
         {
             continue;
         }
         if (sb.Length > 0) sb.Append(" and ");
         sb.AppendFormat("{0}={1}", messageQueue.paramters[i].paramName, messageQueue.paramters[i].ivalue);
     }
     return sb.ToString();
 }