2016-02-25 135 views
0

我打算使用quartz.net來處理文件。如何在Quartz.net中使用多線程

  • 我每小時會收到一個或多個文件,每個文件在不同國家會有1000行。
  • 我想讀取這些行,驗證,處理並將其插入到多個表中。
  • 我想選擇1個文件,併爲每個不同的國家在該文件中我喜歡創建一個線程。這樣1個線程將處理1個國家的所有數據。
  • 在給定的時間不應該有超過5個線程。

現在我該如何在quartz.net中定義這個?下面是我的代碼中,我會通過文件中的每個文件,並通過行的每一行,我沒有做任何多線程

調度作業

var properties = new NameValueCollection(); 
properties["quartz.scheduler.instanceName"] = "MyScheduler"; 
properties["quartz.threadPool.threadCount"] = "5"; 
ISchedulerFactory sf = new StdSchedulerFactory(properties); 
IScheduler s = sf.GetScheduler(); 
if (!s.IsStarted) 
    s.Start(); 

var jobKey = new JobKey("UniqueJobName", "BatchProcess"); 
if (s.GetJobDetail(jobKey) != null) 
    return "Error! Already running"; 

IJobDetail jobDetail = JobBuilder.Create<CountryProcessJob>() 
    .WithIdentity(jobKey) 
    .Build(); 

ITrigger trigger = TriggerBuilder.Create() 
    .WithIdentity("UniqueTriggerName", "BatchProcess") 
    .StartAt(DateTime.Now.AddSeconds(1)) 
    .Build(); 

s.ScheduleJob(jobDetail, trigger); 

工作

public class CountryProcessJob : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     While() // TillAllFilesAreProcessed 
     { 
     // Read A File 
      While() //for each row in file 
      { 
       // validate 
       // Process 
       // Insert 
      } 
     } 
    } 
} 

我是否應該有一個主要的Job來循環遍歷一個文件,然後在Job中定義多個Jobs來處理每個國家?這是如何實現石英多線程?

計劃的主要工作

var properties = new NameValueCollection(); 
properties["quartz.scheduler.instanceName"] = "MyScheduler"; 
properties["quartz.threadPool.threadCount"] = "5"; 
ISchedulerFactory sf = new StdSchedulerFactory(properties); 
IScheduler s = sf.GetScheduler(); 
if (!s.IsStarted) 
    s.Start(); 

var jobKey = new JobKey("UniqueJobName", "BatchProcess"); 
if (s.GetJobDetail(jobKey) != null) 
    return "Error! Already running"; 

IJobDetail jobDetail = JobBuilder.Create<FileProcessJob>() 
    .WithIdentity(jobKey) 
    .Build(); 

ITrigger trigger = TriggerBuilder.Create() 
    .WithIdentity("UniqueTriggerName", "BatchProcess") 
    .StartAt(DateTime.Now.AddSeconds(1)) 
    .Build(); 

s.ScheduleJob(jobDetail, trigger); 

主要工作

public class FileProcessJob : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     while() // TillAllFilesAreProcessed 
     { 
     // Read A File 
      foreach(var eachCountry in file) //for each row in file 
      { 
       // Create a Job 
       var jobKey = new JobKey("UniqueCountryJobName", "BatchProcess"); 
       if (s.GetJobDetail(jobKey) != null) 
        return "Error! Already running"; 

       IJobDetail jobDetail = JobBuilder.Create<CountryProcessJob>() 
        .WithIdentity(jobKey) 
        .Build(); 

       ITrigger trigger = TriggerBuilder.Create() 
        .WithIdentity("UniqueCountryTriggerName", "BatchProcess") 
        .StartAt(DateTime.Now.AddSeconds(1)) 
        .Build(); 

       s.ScheduleJob(jobDetail, trigger);    
      } 
     } 
    } 
} 

每個國家

public class CountryProcessJob : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     // For each row for that country 
     // validate 
     // Process 
     // Insert 
    } 
} 

所以我應該創建多個職位,以實現多個線程在運行時創建多個作業同時?請幫助我在文件中運行5個併發線程處理每個不同的國家/地區。

回答

0

你可以創建一個單獨的靜態類,並在execute方法中調用它。一個靜態類在內存中有一個固定的空間。

public class ReadCountry : IJob 
{ 
    public void Execute(IJobExecutionContext context) 
    { 
     CountryProcessJob.DoIt(); 
    } 
} 

CountryProcessJob類

public static class CountryProcessJob 
    { 
     public static void DoIt() 
     { 
      While() // TillAllFilesAreProcessed 
      { 
      // Read A File 
       While() //for each row in file 
       { 
        // validate 
        // Process 
        // Insert 
       } 
      } 
     } 
    }