예제 #1
0
 //public void Close()
 //{
 //    RedisManager.UnSubscribe("SyncDatabase");
 //}
 public void Start()
 {
     try
     {
         if (string.IsNullOrEmpty(storeId) || companyToken == 0)
         {
             Thread.CurrentThread.Abort();
             return;
         }
         StoreManager.Subscribe("SyncDatabase", (channel, info) =>
         {
             if (!string.IsNullOrEmpty(info))
             {
                 LocalSyncContext localSyncContext = new LocalSyncContext();
                 var kv = localSyncContext.ServiceMappings.FirstOrDefault(o => o.Key.EndsWith(info));
                 DoSynchronize(kv.Key, kv.Value);
             }
             else
             {
                 DoSynchronize();
             }
         });
         var date = DateTime.Now.Date.AddDays(1).AddHours(2);
         lock (StdSchedulerFactory.SystemPropertyAsInstanceId)
         {
             IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
             scheduler.Start();                              //开启调度器
             IJobDetail job1 = JobBuilder.Create <SyncJob>() //创建一个作业
                               .WithIdentity("SyncDatabaseJob", "SyncDatabase")
                               .Build();
             ITrigger trigger1 = TriggerBuilder.Create()
                                 .WithIdentity("SyncDatabase", "SyncDatabase")
                                 .StartNow()                           //启动时间
                                 .WithSimpleSchedule(x => x            //触发时间,5秒一次。
                                                     .WithIntervalInHours(5)
                                                     .RepeatForever()
                                                     )
                                 .Build();
             scheduler.ScheduleJob(job1, trigger1);      //把作业,触发器加入调度器。
         }
         //Task.Factory.StartNew(() =>
         //{
         //    DoSynchronize();
         //});
     }
     catch (Exception ex)
     {
         ILog logger = LogManager.GetLogger(Assembly.GetExecutingAssembly().GetName().Name);
         logger.Error(ex.Message, ex);
         Console.WriteLine(ex.Message);
     }
 }
예제 #2
0
        public void DoSynchronize()
        {
            LocalSyncContext localSyncContext = new LocalSyncContext();

            foreach (var item in localSyncContext.ServiceMappings)
            {
                var task = Task.Factory.StartNew((o) =>
                {
                    var temp = (KeyValuePair <string, ISyncDataService>)o;
                    DoSynchronize(temp.Key, temp.Value);
                }, item);
            }
        }