예제 #1
0
        public static IJobInfo GetAssemblyJob(JobDataMap jobDataMap)
        {
            var targetAssemblyPath = jobDataMap.GetString(TargetAssemblyPath);

            if (targetAssemblyPath == null)
            {
                return(null);
            }
            var targetTypeFullName = jobDataMap.GetString(TargetTypeName);

            if (targetTypeFullName == null)
            {
                return(null);
            }
            var targetMethodName = jobDataMap.GetString(TargetMethodName);

            if (string.IsNullOrEmpty(targetMethodName))
            {
                return(null);
            }

            string key = string.Format("{0}_{1}_{2}", targetAssemblyPath, targetTypeFullName, targetMethodName);

            if (!dictionary.ContainsKey(key))
            {
                if (!File.Exists(targetAssemblyPath))
                {
                    targetAssemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, targetAssemblyPath);
                }
                if (!File.Exists(targetAssemblyPath))
                {
                    return(null);
                }
                var assembly = Assembly.LoadFrom(targetAssemblyPath);
                if (assembly == null)
                {
                    return(null);
                }

                var type = assembly.GetType(targetTypeFullName);
                if (type == null)
                {
                    targetTypeFullName = assembly.FullName.Split(',')[0] + "." + targetTypeFullName;
                }
                type = assembly.GetType(targetTypeFullName);
                if (type == null)
                {
                    return(null);
                }
                var obj = new JobAssemblyInfo(type, targetMethodName, jobDataMap);
                if (!string.IsNullOrEmpty(obj.ErrorMessage))
                {
                    return(null);
                }
                dictionary.Add(key, obj);
            }
            return(dictionary[key]);
        }
예제 #2
0
        public Task Execute(IJobExecutionContext context)
        {
            return(Task.Run(() =>
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                var jobDataMap = context.MergedJobDataMap;
                var job = JobAssemblyInfo.GetAssemblyJob(jobDataMap);
                if (job == null)
                {
                    return;
                }
                var jobName = context.JobDetail.Key.Name;
                var jobGroupName = context.JobDetail.Key.Group;
                var jobScheduleName = context.Trigger.Key.Name;
                var startTime = DateTime.Now;
                long periodBySecond;
                var endTime = DateTime.Now;

                try
                {
                    job.Invoke( );
                    stopwatch.Stop();
                    periodBySecond = stopwatch.ElapsedMilliseconds;
                    endTime = DateTime.Now;
                }
                catch (Exception ex)
                {
                    var message = ex.Message;
                    stopwatch.Stop();
                    periodBySecond = stopwatch.ElapsedMilliseconds;
                    endTime = DateTime.Now;
                }
                finally
                {
                }
            }));
        }