gpt4 book ai didi

c# - Topshelf 需要使用 Autofac 的 Quartz 作业工厂

转载 作者:太空狗 更新时间:2023-10-29 20:41:41 27 4
gpt4 key购买 nike

我正在尝试将 TopShelf 与 Quartz.net 和 Autofac 一起使用。我下面的代码工作得很好。然而,这一行:

cfg.UsingQuartzJobFactory(() => container.Resolve<IJobFactory>());

似乎是错误的做事方式。有没有更好的方法告诉 Topshelf 使用自定义 autofac jobfactory? jobfactory 的生命周期范围是多少?我担心这行代码将来某个时候会让我头疼。不再需要时如何释放 jobfactory?这条线可以吗?

class Poller : IJob
{
private readonly ILogger _log;

public Poller(ILogger log)
{
_log = log;
_log.Info("Instantiating...");
}

public void Execute(IJobExecutionContext context)
{
_log.Info("Executing...");
}
}

class Program
{
static Autofac.IContainer BuildContainer()
{
var builder = new ContainerBuilder();

builder.RegisterModule<NLogModule>();
builder.RegisterModule<QuartzAutofacFactoryModule>();
builder.RegisterModule(new QuartzAutofacJobsModule(typeof(Poller).Assembly));

var container = builder.Build();
return container;
}

static void Main(string[] args)
{
var container = BuildContainer();

HostFactory.Run(cfg =>
{
cfg.UseNLog();
cfg.UseAutofacContainer(container);
cfg.SetDescription("DESCRIPTION");
cfg.SetDisplayName("DISPLAY");
cfg.SetServiceName("NAME");

cfg.UsingQuartzJobFactory(() => container.Resolve<IJobFactory>());

cfg.ScheduleQuartzJobAsService(q =>
{
q.WithJob(() => JobBuilder.Create<Poller>().Build());
q.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(b => b.WithIntervalInSeconds(20).RepeatForever()).Build());
});

cfg.StartAutomatically();
cfg.RunAsLocalSystem();

});
}
}

供引用:TopShelf.Quartz.ScheduleHobHostConfiguratorExtensions

另请引用:Autofac.Extras.Quartz.QuartzAutofacFactoryModule

最佳答案

我认为你应该用容器初始化 quartz 服务器,这个例子使用 unity,但我确信它可以与其他容器一起工作。

try
{
var container = new UnityContainer();
schedulerFactory = CreateSchedulerFactory();
quartzscheduler = GetScheduler();
SyncPost.Initialize.RepositoryConfig(container);
SyncPost.Initialize.AddToSchedulerContextCustomVars(quartzscheduler, container);
quartzscheduler.JobFactory = new JobFactoryInjection(container);

}
catch (Exception e)
{
logger.Error("Server initialization failed:" + e.Message, e);
throw;
}

JobFactoryInjection 实现 IJobFactory 的地方:

 public class JobFactoryInjection : IJobFactory
{

private readonly UnityContainer container = new UnityContainer();

public JobFactoryInjection(UnityContainer container)
{

if (container == null) throw new ArgumentNullException("container", "Container is null");
this.container = container;
}



public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) {


// Return job registrated in container
bundle.JobDetail.JobDataMap.Put(SyncUtils.ContextKeyCenterCode, scheduler.Context.Get(SyncUtils.ContextKeyCenterCode));
return (IJob)container.Resolve(bundle.JobDetail.JobType);

}

public void ReturnJob(IJob job) {

}
}

关于 JobFactory 的生命周期,不用担心。从 quartz 文档:“JobFactory 只是激活作业类的一个新实例。您可能希望创建自己的 JobFactory 实现来完成诸如让应用程序的 IoC 或 DI 容器生成/初始化作业实例之类的事情”

关于c# - Topshelf 需要使用 Autofac 的 Quartz 作业工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27020376/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com