/// <summary> /// Inserts data to database /// </summary> /// <returns></returns> public AsyncProcessingServiceParameterValue InsertData() { AsyncProcessingServiceParameterValue asyncParameterValue; byte[] arr = { 1, 2, 3, 4, 5 }; asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", "Start", "Start", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.UserId = "A"; asyncParameterValue.ProcessName = "AAA"; asyncParameterValue.Data = CustomEncode.ToBase64String(arr); asyncParameterValue.ExecutionStartDateTime = DateTime.Now; asyncParameterValue.RegistrationDateTime = DateTime.Now; asyncParameterValue.NumberOfRetries = 0; asyncParameterValue.ProgressRate = 0; asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)(AsyncProcessingServiceParameterValue.AsyncStatus.Register); asyncParameterValue.CommandId = 0; asyncParameterValue.ReservedArea = "xxxxxx"; DbEnum.IsolationLevelEnum iso = DbEnum.IsolationLevelEnum.DefaultTransaction; AsyncProcessingServiceReturnValue asyncReturnValue; AsyncSvc_sample.LayerB layerB = new AsyncSvc_sample.LayerB(); asyncReturnValue = (AsyncProcessingServiceReturnValue)layerB.DoBusinessLogic((AsyncProcessingServiceParameterValue)asyncParameterValue, iso); return asyncParameterValue; }
/// <summary> /// Inserts async parameter values to database /// </summary> /// <param name="asyncParameterValue"></param> /// <param name="asyncReturnValue"></param> public void Insert(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; // 静的SQL filename = "AsyncProcessingServiceInsert.sql"; // -- ファイルから読み込む場合。 this.SetSqlByFile2(filename); // パラメタ ライズド クエリのパラメタに対して、動的に値を設定する。 this.SetParameter("P2", asyncParameterValue.UserId); this.SetParameter("P3", asyncParameterValue.ProcessName); this.SetParameter("P4", asyncParameterValue.Data); this.SetParameter("P5", asyncParameterValue.RegistrationDateTime); this.SetParameter("P6", asyncParameterValue.ExecutionStartDateTime); this.SetParameter("P7", asyncParameterValue.NumberOfRetries); this.SetParameter("P8", asyncParameterValue.CompletionDateTime); this.SetParameter("P9", asyncParameterValue.StatusId); this.SetParameter("P10", asyncParameterValue.ProgressRate); this.SetParameter("P11", asyncParameterValue.CommandId); this.SetParameter("P12", asyncParameterValue.ReservedArea); object obj; // -- 追加(件数を確認できる) obj = this.ExecInsUpDel_NonQuery(); // ↑DBアクセス----------------------------------------------------- // 戻り値を設定 asyncReturnValue.Obj = obj; }
/// <summary> /// Inserts async parameter values to database /// </summary> /// <param name="asyncParameterValue"></param> /// <param name="asyncReturnValue"></param> public void InsertTask(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; filename = "AsyncProcessingServiceInsert.sql"; // Get SQL query from file. this.SetSqlByFile3(filename); // Set SQL parameter values this.SetParameter("P2", asyncParameterValue.UserId); this.SetParameter("P3", asyncParameterValue.ProcessName); this.SetParameter("P4", asyncParameterValue.Data); this.SetParameter("P5", asyncParameterValue.RegistrationDateTime); this.SetParameter("P6", DBNull.Value); this.SetParameter("P7", asyncParameterValue.NumberOfRetries); this.SetParameter("P8", DBNull.Value); this.SetParameter("P9", asyncParameterValue.StatusId); this.SetParameter("P10", asyncParameterValue.ProgressRate); this.SetParameter("P11", asyncParameterValue.CommandId); this.SetParameter("P12", asyncParameterValue.ReservedArea); // Execute SQL query asyncReturnValue.Obj = this.ExecInsUpDel_NonQuery(); }
/// <summary> /// Selects Asynchronous task from LayerD /// </summary> /// <param name="asyncParameterValue">Async Parameter Value</param> private void UOC_SelectTask(AsyncProcessingServiceParameterValue asyncParameterValue) { AsyncProcessingServiceReturnValue asyncReturnValue = new AsyncProcessingServiceReturnValue(); this.ReturnValue = asyncReturnValue; LayerD myDao = new LayerD(this.GetDam()); myDao.SelectTask(asyncParameterValue, asyncReturnValue); DataTable dt = (DataTable)asyncReturnValue.Obj; asyncReturnValue.Obj = null; if (dt != null) { if (dt.Rows.Count != 0) { asyncReturnValue.TaskId = Convert.ToInt32(dt.Rows[0]["Id"]); asyncReturnValue.UserId = dt.Rows[0]["UserId"].ToString(); asyncReturnValue.ProcessName = dt.Rows[0]["ProcessName"].ToString(); asyncReturnValue.Data = dt.Rows[0]["Data"].ToString(); asyncReturnValue.NumberOfRetries = Convert.ToInt32(dt.Rows[0]["NumberOfRetries"]); asyncReturnValue.ReservedArea = dt.Rows[0]["ReservedArea"].ToString(); asyncReturnValue.CommandId = Convert.ToInt32(dt.Rows[0]["CommandId"]); } } }
/// <summary> /// Updates Async Parameter values to Database through LayerD /// </summary> /// <param name="asyncParameterValue"></param> private void UOC_Update(AsyncProcessingServiceParameterValue asyncParameterValue) { AsyncProcessingServiceReturnValue asyncReturnValue = new AsyncProcessingServiceReturnValue(); this.ReturnValue = asyncReturnValue; LayerD myDao = new LayerD(this.GetDam()); myDao.Update(asyncParameterValue, asyncReturnValue); }
/// <summary> /// Inserts Async Parameter values to Database through LayerD /// </summary> /// <param name="asyncParameterValue"></param> public void UOC_Start(AsyncProcessingServiceParameterValue asyncParameterValue) { // 戻り値クラスを生成して、事前に戻り地に設定しておく。 AsyncProcessingServiceReturnValue asyncReturnValue = new AsyncProcessingServiceReturnValue(); this.ReturnValue = asyncReturnValue; LayerD myDao = new LayerD(this.GetDam()); myDao.Insert(asyncParameterValue, asyncReturnValue); }
/// <summary> /// Updates information in the database that the asynchronous task is started /// </summary> /// <param name="asyncParameterValue">Asynchronous Parameter Values</param> private void UOC_UpdateTaskStart(AsyncProcessingServiceParameterValue asyncParameterValue) { AsyncProcessingServiceReturnValue asyncReturnValue = new AsyncProcessingServiceReturnValue(); this.ReturnValue = asyncReturnValue; LayerD myDao = new LayerD(this.GetDam()); myDao.UpdateTaskStart(asyncParameterValue, asyncReturnValue); }
/// <summary> /// Selects user command from Database through LayerD /// </summary> /// <param name="asyncParameterValue"></param> private void UOC_SelectCommand(AsyncProcessingServiceParameterValue asyncParameterValue) { AsyncProcessingServiceReturnValue asyncReturnValue = new AsyncProcessingServiceReturnValue(); this.ReturnValue = asyncReturnValue; LayerD myDao = new LayerD(this.GetDam()); myDao.SelectCommand(asyncParameterValue, asyncReturnValue); }
/// <summary> /// Inserts Async Parameter values to Database through LayerD /// </summary> /// <param name="asyncParameterValue"></param> public void UOC_InsertTask(AsyncProcessingServiceParameterValue asyncParameterValue) { // 戻り値クラスを生成して、事前に戻り地に設定しておく。 AsyncProcessingServiceReturnValue asyncReturnValue = new AsyncProcessingServiceReturnValue(); this.ReturnValue = asyncReturnValue; LayerD myDao = new LayerD(this.GetDam()); myDao.InsertTask(asyncParameterValue, asyncReturnValue); }
/// <summary> /// Selects user command from database /// </summary> /// <param name="asyncParameterValue">Asynchronous Parameter Values</param> /// <param name="asyncReturnValue">Asynchronous Return Values</param> public void SelectCommand(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; filename = "SelectCommand.sql"; // Get SQL query from file. this.SetSqlByFile3(filename); // Set SQL parameter values this.SetParameter("P1", asyncParameterValue.TaskId); // Execute SQL query asyncReturnValue.Obj = this.ExecSelectScalar(); }
/// <summary> /// Set stop command for all running asynchronous task. /// </summary> /// <param name="asyncParameterValue">Asynchronous Parameter Values</param> /// <param name="asyncReturnValue">Asynchronous Return Values</param> public void StopAllTask(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; filename = "StopAllTask.sql"; // Get SQL query from file. this.SetSqlByFile3(filename); // Set SQL parameter values this.SetParameter("P1", asyncParameterValue.StatusId); this.SetParameter("P2", asyncParameterValue.CommandId); // Execute SQL query asyncReturnValue.Obj = this.ExecInsUpDel_NonQuery(); }
/// <summary> /// Updates progress rate of the asynchronous task in the database. /// </summary> /// <param name="asyncParameterValue">Asynchronous Parameter Values</param> /// <param name="asyncReturnValue">Asynchronous Return Values</param> public void UpdateTaskProgress(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; filename = "UpdateTaskProgress.sql"; // Get SQL query from file. this.SetSqlByFile3(filename); // Set SQL parameter values this.SetParameter("P1", asyncParameterValue.TaskId); this.SetParameter("P2", asyncParameterValue.ProgressRate); // Execute SQL query asyncReturnValue.Obj = this.ExecInsUpDel_NonQuery(); }
/// <summary> /// Updates information in the database that the asynchronous task is failed and abort this task [status=Abort] /// </summary> /// <param name="asyncParameterValue">Asynchronous Parameter Values</param> /// <param name="asyncReturnValue">Asynchronous Return Values</param> public void UpdateTaskFail(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; filename = "UpdateTaskFail.sql"; // Get SQL query from file. this.SetSqlByFile3(filename); // Set SQL parameter values this.SetParameter("P1", asyncParameterValue.TaskId); this.SetParameter("P2", asyncParameterValue.CompletionDateTime); this.SetParameter("P3", asyncParameterValue.StatusId); this.SetParameter("P4", asyncParameterValue.ExceptionInfo); // Execute SQL query asyncReturnValue.Obj = this.ExecInsUpDel_NonQuery(); }
/// <summary> /// To get Asynchronous Task from the database /// </summary> /// <param name="asyncParameterValue"></param> /// <param name="asyncReturnValue"></param> public void SelectTask(AsyncProcessingServiceParameterValue asyncParameterValue, AsyncProcessingServiceReturnValue asyncReturnValue) { string filename = string.Empty; filename = "SelectTask.sql"; // Get SQL query from file. this.SetSqlByFile3(filename); // Set SQL parameter values this.SetParameter("P1", asyncParameterValue.RegistrationDateTime); this.SetParameter("P2", asyncParameterValue.NumberOfRetries); this.SetParameter("P3", (int)AsyncProcessingServiceParameterValue.AsyncStatus.Register); this.SetParameter("P4", (int)AsyncProcessingServiceParameterValue.AsyncStatus.AbnormalEnd); this.SetParameter("P7", asyncParameterValue.CompletionDateTime); DataTable dt = new DataTable(); // Get Asynchronous Task from the database this.ExecSelectFill_DT(dt); asyncReturnValue.Obj = dt; }
/// <summary> /// Maintains the single worker thread functionalities. /// </summary> /// <param name="asyncTask">Selected Asynchronous Task</param> private void WorkerThreadCallBack(object asyncTask) { AsyncProcessingServiceReturnValue selectedAsyncTask = (AsyncProcessingServiceReturnValue)asyncTask; // A new worker thread started an Async task this._workerThreadCount++; try { // To handle unstable "Register" state, when you invoke [Abort] at this state if (selectedAsyncTask.CommandId == (int)AsyncProcessingServiceParameterValue.AsyncCommand.Abort) { throw new BusinessSystemException("APSAbortCommand", GetMessage.GetMessageDescription("CTE0004")); } // Call User Program to execute by using communication control function AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", "StartCopyFromBlob", "StartCopyFromBlob", "SQL", new MyUserInfo(selectedAsyncTask.UserId, selectedAsyncTask.TaskId.ToString())); asyncParameterValue.TaskId = selectedAsyncTask.TaskId; asyncParameterValue.Data = selectedAsyncTask.Data; CallController callController = new CallController(asyncParameterValue.User); AsyncProcessingServiceReturnValue asyncReturnValue = (AsyncProcessingServiceReturnValue)callController.Invoke(selectedAsyncTask.ProcessName, asyncParameterValue); if (asyncReturnValue.ErrorFlag == true) { if (asyncReturnValue.ErrorMessageID == "APSStopCommand") { string exceptionInfo = "ErrorMessageID: " + asyncReturnValue.ErrorMessageID + Environment.NewLine + "ErrorMessage: " + asyncReturnValue.ErrorMessage; // Asynchronous task is stopped due to user 'stop' command. this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.RETRY, exceptionInfo); LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0001") + asyncReturnValue.ErrorMessage, selectedAsyncTask.TaskId)); } else { // Exception occurred by other than BusinessApplicationException if (selectedAsyncTask.NumberOfRetries < this._maxNumberOfRetries) { // Asynchronous task does not exceeds the maximum number of retries // Updated as retry later string exceptionInfo = "ErrorMessageID: " + asyncReturnValue.ErrorMessageID + Environment.NewLine + "ErrorMessage: " + asyncReturnValue.ErrorMessage; selectedAsyncTask.NumberOfRetries += 1; this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.RETRY, exceptionInfo); LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0004"), selectedAsyncTask.TaskId)); } else { // Asynchronous task exceeds maximum number of retries // Update task as abort string exceptionInfo = "ErrorMessageID: " + asyncReturnValue.ErrorMessageID + Environment.NewLine + "ErrorMessage: " + asyncReturnValue.ErrorMessage; this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.FAIL, exceptionInfo); LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0005"), selectedAsyncTask.TaskId)); } } } else { // Selected Asynchronous task is completed successfully. this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.SUCCESS); LogIF.InfoLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("I0003"), selectedAsyncTask.TaskId)); } } catch (BusinessSystemException ex) { // Asynchronous task is aborted due to BusinessSystemException sent by user program. string exceptionInfo = "ErrorMessageID: " + ex.messageID + Environment.NewLine + "ErrorMessage: " + ex.Message; this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.FAIL, exceptionInfo); LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0006"), selectedAsyncTask.TaskId, ex.Message)); } catch (Exception ex) { // Asynchronous task is aborted due to unexpected exception. string exceptionInfo = "ErrorMessageID: " + ex.GetType().Name + Environment.NewLine + "ErrorMessage: " + ex.Message; this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.FAIL, exceptionInfo); LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0006"), selectedAsyncTask.TaskId, ex.Message)); } finally { // Async task is over this._workerThreadCount--; } }
/// <summary> /// Maintains the Main thread of the Asynchronous Service /// </summary> public void MainThreadInvoke() { // Asynchronous service is started LogIF.InfoLog("ASYNC-SERVICE", GetMessage.GetMessageDescription("I0001")); // Infinte loop processing for selecting register task. while (this._infiniteLoop) { try { // Get asynchronous task from the database. AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", "SelectTask", "SelectTask", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.RegistrationDateTime = DateTime.Now - new TimeSpan(_maxNumberOfHours, 0, 0); asyncParameterValue.NumberOfRetries = this._maxNumberOfRetries; asyncParameterValue.CompletionDateTime = DateTime.Now - new TimeSpan(_maxNumberOfHours, 0, 0); LayerB layerB = new LayerB(); AsyncProcessingServiceReturnValue selectedAsyncTask = (AsyncProcessingServiceReturnValue)layerB.DoBusinessLogic( (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted); // Existence check of asynchronous task if (string.IsNullOrEmpty(selectedAsyncTask.UserId)) { // Asynchronous task does not exist. // Wait for the asynchronous task to be registered in the database. Thread.Sleep(this._numberOfSeconds * 1000); // Continue to this infinite loop. continue; } // Asynchronous task exists. // Check the number of free worker threads. int freeWorkerThreads = 0; int completionPortThreads = 0; // Gets the available threads. ThreadPool.GetAvailableThreads(out freeWorkerThreads, out completionPortThreads); while (freeWorkerThreads == 0) { // Wait for the completion of the worker thread. Thread.Sleep(this._numberOfSeconds * 1000); // Get available threads. ThreadPool.GetAvailableThreads(out freeWorkerThreads, out completionPortThreads); } // Selected asynchronous task is assigned to a worker thread this.UpdateAsyncTask(selectedAsyncTask, AsyncTaskUpdate.START); LogIF.InfoLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("I0002"), selectedAsyncTask.TaskId)); // Assign the task to the worker thread ThreadPool.QueueUserWorkItem(new WaitCallback(this.WorkerThreadCallBack), (object)selectedAsyncTask); } catch (Exception ex) { // Service Failed due to unexpected exception. this._infiniteLoop = false; LogIF.ErrorLog("ASYNC-SERVICE", string.Format(GetMessage.GetMessageDescription("E0000"), ex.Message.ToString())); } } }
/// <summary> /// Updates the selected asynchronous task based type of SQL_Update_Method_name /// </summary> /// <param name="selectedAsyncTask">Selected Asynchronous Task</param> /// <param name="updateTask">SQL_Update_Method_name</param> /// <returns></returns> public AsyncProcessingServiceReturnValue UpdateAsyncTask(AsyncProcessingServiceReturnValue selectedAsyncTask, string updateTask, string exceptionInfo = null) { AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", updateTask, updateTask, "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.TaskId = selectedAsyncTask.TaskId; // Update databse based on task switch (updateTask) { case AsyncTaskUpdate.START: asyncParameterValue.ExecutionStartDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.Processing; break; case AsyncTaskUpdate.RETRY: if (exceptionInfo.Length > 512) { exceptionInfo = exceptionInfo.Substring(0, 512); } asyncParameterValue.NumberOfRetries = selectedAsyncTask.NumberOfRetries; asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.AbnormalEnd; asyncParameterValue.ExceptionInfo = exceptionInfo; break; case AsyncTaskUpdate.FAIL: if (exceptionInfo.Length > 512) { exceptionInfo = exceptionInfo.Substring(0, 512); } asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.Abort; asyncParameterValue.ExceptionInfo = exceptionInfo; break; case AsyncTaskUpdate.SUCCESS: asyncParameterValue.CompletionDateTime = DateTime.Now; asyncParameterValue.ProgressRate = 100; asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.End; break; } LayerB layerB = new LayerB(); AsyncProcessingServiceReturnValue asyncReturnValue = (AsyncProcessingServiceReturnValue)layerB.DoBusinessLogic( (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted); return asyncReturnValue; }
/// <summary> /// Stop the asynchronous service and Waits to complete all worker thread to complete. /// </summary> public void StopAsyncProcess() { // Breaks the infinite loop of Main thread. this._infiniteLoop = false; // Wait the end of the main thread. this._mainThread.Join(); // Update stop command to all the running asynchronous task AsyncProcessingServiceParameterValue asyncParameterValue = new AsyncProcessingServiceParameterValue("AsyncProcessingService", "StopAllTask", "StopAllTask", "SQL", new MyUserInfo("AsyncProcessingService", "AsyncProcessingService")); asyncParameterValue.StatusId = (int)AsyncProcessingServiceParameterValue.AsyncStatus.Processing; asyncParameterValue.CommandId = (int)AsyncProcessingServiceParameterValue.AsyncCommand.Stop; LayerB layerB = new LayerB(); AsyncProcessingServiceReturnValue asyncReturnValue = (AsyncProcessingServiceReturnValue)layerB.DoBusinessLogic( (BaseParameterValue)asyncParameterValue, DbEnum.IsolationLevelEnum.ReadCommitted); if (asyncReturnValue.ErrorFlag) { LogIF.ErrorLog("ASYNC-SERVICE", "ErrorMessageID: " + asyncReturnValue.ErrorMessageID + "ErrorMessage: " + asyncReturnValue.ErrorMessage); } // Wait for all worker thread to be complete LogIF.InfoLog("ASYNC-SERVICE", GetMessage.GetMessageDescription("I0004")); while (this._workerThreadCount != 0) { // Wait for the completion of the worker thread. Thread.Sleep(this._numberOfSeconds * 1000); } // Log after completing all worker threads LogIF.InfoLog("ASYNC-SERVICE", GetMessage.GetMessageDescription("I0008")); }