[英]How to use multi threading in Quartz.net
I am planning to use quartz.net for processing files. 我打算使用quartz.net来处理文件。
Now how do I define this in quartz.net? 现在如何在quartz.net中定义它? The below is the code I have in which I am going through each file by file and each row by row and I am not doing any multithreading 下面是我拥有的代码,在其中我逐文件逐行,逐行浏览,并且不执行任何多线程处理
Scheduling a job 安排工作
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);
Job 工作
public class CountryProcessJob : IJob
{
public void Execute(IJobExecutionContext context)
{
While() // TillAllFilesAreProcessed
{
// Read A File
While() //for each row in file
{
// validate
// Process
// Insert
}
}
}
}
Should I have a main Job to loop through the Files one at time and then with in the Job should I define multiple Jobs for processing each country? 我是否应该有一个主要职位,一次要遍历文件,然后在职位中定义多个职位来处理每个国家? Is this how to achieve multi threading in quartz? 这是如何在石英中实现多线程?
Schedule the main Job 安排主要工作
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);
Main Job 主要工作
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);
}
}
}
}
Create multiple Jobs for each country 为每个国家/地区创建多个职位
public class CountryProcessJob : IJob
{
public void Execute(IJobExecutionContext context)
{
// For each row for that country
// validate
// Process
// Insert
}
}
So should I create multiple Jobs to implement multiple thread to run at same time? 那么我应该创建多个Job来实现多个线程同时运行吗? Please help me to run 5 concurrent threads processing each distinct country with in a file. 请帮助我在一个文件中运行5个并发线程来处理每个不同的国家/地区。
Quartz.Net Jobs run in a seperate thread. Quartz.Net作业在单独的线程中运行。 so if you want to configure the max nuber of threads take a look at this answer: https://stackoverflow.com/a/4108795/745011 因此,如果您想配置线程的最大数量,请查看以下答案: https ://stackoverflow.com/a/4108795/745011
You can create a separate static class, and call it in the execute method. 您可以创建一个单独的静态类,然后在execute方法中调用它。 An static class have a fixed space in the memory. 静态类在内存中具有固定的空间。
public class ReadCountry : IJob
{
public void Execute(IJobExecutionContext context)
{
CountryProcessJob.DoIt();
}
}
CountryProcessJob class CountryProcessJob类
public static class CountryProcessJob
{
public static void DoIt()
{
While() // TillAllFilesAreProcessed
{
// Read A File
While() //for each row in file
{
// validate
// Process
// Insert
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.