适用于不同班级和时间表的通用石英作业

发布于 2025-01-17 02:15:31 字数 1171 浏览 1 评论 0原文

我在 Net Core 6 项目中有以下 Quartz 作业:

public class StrategyJob : IJob {

  private readonly Logger _logger;

  private readonly Strategy _strategy;

  public StrategyJob(Logger logger, Strategy strategy) {

    _logger = logger;
    _strategy = strategy;

  }
  
  public async Task Execute(IJobExecutionContext context) {

    Result result = await strategy.Run();

    if (result.Code == ResultCode.Error)
      _logger.Error(result.ErrorMessage);

  } 

}

Quartz 配置如下:

services.AddQuartz(x => {

  // Quartz configuration

  x.AddJob<StrategyJob>(y => y
    .WithIdentity("StrategyJob")
  );

  x.AddTrigger(y => y
    .WithIdentity("Minute10OfEveryHour")  
    .ForJob("StrategyJob")
    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
  );

});

我需要使 StrategyJob 通用并将其应用于不同的策略/计划:

Strategy fs = new FastStrategy(2, 4, 5); > Run it every second

Strategy ss = new SlowStrategy("xy", 2); > Run it every day

... More strategies, each with a schedule, but the job execution will be the same.

如何创建通用 StrategyJob 并将其应用于不同的策略/计划?

I have the following Quartz Job in a Net Core 6 project:

public class StrategyJob : IJob {

  private readonly Logger _logger;

  private readonly Strategy _strategy;

  public StrategyJob(Logger logger, Strategy strategy) {

    _logger = logger;
    _strategy = strategy;

  }
  
  public async Task Execute(IJobExecutionContext context) {

    Result result = await strategy.Run();

    if (result.Code == ResultCode.Error)
      _logger.Error(result.ErrorMessage);

  } 

}

And the Quartz configuration is the following:

services.AddQuartz(x => {

  // Quartz configuration

  x.AddJob<StrategyJob>(y => y
    .WithIdentity("StrategyJob")
  );

  x.AddTrigger(y => y
    .WithIdentity("Minute10OfEveryHour")  
    .ForJob("StrategyJob")
    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
  );

});

I need to make StrategyJob generic and apply it to different Strategies / Schedules:

Strategy fs = new FastStrategy(2, 4, 5); > Run it every second

Strategy ss = new SlowStrategy("xy", 2); > Run it every day

... More strategies, each with a schedule, but the job execution will be the same.

How to create a generic StrategyJob and apply it to different strategies / schedules?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-01-24 02:15:31

您可以将策略添加到触发器的数据中。只要您不将调度程序保存在数据库中,您就不必担心触发器数据的序列化。

var jobData = new Dictionary<string, object>
{
    { "Strategy", new FastStrategy(...) }
}

x.AddTrigger(y => y
   .UsingJobData(new JobDataMap(jobData))
   . ...

您需要提供自己的 IJobFactory 实现

public class JobFactory : IJobFactory
{
    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        var strategy = (Strategy)bundle.JobDetail.JobDataMap.Get("Strategy");
        return new StrategyJob(logger, strategy);
    }
}

You can add the strategy to the trigger's data. As long as you don't persist the scheduler in a database, you shouldn't be worried about the serialization of the trigger data.

var jobData = new Dictionary<string, object>
{
    { "Strategy", new FastStrategy(...) }
}

x.AddTrigger(y => y
   .UsingJobData(new JobDataMap(jobData))
   . ...

You'll need to provide your own implementation of IJobFactory

public class JobFactory : IJobFactory
{
    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        var strategy = (Strategy)bundle.JobDetail.JobDataMap.Get("Strategy");
        return new StrategyJob(logger, strategy);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文