繁体   English   中英

如何在Quartz.net中使用多线程

[英]How to use multi threading in Quartz.net

我打算使用quartz.net来处理文件。

  • 我每小时将收到1个或多个文件,并且每个文件在不同国家/地区将有1000行。
  • 我想读取这些行,进行验证,处理并将其插入到多个表中。
  • 我想选择一个文件,并针对该文件中的每个不同的国家/地区创建一个线程。 这样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
            }
        }
    }
}

我是否应该有一个主要职位,一次要遍历文件,然后在职位中定义多个职位来处理每个国家? 这是如何在石英中实现多线程?

安排主要工作

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
    }
}

那么我应该创建多个Job来实现多个线程同时运行吗? 请帮助我在一个文件中运行5个并发线程来处理每个不同的国家/地区。

Quartz.Net作业在单独的线程中运行。 因此,如果您想配置线程的最大数量,请查看以下答案: https ://stackoverflow.com/a/4108795/745011

您可以创建一个单独的静态类,然后在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
                }
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM