using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using BLWWS_BLL.Job; using NLog; using Quartz; using Quartz.Impl; using LogManager = NLog.LogManager; namespace BLWWS_BLL { public class GlobalCache { public static ISchedulerFactory sf = null; public static IScheduler sched = null; public static ConcurrentQueue GlobalCacheData = new ConcurrentQueue(); public static System.Timers.Timer timer; private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); public static void Init() { _logger.Error("初始化"); if (timer == null) { timer = new System.Timers.Timer(); timer.Interval = 500; timer.Elapsed -= Timer_Elapsed; timer.Elapsed += Timer_Elapsed; timer.Start(); } } private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { if (GlobalCache.GlobalCacheData.TryDequeue(out string data)) { if (!string.IsNullOrEmpty(data)) { string strResult = Tools.PostWebRequest(data, "");//调用人脸机接口 _logger.Error("人脸 后台:" + strResult); } } } catch (Exception ex) { _logger.Error("人脸 后台异常:" + ex.Message); } } public static void Add(string jobData) { if (timer == null) { timer = new System.Timers.Timer(); timer.Interval = 500; timer.Elapsed -= Timer_Elapsed; timer.Elapsed += Timer_Elapsed; timer.Start(); } else { } _logger.Error("后台来了任务"); GlobalCacheData.Enqueue(jobData); //if (sf == null) //{ // Init(); //} //DateTimeOffset runTime = DateTimeOffset.UtcNow.AddSeconds(2); //string JobName = "Job" + System.Guid.NewGuid().ToString("N"); //string TriggerName = "Trigger" + System.Guid.NewGuid().ToString("N"); //IJobDetail job = JobBuilder.Create() // .WithIdentity(JobName, "group1") // .UsingJobData("JobData", jobData) // .Build(); //// Trigger the job to run on the next round minute //ITrigger trigger = TriggerBuilder.Create() // .WithIdentity(TriggerName, "group1") // .StartAt(runTime) // .Build(); //GlobalCache.sched.ScheduleJob(job, trigger); } public static void TaskAdd(string jobData) { if (sched != null) { DateTimeOffset runTime = DateTimeOffset.UtcNow.AddSeconds(2); string JobName = "Job" + System.Guid.NewGuid().ToString("N"); string TriggerName = "Trigger" + System.Guid.NewGuid().ToString("N"); IJobDetail job = JobBuilder.Create() .WithIdentity(JobName, "group1") .UsingJobData("JobData", jobData) .Build(); // Trigger the job to run on the next round minute ITrigger trigger = TriggerBuilder.Create() .WithIdentity(TriggerName, "group1") .StartAt(runTime) .Build(); GlobalCache.sched.ScheduleJob(job, trigger); } else { LogHelper.WriteLog("初始化"); } } } }