我正在尝试使用StructureMap实现Quartz.Net,但是工作没有被触发。我的代码看起来像这样。
public class SampleRegistry : Registry
{
public SampleRegistry()
{
Scan(x =>
{
x.AssembliesFromApplicationBaseDirectory();
x.WithDefaultConventions();
x.AddAllTypesOf<IJob>().NameBy(c => c.Name);
});
For<IJobFactory>().Use<StructureMapJobFactory>();
For<ISchedulerFactory>().Use(ctx => new StdSchedulerFactory());
For<IScheduler>().Use(delegate(IContext ctx)
{
var scheduler = ctx.GetInstance<ISchedulerFactory>().GetScheduler();
scheduler.JobFactory = ctx.GetInstance<IJobFactory>();
return scheduler;
});
}
}
StructureMapJobFactory代码看起来像
public class StructureMapJobFactory : IJobFactory
{
private readonly IContainer container;
public StructureMapJobFactory(IContainer container)
{
this.container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
return (IJob) container.GetInstance(bundle.JobDetail.JobType);
}
}
示例作业看起来像
public class SendMailJob : IJob
{
private readonly IMailService _mailService;
public SendMailJob(IMailService mailService)
{
_mailService = mailService;
}
public void Execute(IJobExecutionContext context)
{
_mailService.SendMail();
}
}
和JobDetails
IScheduler scheduler = ObjectFactory.GetInstance<IScheduler>();
scheduler.Start();
IJobDetail job = JobBuilder.Create(typeof(Code.Services.SendMailJob))
.WithIdentity("sampleJob", "group1")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("sampleTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())
.Build();
scheduler.ScheduleJob(job, trigger);