2015-03-25 90 views
1

我對ASp.net應用程序有麻煩,我測試了幾個解決方案,但是我沒有看到任何問題。感謝您的建議。用Quartz.Net和Asp.Net應用程序執行JobSchedule的好方法是什麼?

我已經安裝並運行Quartz.Net作爲Windows服務。 Quartz.Net與我的API位於同一個文件夾中,以便在其中執行作業。

下面我的Global.asax的Application_Start

public static ISchedulerFactory schedFact; 
public static IScheduler scheduler; 

protected void Application_Start() 
{ 
    NameValueCollection properties = new NameValueCollection(); 
    properties["quartz.scheduler.instanceName"] = "ServerScheduler"; 
    properties["quartz.scheduler.proxy"] = "true"; 
    properties["quartz.threadPool.threadCount"] = "10"; 
    properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler"; 


    schedFact = new StdSchedulerFactory(properties); 
    scheduler = schedFact.GetScheduler(); 
    scheduler.Start(); 

    MyAPI.Services.ScheduleTasks.JobScheduler.StartGenerateContract(); 
} 

這在我的作業scrippted類:

public class ScheduleTasks 
{ 
    public static Dictionary<string, string> report; 

    public class JobScheduler 
    { 
     public static void StartGenerateContract() 
     { 
      try 
      { 
       MailService.SendMail("StartGenerateContract", "Scheduletask", 
        new Exception(DateTime.Now.ToString())); 


       IJobDetail job_generate = JobBuilder.Create<GenerateAndSendContract>() 
        .WithIdentity("generateContractJob", "group1") 
        .Build(); 

       ITrigger trigger = TriggerBuilder.Create() 
        .WithIdentity("trigger_contracts", "group1") 
        .ForJob("generateContractJob", "group1") 
        .StartNow() 
        .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(01, 00)) 
        .Build(); 

       MyAPI.MvcApplication.scheduler.ScheduleJob(job_generate, trigger); 
      } 
      catch (Exception e) 
      { 
       throw new Exception(e.Message); 
      } 
     } 
    } 
} 

然後我的工作是在同一個文件和類ScheduleTasks

public class GenerateAndSendContract : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     try 
     { 
      MailService.SendMail("GenerateAndSendContract", "Scheduletask"); 
       //my working code... 
     } 
     catch (Exception e) 
     { 
      MailService.SendErrorMail("GenerateAndSendContract", "ScheduleTasks", e); 
     } 
    } 
} 

我的scheduleTask完美執行,因爲我收到第一封電子郵件(StartGenerateC良好的時間間隔。但是該作業沒有執行,因爲類generateandsendcontract中的代碼沒有被觸發(沒有中斷點,沒有發送郵件GenerateAndSendContract)。

我的代碼有問題嗎?感謝您的幫助。

+0

'MyAPI.MvcApplication.scheduler'背後的代碼是什麼? – 2015-03-26 14:56:05

+0

嗨,這是我的公共靜態IScheduler調度程序;這是在我的application_start中啓動的 – Seb 2015-03-26 15:13:35

回答

1

當創建的MVC應用程序側的調度,你只需要這些屬性:

properties["quartz.scheduler.instanceName"] = "RemoteClient"; 
properties["quartz.scheduler.proxy"] = "true"; 
properties["quartz.threadPool.threadCount"] = "0"; 
properties["quartz.scheduler.proxy.address"] = address; 

你也不需要調用上充當客戶機調度程序的啓動方法。

相關問題