运行 Net Core 控制台应用程序中的简单石英示例
Running simple quartz example in Net Core console app
我正在尝试 运行 .NET Core 控制台应用程序中的简单 Quartz 3.x 示例。我有这段代码(取自 here):
public static async Task Main(string[] args)
{
// construct a scheduler factory
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
// get a scheduler
IScheduler sched = await factory.GetScheduler();
await sched.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
await sched.ScheduleJob(job, trigger);
}
public class HelloJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync("HelloJob is executing.");
}
}
随着对 3.x 的更改,一切都变得异步,它永远不会执行 HelloJob。只需 运行s 即可结束并退出控制台应用程序。我在这里错过了什么吗?我们不能在核心控制台应用程序中 运行 像这样的 quartz 作业吗?
我可以 运行 2.x 版本(没有异步方法)的常规控制台应用程序中的旧版本就好了:
public static void Main(string[] args)
{
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
sched.ScheduleJob(job, trigger);
}
public class HelloJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("HelloJob is executing.");
}
}
我没有在控制台应用程序中使用 Quartz 2.x,但是版本 3.x 的行为有点符合预期:在安排 sched
之后应用程序继续执行并且退出。
可能这不是最优雅的解决方案,但最后的这两行应该有所帮助:
...
sched.ScheduleJob(job, trigger);
Console.ReadKey()
await sched.Shutdown();
我正在尝试 运行 .NET Core 控制台应用程序中的简单 Quartz 3.x 示例。我有这段代码(取自 here):
public static async Task Main(string[] args)
{
// construct a scheduler factory
NameValueCollection props = new NameValueCollection
{
{ "quartz.serializer.type", "binary" }
};
StdSchedulerFactory factory = new StdSchedulerFactory(props);
// get a scheduler
IScheduler sched = await factory.GetScheduler();
await sched.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
await sched.ScheduleJob(job, trigger);
}
public class HelloJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync("HelloJob is executing.");
}
}
随着对 3.x 的更改,一切都变得异步,它永远不会执行 HelloJob。只需 运行s 即可结束并退出控制台应用程序。我在这里错过了什么吗?我们不能在核心控制台应用程序中 运行 像这样的 quartz 作业吗?
我可以 运行 2.x 版本(没有异步方法)的常规控制台应用程序中的旧版本就好了:
public static void Main(string[] args)
{
// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
IScheduler sched = schedFact.GetScheduler();
sched.Start();
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob", "group1")
.Build();
// Trigger the job to run now, and then every 40 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(40)
.RepeatForever())
.Build();
sched.ScheduleJob(job, trigger);
}
public class HelloJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("HelloJob is executing.");
}
}
我没有在控制台应用程序中使用 Quartz 2.x,但是版本 3.x 的行为有点符合预期:在安排 sched
之后应用程序继续执行并且退出。
可能这不是最优雅的解决方案,但最后的这两行应该有所帮助:
...
sched.ScheduleJob(job, trigger);
Console.ReadKey()
await sched.Shutdown();