ulysseskao
2016-05-01 8f5c8891aba521570fa63c4baf616e3a3c8d9526
CCSTrace/CCS/CCSMain.cs
@@ -19,7 +19,7 @@
    {
        private const string DbConfigFilename = "DBConfig.xml";
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private static CcsMain _instance;
        public static ArrayList ProcessCases = new ArrayList();
@@ -29,7 +29,6 @@
        private int _connectionCount = 1;
        private OracleConnection _mainConn = null;
        private SEventLog _mainLog = null;
        private SmartThreadPool _mainjobThreadPool = null;
        private readonly object _syncDbQueue = new object();
@@ -58,15 +57,17 @@
        public void Startup()
        {
            _mainLog = new SEventLog();
            Logger.Info("CCSMain Startup.");
            try
            {
                ReadXml();
                Logger.Info("CCSMain ReadXml Complete.");
                if (_mainConn == null)
                    _mainConn = CreateConnection();
                //MainConn = CreateMainConnection();
                Logger.Info("CCSMain has been connected database.");
                GlobalVariable.CcsCodelist = new CcsCodelist(_mainConn);
                GlobalVariable.EosCodelist = new EosCodelist(_mainConn);
@@ -78,18 +79,11 @@
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                _mainLog.Error(e.Message);
                if (GlobalVariable.ShowError)
                    _mainLog.Error(e.StackTrace);
                Logger.Error(e, e.Message);
                throw;
            }
            finally
            {
                _mainLog.Close();
            }
            CcsRecord mRecord = GetWaitRecord(_mainConn);
@@ -106,6 +100,7 @@
            stpStartInfo.MaxWorkerThreads = GlobalVariable.MaxThreadSize;
            stpStartInfo.MinWorkerThreads = GlobalVariable.MinThreadSize;
            _mainjobThreadPool = new SmartThreadPool(stpStartInfo);
            Logger.Info("Create Thread Pool.");
        }
        public void Shutdown()
@@ -116,8 +111,14 @@
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public void AcceptEvent(CcsRecord record)
        {
            /*
            WorkItemCallback workItemCallback = new WorkItemCallback(this.DoWork);
            _mainjobThreadPool.QueueWorkItem(workItemCallback, record);
            */
            int i = 0;
            int reConnectCount = 1;
@@ -134,7 +135,8 @@
                            if (mRecord != null)
                            {
                                OracleConnection conn = _dbQueue.Dequeue();
                                OracleConnection conn = null;
                                lock (_syncDbQueue) conn = _dbQueue.Dequeue();
                                ProcessCase(mRecord, conn);
                            }
                        }
@@ -142,8 +144,9 @@
                        break;
                    }
                }
                catch (Exception)
                catch (Exception e)
                {
                    Logger.Error(e, e.Message);
                    if (_mainConn.State == ConnectionState.Closed)
                    {
                        i++;
@@ -216,7 +219,7 @@
            }
            catch (XmlException xe)
            {
                Console.WriteLine(xe.StackTrace);
                Logger.Error(xe, xe.Message);
                reader?.Close();
            }
        }
@@ -237,14 +240,12 @@
            try
            {
                _mainLog = new SEventLog();
                if (conn.State.ToString().Equals("Closed"))
                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                transaction = conn.BeginTransaction();
                if (record.InsertDb(conn, transaction, _mainLog))
                if (record.InsertDb(conn, transaction))
                {
                    var eventQuery = new EventQuery
                    {
@@ -255,11 +256,11 @@
                    };
                    if (eventQuery.Insert(_mainLog, conn, transaction))
                    if (eventQuery.Insert(conn, transaction))
                        transaction.Commit();
                    else
                    {
                        if (transaction.Connection.State.ToString().Equals("Open"))
                        if (transaction.Connection.State == ConnectionState.Open)
                            transaction.Rollback();
                        throw new Exception("案件未受理成功。");
@@ -267,7 +268,7 @@
                }
                else
                {
                    if (transaction.Connection.State.ToString().Equals("Open"))
                    if (transaction.Connection.State == ConnectionState.Open)
                        transaction.Rollback();
                    throw new Exception("案件未受理成功。");
@@ -275,35 +276,21 @@
            }
            catch (OracleException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                _mainLog.Error(e.Message);
                Logger.Error(e, e.Message);
                if (GlobalVariable.ShowError)
                    _mainLog.Error(e.StackTrace);
                if (transaction != null && transaction.Connection.State.ToString().Equals("Open"))
                if (transaction != null && (transaction.Connection.State == ConnectionState.Open))
                    transaction.Rollback();
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                _mainLog.Error(ex.Message);
                Logger.Error(ex, ex.Message);
                if (GlobalVariable.ShowError)
                    _mainLog.Error(ex.StackTrace);
                if (transaction != null && transaction.Connection.State.ToString().Equals("Open"))
                if (transaction != null && (transaction.Connection.State == ConnectionState.Open))
                    transaction.Rollback();
                throw;
            }
            finally
            {
                _mainLog.Close();
            }
            return true;
@@ -391,11 +378,7 @@
                }
                catch (Exception e)
                {
                    Log(e.Message);
                    _mainLog.Error("無法取得等候處理CCS案件資料。錯誤訊息 = " + e.Message);
                    if (GlobalVariable.ShowError)
                        _mainLog.Error(e.StackTrace);
                    Logger.Error(e, e.Message);
                }
                finally
                {
@@ -405,7 +388,7 @@
            }
            if (ccsid.Length != 0)
                record = new CcsRecord(ccsid, conn, _mainLog);
                record = new CcsRecord(ccsid, conn);
            if (record != null)
            {
@@ -418,15 +401,15 @@
                try
                {
                    if (eventQuery.UpdateCaseStatus(_mainLog, conn, transaction))
                    if (eventQuery.UpdateCaseStatus(conn, transaction))
                    {
                        _mainLog.Info("更新EVENTQUERY的案件狀態為處理中.(CCSID = " + record.CcsId + ")");
                        Logger.Info("更新EVENTQUERY的案件狀態為處理中.(CCSID = " + record.CcsId + ")");
                        transaction.Commit();
                        ProcessCases.Add(record.CcsId);
                    }
                    else
                    {
                        _mainLog.Error("無法更新EVENTQUERY的案件狀態.(CCSID = " + record.CcsId + ")");
                        Logger.Error("無法更新EVENTQUERY的案件狀態.(CCSID = " + record.CcsId + ")");
                        if (transaction.Connection.State.ToString().Equals("Open"))
                            transaction.Rollback();
@@ -437,29 +420,12 @@
                    if (transaction.Connection.State.ToString().Equals("Open"))
                        transaction.Rollback();
                    Log(e.Message);
                    Logger.Error(e, e.Message);
                    record = null;
                }
            }
            return record;
        }
        private void Log(string message)
        {
            RecordLog pLog = null;
            try
            {
                pLog = new RecordLog(GlobalVariable.CcsListPath + "MAIN.txt");
                pLog.Error(message);
            }
            finally
            {
                if (pLog != null)
                {
                    pLog.Close();
                }
            }
        }
    }
}