Ejemplo n.º 1
0
 public JobContext(Trigger trigger, JobDetail jobDetail, JobDataMap jobDataMap, IWindsorContainer container)
 {
     this.trigger = trigger;
     this.jobDetail = jobDetail;
     this.jobDataMap = jobDataMap;
     this.container = container;
 }
Ejemplo n.º 2
0
        public void RunBatchJobs(IWindsorContainer container)
        {
            log.Info("----------------------------------Invincible's dividing line---------------------------------------");
            log.Info("BatchJobs run start.");

            IList<Trigger> tobeFiredTriggerList = this.GetTobeFiredTrigger();
            string siServiceAddress = this.dao.FindAllWithNativeSql<string>("select Value from SYS_EntityPreference where Id = ?", new object[] { com.Sconit.Entity.SYS.EntityPreference.CodeEnum.SIServiceAddress }).SingleOrDefault();
            string siServicePort = this.dao.FindAllWithNativeSql<string>("select Value from SYS_EntityPreference where Id = ?", new object[] { com.Sconit.Entity.SYS.EntityPreference.CodeEnum.SIServicePort }).SingleOrDefault();

            if (tobeFiredTriggerList != null && tobeFiredTriggerList.Count > 0)
            {
                //Parallel.ForEach(tobeFiredTriggerList, (tobeFiredTrigger) =>
                foreach (Trigger tobeFiredTrigger in tobeFiredTriggerList)
                {
                    Thread.Sleep(500);

                    JobDetail jobDetail = tobeFiredTrigger.JobDetail;
                    RunLog runLog = new RunLog();
                    try
                    {
                        #region Job运行前处理
                        BeforeJobRun(runLog, tobeFiredTrigger);
                        #endregion

                        #region 运行Job
                        JobDataMap dataMap = new JobDataMap();

                        #region 把WebService地址传入Job
                        if (!string.IsNullOrWhiteSpace(siServiceAddress)
                            && !string.IsNullOrWhiteSpace(siServicePort))
                        {
                            dataMap.PutData("SIServiceAddress", siServiceAddress);
                            dataMap.PutData("SIServicePort", siServicePort);
                        }
                        #endregion

                        #region Job参数获取
                        IList<JobParameter> jobParameterList = this.dao.FindAllWithCustomQuery<JobParameter>("from JobParameter where JobId = ?", jobDetail.Id);
                        if (jobParameterList != null && jobParameterList.Count > 0)
                        {
                            foreach (JobParameter jobParameter in jobParameterList)
                            {
                                log.Debug("Set Job Parameter Name:" + jobParameter.Key + ", Value:" + jobParameter.Value);
                                dataMap.PutData(jobParameter.Key, jobParameter.Value);
                            }
                        }
                        #endregion

                        #region Trigger参数获取
                        IList<TriggerParameter> triggerParameterList = this.dao.FindAllWithCustomQuery<TriggerParameter>("from TriggerParameter where TriggerId = ?", tobeFiredTrigger.Id);
                        if (triggerParameterList != null && triggerParameterList.Count > 0)
                        {
                            foreach (TriggerParameter triggerParameter in triggerParameterList)
                            {
                                log.Debug("Set Trigger Parameter Name:" + triggerParameter.Key + ", Value:" + triggerParameter.Value);
                                if (!dataMap.ContainKey(triggerParameter.Key))
                                {
                                    dataMap.PutData(triggerParameter.Key, triggerParameter.Value);
                                }
                            }
                        }
                        #endregion

                        #region 初始化JobRunContext
                        JobContext jobRunContext = new JobContext(tobeFiredTrigger, jobDetail, dataMap, container);
                        #endregion

                        #region 调用Job
                        IJob job = container.Resolve<IJob>(jobDetail.ServiceType);
                        log.Debug("Start run job: " + jobDetail.ServiceType);
                        job.Execute(jobRunContext);
                        this.dao.FlushSession();
                        #endregion

                        #endregion

                        #region Job运行后处理
                        AfterJobRunSuccess(runLog, tobeFiredTrigger);
                        #endregion
                    }
                    catch (Exception ex)
                    {
                        AfterJobRunFail(runLog, tobeFiredTrigger, ex);
                    }
                    finally
                    {
                        #region 更新BatchTrigger
                        UpdateTrigger(tobeFiredTrigger);
                        #endregion
                    }
                }
                //);
            }
            else
            {
                log.Info("No job found may run in this batch.");
            }

            log.Info("BatchJobs run end.");
        }