public TYDNewsService() { InitializeComponent(); server = QuartzServerFactory.CreateServer(); server.Initialize(); }
/// <summary> /// Initializes a new instance of the <see cref="QuartzService"/> class. /// </summary> public QuartzService() { logger = LogManager.GetLogger(GetType()); logger.Debug("Obtaining instance of an IQuartzServer"); server = QuartzServerFactory.CreateServer(); logger.Debug("Initializing server"); server.Initialize(); logger.Debug("Server initialized"); }
public virtual void Run() { ILog log = LogManager.GetLogger(typeof(HelloRun)); log.Info("------- Initializing ----------------------"); // First we must get a reference to a scheduler //ISchedulerFactory sf = new StdSchedulerFactory(); //IScheduler sched = sf.GetScheduler(); IQuartzServer server = QuartzServerFactory.CreateServer(); server.Initialize(); IScheduler sched = server.GetScheduler(); log.Info("------- Initialization Complete -----------"); // computer a time that is on the next round minute DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow); log.Info("------- Scheduling Job -------------------"); // define the job and tie it to our HelloJob class IJobDetail job = JobBuilder.Create <HelloJob>() .WithIdentity("job1", "group1") .Build(); // Trigger the job to run on the next round minute ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartAt(runTime) .Build(); // Tell quartz to schedule the job using our trigger sched.ScheduleJob(job, trigger); log.Info(string.Format("{0} will run at: {1}", job.Key, runTime.ToString("r"))); // Start up the scheduler (nothing can actually run until the // scheduler has been started) sched.Start(); log.Info("------- Started Scheduler -----------------"); // wait long enough so that the scheduler as an opportunity to // run the job! log.Info("------- Waiting 65 seconds... -------------"); // wait 65 seconds to show jobs Thread.Sleep(TimeSpan.FromSeconds(65)); // shut down the scheduler log.Info("------- Shutting Down ---------------------"); sched.Shutdown(true); log.Info("------- Shutdown Complete -----------------"); }
/// <summary> /// 创建新的任务调度实例 /// </summary> /// <param name="typeFullName">计划任务调度类型名称(GetType().FullName)</param> /// <param name="autoStart">是否自动初始化并启动任务实例</param> /// <returns></returns> public static IQuartzServer CreateSchedulerServer(string typeFullName, bool autoStart = false) { IQuartzServer serverInstance = null; try { //1、先从缓存中读取同名的任务调度实例 if (string.IsNullOrWhiteSpace(typeFullName)) { logger.FatalFormat("==>计划任务调度类型名称不能为空:'{0}'", typeFullName); throw new ArgumentNullException("CreateSchedulerServer.typeFullName 参数不能为空!"); } //typeFullName = typeFullName ?? HostServiceConfigurationLoader.HostServiceCfg.serverImplementationTypeName; if (_quartzServerInstances.ContainsKey(typeFullName) && _quartzServerInstances[typeFullName] != null) { serverInstance = _quartzServerInstances[typeFullName]; return(serverInstance); } //2、缓存不存在则重新创建新的任务调度实例 if (!_quartzServiceTypes.ContainsKey(typeFullName)) { lock (Locker) { if (!_quartzServiceTypes.ContainsKey(typeFullName)) { LoadAssemblyForQuartzServer(); } } } logger.Debug(String.Format("==>创建新的任务服务实例:'{0}',时间:{1}", typeFullName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); Type t = _quartzServiceTypes[typeFullName]; serverInstance = ObjectUtils.InstantiateType <IQuartzServer>(t); //(IQuartzServer)Activator.CreateInstance(t); if (autoStart) { serverInstance.Initialize(true); logger.Debug(String.Format("==>任务服务实例初始化成功! 时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))); } //3、缓存创建的任务调度实例 if (serverInstance != null) { _quartzServerInstances.AddOrUpdate(typeFullName, serverInstance, (key, oldInstance) => { return(serverInstance); }); } } catch (Exception Ex) { logger.FatalFormat("==>任务服务实例创建出错:{0}", Ex.Message); } return(serverInstance); }
public static void Initialize() { var container = new WindsorContainer(); container .Register(Component.For <IJsonSerializer>().ImplementedBy <JsonSerializer>()) .Register(Component.For <ExceptionInterceptor>().LifeStyle.PerWcfOperation()) .Kernel.AddFacility <WcfFacility>(); // WCF Config //Enables debugging and help information features for a Windows Communication Foundation (WCF) service. var returnFaults = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true, HttpHelpPageEnabled = true }; //Configures run-time throughput settings that enable you to tune service performance. var serviceThrottlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = 16, MaxConcurrentInstances = 10, MaxConcurrentSessions = Int32.MaxValue }; container.Register(Component.For <IServiceBehavior>().Instance(returnFaults)); container.Register(Component.For <IServiceBehavior>().Instance(serviceThrottlingBehavior)); container.Register(Component.For <ISchedulerService>() .ImplementedBy <SchedulerService>().LifestyleTransient() .Interceptors(InterceptorReference.ForType <ExceptionInterceptor>()).Anywhere .AsWcfService(new DefaultServiceModel() .AddEndpoints(WcfEndpoint.BoundTo(new BasicHttpBinding { MaxReceivedMessageSize = 200000, MaxBufferSize = 200000, MaxBufferPoolSize = 200000, } ).At(ConfigurationManager.AppSettings.Get("QuartzSoapAddress") + "BasicHttpEndPoint")) .AddBaseAddresses(ConfigurationManager.AppSettings.Get("QuartzServiceUrl")) .PublishMetadata(extension => extension.EnableHttpGet()) )); bool isAllValid = true; foreach (IHandler handler in container.Kernel.GetAssignableHandlers(typeof(object))) { if (handler.CurrentState != HandlerState.Valid) { Console.WriteLine("HandlerState NOT Valid for : " + handler.ComponentModel.ComponentName + " - " + handler.CurrentState); isAllValid = false; } } if (!isAllValid) { throw new Exception("Invalid components !"); } IQuartzServer server = QuartzServerFactory.CreateServer(); server.Initialize(); server.Start(); }