feat(新增): quartz运行时改变数据
This commit is contained in:
parent
59012062af
commit
5e8ecbd031
|
@ -0,0 +1,29 @@
|
||||||
|
using Quartz;
|
||||||
|
|
||||||
|
namespace Bunny.Service.Job;
|
||||||
|
|
||||||
|
public class CronJob : IJob
|
||||||
|
{
|
||||||
|
public Task Execute(IJobExecutionContext context)
|
||||||
|
{
|
||||||
|
// 工作Map集合
|
||||||
|
var dataMap = context.JobDetail.JobDataMap;
|
||||||
|
var jobDataMap = context.Trigger.JobDataMap;
|
||||||
|
|
||||||
|
// 工作Map中 username 和 password
|
||||||
|
var username = dataMap.GetString("username");
|
||||||
|
var password = dataMap.GetString("password");
|
||||||
|
var count = dataMap.Get("count");
|
||||||
|
var triggerCount = jobDataMap.Get("triggerCount");
|
||||||
|
|
||||||
|
Console.WriteLine($"用户名:{username},密码: {password},count:{count},triggerCount:{triggerCount}");
|
||||||
|
|
||||||
|
// 设置 count 值
|
||||||
|
dataMap.Put("count", Convert.ToInt32(count) + 1);
|
||||||
|
jobDataMap.Put("triggerCount", Convert.ToInt32(triggerCount) + 1);
|
||||||
|
|
||||||
|
// Trigger 触发器中的值是可以修改的,也会有响应
|
||||||
|
// 设置 JobBuilder 往里面推数据时是不会有变化的
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,9 +11,10 @@ public static class CronJobService
|
||||||
var stdSchedulerFactory = new StdSchedulerFactory();
|
var stdSchedulerFactory = new StdSchedulerFactory();
|
||||||
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
var jobDetail = JobBuilder.Create<SimpleJob>()
|
var jobDetail = JobBuilder.Create<CronJob>()
|
||||||
.UsingJobData("username", "用户名")
|
.UsingJobData("username", "用户名")
|
||||||
.UsingJobData("password", "密码")
|
.UsingJobData("password", "密码")
|
||||||
|
.UsingJobData("count", 1)
|
||||||
.StoreDurably() // 设置作业为持久的
|
.StoreDurably() // 设置作业为持久的
|
||||||
.WithIdentity("simpleJob", "简单的JOB")
|
.WithIdentity("simpleJob", "简单的JOB")
|
||||||
.Build();
|
.Build();
|
||||||
|
@ -23,6 +24,7 @@ public static class CronJobService
|
||||||
var trigger = TriggerBuilder.Create()
|
var trigger = TriggerBuilder.Create()
|
||||||
.ForJob(jobDetail)
|
.ForJob(jobDetail)
|
||||||
.UsingJobData("trigger", "trigger值")
|
.UsingJobData("trigger", "trigger值")
|
||||||
|
.UsingJobData("triggerCount", 1)
|
||||||
.WithIdentity("testTrigger", "测试发出器")
|
.WithIdentity("testTrigger", "测试发出器")
|
||||||
.StartNow()
|
.StartNow()
|
||||||
.WithCronSchedule("0/1 * * * * ?")
|
.WithCronSchedule("0/1 * * * * ?")
|
||||||
|
|
|
@ -9,8 +9,6 @@ public static class SimpleJobService
|
||||||
public static void AddCreateSimpleJobService(this WebApplicationBuilder builder)
|
public static void AddCreateSimpleJobService(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
var schedulerFactory = new StdSchedulerFactory();
|
var schedulerFactory = new StdSchedulerFactory();
|
||||||
// TODO 如果使用异步,必须使用await 和 async
|
|
||||||
// var scheduler = schedulerFactory.GetScheduler();
|
|
||||||
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
|
||||||
|
|
||||||
var jobDetail = JobBuilder.Create<SimpleJob>()
|
var jobDetail = JobBuilder.Create<SimpleJob>()
|
||||||
|
@ -28,9 +26,5 @@ public static class SimpleJobService
|
||||||
// 使用同步方法
|
// 使用同步方法
|
||||||
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
|
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
|
||||||
scheduler.Start().GetAwaiter().GetResult();
|
scheduler.Start().GetAwaiter().GetResult();
|
||||||
|
|
||||||
// TODO 使用异步
|
|
||||||
// await _scheduler.ScheduleJob(jobDetail, trigger);
|
|
||||||
// await _scheduler.Start();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue"><AssemblyExplorer>
|
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue"><AssemblyExplorer>
|
||||||
<Assembly Path="D:\software\Microsoft\Visual Studio\packages\stackexchange.redis\2.7.33\lib\net6.0\StackExchange.Redis.dll" />
|
<Assembly Path="D:\software\Microsoft\Visual Studio\packages\stackexchange.redis\2.7.33\lib\net6.0\StackExchange.Redis.dll" />
|
||||||
<Assembly Path="D:\software\Microsoft\Visual Studio\packages\minio\6.0.2\lib\net7.0\Minio.dll" />
|
<Assembly Path="D:\software\Microsoft\Visual Studio\packages\minio\6.0.2\lib\net7.0\Minio.dll" />
|
||||||
|
<Assembly Path="C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\8.0.7\ref\net8.0\Microsoft.AspNetCore.dll" />
|
||||||
</AssemblyExplorer></s:String>
|
</AssemblyExplorer></s:String>
|
||||||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=22382ad8_002Dcbf4_002D4254_002Da582_002D16e6116533ce/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" Name="TestJob #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=22382ad8_002Dcbf4_002D4254_002Da582_002D16e6116533ce/@EntryIndexedValue"><SessionState ContinuousTestingMode="0" Name="TestJob #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session">
|
||||||
<TestAncestor>
|
<TestAncestor>
|
||||||
|
|
|
@ -3,6 +3,7 @@ using Bunny.Common.Filter;
|
||||||
using Bunny.Service.Filter;
|
using Bunny.Service.Filter;
|
||||||
using Bunny.Service.IService;
|
using Bunny.Service.IService;
|
||||||
using Bunny.Service.IService.Service;
|
using Bunny.Service.IService.Service;
|
||||||
|
using Bunny.Service.Job.JobService;
|
||||||
using Lazy.Captcha.Core;
|
using Lazy.Captcha.Core;
|
||||||
using Lazy.Captcha.Core.Generator;
|
using Lazy.Captcha.Core.Generator;
|
||||||
using SkiaSharp;
|
using SkiaSharp;
|
||||||
|
@ -32,10 +33,9 @@ public static class ServiceRegistration
|
||||||
public static void AddApplicationBackendServices(this WebApplicationBuilder builder)
|
public static void AddApplicationBackendServices(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
// 测试注入后台服务
|
// 测试注入后台服务
|
||||||
// builder.Services.AddHostedService<TemplateBackgroundModule>().Clear();
|
|
||||||
// https://github.com/quartznet/quartznet/tree/main/database/tables
|
// https://github.com/quartznet/quartznet/tree/main/database/tables
|
||||||
// builder.AddCreateSimpleJobService();
|
// builder.AddCreateSimpleJobService();
|
||||||
// builder.AddCronJobService();
|
builder.AddCronJobService();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
using Microsoft.Extensions.Hosting;
|
设置后台服务,原生写法
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
namespace Bunny.Service.BackgroundModule;
|
namespace Bunny.Service.BackgroundModule;
|
||||||
|
|
||||||
|
@ -19,3 +22,4 @@ public class TemplateBackgroundModule : BackgroundService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
Loading…
Reference in New Issue