适用于不同班级和时间表的通用石英作业
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将策略添加到触发器的数据中。只要您不将调度程序保存在数据库中,您就不必担心触发器数据的序列化。
您需要提供自己的
IJobFactory
实现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.
You'll need to provide your own implementation of
IJobFactory