Bladeren bron

线程池和核心task日志记录

1062167724@qq.com 6 jaren geleden
bovenliggende
commit
08e7d9372a

+ 39 - 10
parkMonitor/Database2/AbstractCommand.cs

@@ -8,25 +8,35 @@ using MySql.Data.MySqlClient;
 
 namespace parkMonitor.Database2
 {
+    /// <summary>
+    /// sqlCommand抽象类
+    /// </summary>
     abstract class AbstractCommand
     {
         public string sql { set; get; }
         public List<string> strs { set; get; }
 
         public Connection conn { set; get; }
-
+        /// <summary>
+        /// 抽象类的构造函数
+        /// </summary>
         public AbstractCommand()
         {
             sql = null;
             conn = null;
         }
-
+        /// <summary>
+        /// 类的构造函数
+        /// </summary>
         public AbstractCommand(string sql, Connection conn)
         {
             this.sql = sql;
             this.conn = conn;
         }
-
+        /// <summary>
+        /// 创建sqlCommand
+        /// </summary>
+        /// <returns></returns>
         protected MySqlCommand CreateCommand()
         {
             MySqlCommand cmd = null;
@@ -41,6 +51,10 @@ namespace parkMonitor.Database2
             }
             return cmd;
         }
+        /// <summary>
+        /// 根据连接创建sqlCommand
+        /// </summary>
+        /// <returns></returns>
         protected MySqlCommand CreateCommand(string sql, MySqlConnection mySqlConnection)
         {
             MySqlCommand cmd = null;
@@ -65,7 +79,7 @@ namespace parkMonitor.Database2
     }
 
     /// <summary>
-    /// 查询
+    /// 查询数据库Command
     /// </summary>
     class QueryCommand : AbstractCommand
     {
@@ -88,7 +102,7 @@ namespace parkMonitor.Database2
     }
 
     /// <summary>
-    /// 插入更新
+    /// 插入更新数据库Command
     /// </summary>
     class NoQueryCommand : AbstractCommand
     {
@@ -110,7 +124,7 @@ namespace parkMonitor.Database2
         }
     }
     /// <summary>
-    /// 统计个数
+    /// 统计个数Command
     /// </summary>
     class ScalarCommand : AbstractCommand
     {
@@ -137,7 +151,7 @@ namespace parkMonitor.Database2
         }
     }
     /// <summary>
-    /// 存储过程执行
+    /// 存储过程执行Command
     /// </summary>
     class StoreProcedureCommand : AbstractCommand
     {
@@ -153,22 +167,35 @@ namespace parkMonitor.Database2
     }
 
     /// <summary>
-    /// 事务操作
+    /// 数据库事务操作Command
     /// </summary>
     class TransactionCommand : AbstractCommand
     {
         public List<string> strs { set; get; }
+        /// <summary>
+        /// 实务操作类的构造函数
+        /// </summary>
         public TransactionCommand()
         {
             sql = null;
             conn = null;
             strs = null;
         }
+        /// <summary>
+        /// 实务操作类的构造函数
+        /// </summary>
+        /// <param name="sql"></param>
+        /// <param name="conn"></param>
+        /// <param name="strs"></param>
         public TransactionCommand(string sql, Connection conn, List<string> strs)
             : base(sql,conn)
         {
             this.strs = strs;
         }
+        /// <summary>
+        /// 事务执行
+        /// </summary>
+        /// <returns></returns>
         public override object ExecuteSqlCommand()
         {
             int result = 0;
@@ -229,10 +256,12 @@ namespace parkMonitor.Database2
         }
     }
     /// <summary>
-    /// 枚举类型
+    /// Command枚举类型
     /// </summary>
     enum CommandTypes { QUERYCOMMAND, NOQUERYCOMMAND, SCALARCOMMAND, TRANSACTIONCOMMAND, STOREPROCEDURECOMMAND };
-
+    /// <summary>
+    /// 事务执行结构
+    /// </summary>
     public class TransactionResult
     {
         public int result { get; set; }

+ 31 - 13
parkMonitor/Database2/ConnectionPoolManager.cs

@@ -12,6 +12,9 @@ using parkMonitor.entity;
 
 namespace parkMonitor.Database2
 {
+    /// <summary>
+    /// 连接池管理
+    /// </summary>
     class ConnectionPoolManager
     {
         //public static BlockingQueue bq = new BlockingQueue();
@@ -53,7 +56,7 @@ namespace parkMonitor.Database2
             for (int i = 0; i < max; i++)
             {
                 Connection conn = DatabaseConnPoolFactory.CreateConnection(connectionString);
-                if (conn!=null&&conn.mySqlConnFlag)
+                if (conn != null && conn.mySqlConnFlag)
                 {
                     bq.Enqueue(conn);
                     Console.WriteLine("连接成功入队");
@@ -68,9 +71,9 @@ namespace parkMonitor.Database2
         public static Connection GetConnection(BlockingQueue bq)
         {
             Connection connection = (Connection)bq.Dequeue();
-            if (connection!=null&&!CheckConnection(connection, bq))
+            if (connection != null && !CheckConnection(connection, bq))
             {
-                connection.mySqlConnFlag = false;                
+                connection.mySqlConnFlag = false;
                 while (!connection.mySqlConnFlag)
                 {
                     connection.Dispose();
@@ -81,10 +84,16 @@ namespace parkMonitor.Database2
             Console.WriteLine("连接成功出队");
             return connection;
         }
+        /// <summary>
+        /// 获取连接,超时返回null
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="millisecondsTimeout"></param>
+        /// <returns></returns>
         public static Connection GetConnection(BlockingQueue bq, int millisecondsTimeout)
         {
             object conn = null;
-            Connection connection = null;            
+            Connection connection = null;
             bq.Dequeue(out conn, millisecondsTimeout);
             connection = (Connection)conn;
             Console.WriteLine("连接出队检测");
@@ -130,7 +139,7 @@ namespace parkMonitor.Database2
             InitConnPooling(max, connectionString);
         }
         /// <summary>
-        /// 检查连接是否可用,3<=连接数<=10
+        /// 检查连接池连接是否可用,3<=连接数<=10        
         /// </summary>
         /// <param name="min"> min = 3</param>
         /// <param name="max">max = 10</param>
@@ -149,7 +158,7 @@ namespace parkMonitor.Database2
             {
                 for (int i = 0; i < total; i++)
                 {
-                    conn = GetConnection(bq,1000);
+                    conn = GetConnection(bq, 1000);
                     CheckConnection(conn, bq);
                     ReleaseConnection(conn, bq);
                 }
@@ -168,19 +177,28 @@ namespace parkMonitor.Database2
         {
             bq.Enqueue(conn);
         }
+        /// <summary>
+        /// 检查连接
+        /// </summary>
+        /// <param name="conn"></param>
+        /// <param name="bq"></param>
+        /// <returns></returns>
         public static bool CheckConnection(Connection conn, BlockingQueue bq)
         {
             bool isUseful = true;
             try
             {
                 string sql = "select count(*) from garage";
-                DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
-                object result = access.GetResult();
-                if (result != null)
+                if (conn != null)
                 {
-                    int r = Convert.ToInt32(result);
-                    isUseful = true;
-                    Console.WriteLine("连接测试正常");
+                    DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+                    object result = access.GetResult();
+                    if (result != null)
+                    {
+                        int r = Convert.ToInt32(result);
+                        isUseful = true;
+                        Console.WriteLine("连接测试正常");
+                    }
                 }
             }
             catch (Exception ex)
@@ -189,7 +207,7 @@ namespace parkMonitor.Database2
                 {
                     conn.Dispose();
                 }
-                AddConnToPooling(1,remoteConf);
+                AddConnToPooling(1, remoteConf);
                 Console.WriteLine("连接检测失败,重新添加一个连接入队");
                 isUseful = false;
                 Console.WriteLine(ex.Message);

+ 24 - 4
parkMonitor/Database2/DBAccess.cs

@@ -6,6 +6,9 @@ using System.Threading.Tasks;
 
 namespace parkMonitor.Database2
 {
+    /// <summary>
+    /// 数据库访问类
+    /// </summary>
     class DBAccess
     {
         public string sql { set; get; }
@@ -13,12 +16,17 @@ namespace parkMonitor.Database2
         public CommandTypes commType { set; get; }
 
         public List<string> strs { set; get; }
+        /// <summary>
+        /// 类的构造函数
+        /// </summary>
         public DBAccess()
         {
             sql = null;
             conn = null;
         }
-
+        /// <summary>
+        /// 类的构造函数
+        /// </summary>
         public DBAccess(string sql, Connection conn, CommandTypes commType)
         {
             this.sql = sql;
@@ -26,7 +34,9 @@ namespace parkMonitor.Database2
             this.commType = commType;
 
         }
-
+        /// <summary>
+        /// 类的构造函数
+        /// </summary>
         public DBAccess(Connection conn, CommandTypes commType, List<string> strs)
         {
             this.sql = null;
@@ -34,7 +44,9 @@ namespace parkMonitor.Database2
             this.commType = commType;
             this.strs = strs;
         }
-
+        /// <summary>
+        /// 类的构造函数
+        /// </summary>
         public DBAccess(string sql, Connection conn, CommandTypes commType, Dictionary<string, string> args)
         {
 
@@ -43,6 +55,10 @@ namespace parkMonitor.Database2
             this.commType = commType;
 
         }
+        /// <summary>
+        /// 数据库查询、插入、更新处理
+        /// </summary>
+        /// <returns></returns>
         public object GetResult()
         {
             object result = null;
@@ -59,7 +75,11 @@ namespace parkMonitor.Database2
             }
             return result;
         }
-
+        /// <summary>
+        /// 数据库事务处理
+        /// </summary>
+        /// <param name="strs"></param>
+        /// <returns></returns>
         public TransactionResult DealTransaction(List<string> strs)
         {
             TransactionResult result = null;

+ 14 - 2
parkMonitor/Database2/DBOperation.cs

@@ -14,6 +14,9 @@ namespace parkMonitor.Database2
     /// </summary>
     public class DBOperation
     {
+        /// <summary>
+        /// TIMEOUT超时时间
+        /// </summary>
         public const int TIMEOUT = 120000;
         /// <summary>
         /// 查询车库表获得剩余车位数
@@ -582,7 +585,11 @@ namespace parkMonitor.Database2
             return flag;
         }
 
-
+        /// <summary>
+        /// 创建连接并获取有效连接
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <returns></returns>
         private static Connection CreateConnAndGetValidConn(BlockingQueue bq)
         {
             Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
@@ -596,7 +603,12 @@ namespace parkMonitor.Database2
             }
             return conn;
         }
-
+        /// <summary>
+        /// 清除超时连接
+        /// </summary>
+        /// <param name="ip"></param>
+        /// <param name="TimeOut"></param>
+        /// <param name="bq"></param>
         public static void KillTimeOutConnection(string ip, int TimeOut, BlockingQueue bq)
         {
             object result = null;

+ 20 - 19
parkMonitor/Database2/DatabaseConnPoolFactory.cs

@@ -7,8 +7,16 @@ using MySql.Data.MySqlClient;
 
 namespace parkMonitor.Database2
 {
+    /// <summary>
+    /// 创建数据库连接的简单工厂
+    /// </summary>
     class DatabaseConnPoolFactory
     {
+        /// <summary>
+        /// 根据连接字符串创建连接
+        /// </summary>
+        /// <param name="connectionString"></param>
+        /// <returns></returns>
         public static Connection CreateConnection(string connectionString)
         {
             MySql.Data.MySqlClient.MySqlConnection conn = null;
@@ -66,28 +74,15 @@ namespace parkMonitor.Database2
                 return connection;
             }
         }
-        public static MySqlCommand CreateCommand(string sql,Connection conn) 
-        {
-            MySqlCommand cmd = null;
-            try
-            {
-                cmd = new MySqlCommand(sql, conn.mySqlConnection);
-                return cmd;
-            }
-            catch (MySqlException)
-            {
-                Console.WriteLine("数据库连接异常");
-            }
-            catch
-            {
-                Console.WriteLine("sql语句异常");
-            }
-            return null;
-        }
     }
-
+    /// <summary>
+    /// 数据库连接类
+    /// </summary>
     public class Connection
     {
+        /// <summary>
+        /// 类的构造函数
+        /// </summary>
         public Connection()
         {
             mySqlConnection = null;
@@ -97,6 +92,9 @@ namespace parkMonitor.Database2
 
         public System.Data.ConnectionState state { get; set; }
         public bool mySqlConnFlag { get; set; }
+        /// <summary>
+        /// 连接关闭
+        /// </summary>
         public void Close()
         {
             try
@@ -111,6 +109,9 @@ namespace parkMonitor.Database2
                 Console.WriteLine(ex.Message);
             }
         }
+        /// <summary>
+        /// 连接dispose
+        /// </summary>
         public void Dispose()
         {
             try

+ 8 - 0
parkMonitor/Database2/SimpleCommandFactory.cs

@@ -6,8 +6,16 @@ using System.Threading.Tasks;
 
 namespace parkMonitor.Database2
 {
+    /// <summary>
+    /// 创建sqlCommand执行方法简单工厂
+    /// </summary>
     class SimpleCommandFactory
     {
+        /// <summary>
+        /// 根据类型创建相应Command执行方法
+        /// </summary>
+        /// <param name="type"></param>
+        /// <returns></returns>
         public static AbstractCommand CreateCommandAndExcute(CommandTypes type)
         {
             AbstractCommand absCommands = null;

+ 15 - 0
parkMonitor/Database2/Vehicle.cs

@@ -11,10 +11,25 @@ namespace parkMonitor.Database2
     /// </summary>
     public class Vehicle
     {
+        /// <summary>
+        /// 车辆表车位ID
+        /// </summary>
         public int parkingSpaceID { get; set; }
+        /// <summary>
+        /// 车辆表车库ID
+        /// </summary>
         public int garageID { get; set; }
+        /// <summary>
+        /// 车辆测量前轮距
+        /// </summary>
         public int frontwheelbase { get; set; }
+        /// <summary>
+        /// 车辆测量后轮距
+        /// </summary>
         public int rearwheelbase { get; set; }
+        /// <summary>
+        /// 车辆表停车记录ID
+        /// </summary>
         public int parkingRecordsID { get; set; }
     }
 }

+ 14 - 5
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -108,8 +108,8 @@ namespace parkMonitor.server.CoreThread
         /// </summary>
         public void BeginWorking()
         {
-            Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 36000, 36000);
-            Timer killTimeoutConnection = new System.Threading.Timer(KillTimeoutConnection, null, 36000, 36000);
+            Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 3600000, 3600000);
+            Timer killTimeoutConnection = new System.Threading.Timer(KillTimeoutConnection, null, 3600000, 3600000);
             //Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 10000, 300000);
             //Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 30000);
             Object lockObj = new object();
@@ -171,6 +171,7 @@ namespace parkMonitor.server.CoreThread
                     {
                         if (queueCmdRecord != null)
                         {
+                            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "等待机械手1资源线程计数:" + Robot.robot1.waitCount);
                             //对依赖同一机械手的线程计数
                             if (queueCmdRecord.id == Robot.robot1.id)
                             {
@@ -183,8 +184,8 @@ namespace parkMonitor.server.CoreThread
                         }
                         queueCmdRecord = (Command)queueCmd.Clone();
                     }
-                    Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "线程创建:" + queueCmd.LicenseNum + "准备,等待机械手线程个数:" + Robot.robot1.waitCount);
-                    Task.Factory.StartNew(() =>
+                    Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "线程创建:" + queueCmd.LicenseNum + "准备,等待机械手1线程个数:" + Robot.robot1.waitCount);
+                    Task t = Task.Factory.StartNew(() =>
                     {
                         Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "线程创建:" + queueCmd.LicenseNum + "开始");
                         SimpleCMDFactory simpleCMDFactory = new SimpleCMDFactory();
@@ -192,9 +193,17 @@ namespace parkMonitor.server.CoreThread
                         abstractCmd.executeCmd(queueCmd);
                         Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "线程创建:" + queueCmd.LicenseNum + "完成");
                     });
+                    if (t != null)
+                    {
+                        Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "线程创建:" + queueCmd.LicenseNum + "状态:"+ t.Status);
+                    }
+                    else
+                    {
+                        Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "线程创建:" + queueCmd.LicenseNum + "为空");
+                    }
 
                 }
-                Thread.Sleep(100);
+                Thread.Sleep(300);
             }
         }
 

+ 7 - 3
parkMonitor/tools/BlockingQueue.cs

@@ -15,14 +15,15 @@ namespace parkMonitor.tools
             try
             {
                 bQueue.Add(message);
+                Console.WriteLine("入队后连接池个数:" + bQueue.Count);
             }
             catch (ObjectDisposedException ex)
             {
-                Console.WriteLine(ex.Message+"对象已被释放");
+                Console.WriteLine(ex.Message + "对象已被释放");
             }
             catch (InvalidOperationException ex)
             {
-                Console.WriteLine(ex.Message+"已标记为已完成添加操作或队列未接收该项");
+                Console.WriteLine(ex.Message + "已标记为已完成添加操作或队列未接收该项");
             }
         }
         public object Dequeue()
@@ -31,6 +32,7 @@ namespace parkMonitor.tools
             try
             {
                 obj = bQueue.Take();
+                Console.WriteLine("出队后连接池个数:" + bQueue.Count);
             }
             catch (ObjectDisposedException ex)
             {
@@ -49,6 +51,7 @@ namespace parkMonitor.tools
             try
             {
                 flag = bQueue.TryTake(out item, millisecondsTimeout);
+                Console.WriteLine("出队后连接池个数超时:" + bQueue.Count);
             }
             catch (ObjectDisposedException ex)
             {
@@ -71,10 +74,11 @@ namespace parkMonitor.tools
             try
             {
                 counter = bQueue.Count();
+                Console.WriteLine("调用Count方法后" + counter);
             }
             catch (ObjectDisposedException ex)
             {
-                Console.WriteLine(ex.Message+"对象已被释放");
+                Console.WriteLine(ex.Message + "对象已被释放");
             }
             return counter;
         }