feat(新增): quartz运行时改变数据

This commit is contained in:
bunny 2024-08-20 16:21:03 +08:00
parent 59012062af
commit 5e8ecbd031
6 changed files with 41 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -11,9 +11,10 @@ public static class CronJobService
var stdSchedulerFactory = new StdSchedulerFactory();
var scheduler = stdSchedulerFactory.GetScheduler().GetAwaiter().GetResult();
var jobDetail = JobBuilder.Create<SimpleJob>()
var jobDetail = JobBuilder.Create<CronJob>()
.UsingJobData("username", "用户名")
.UsingJobData("password", "密码")
.UsingJobData("count", 1)
.StoreDurably() // 设置作业为持久的
.WithIdentity("simpleJob", "简单的JOB")
.Build();
@ -23,6 +24,7 @@ public static class CronJobService
var trigger = TriggerBuilder.Create()
.ForJob(jobDetail)
.UsingJobData("trigger", "trigger值")
.UsingJobData("triggerCount", 1)
.WithIdentity("testTrigger", "测试发出器")
.StartNow()
.WithCronSchedule("0/1 * * * * ?")

View File

@ -9,8 +9,6 @@ public static class SimpleJobService
public static void AddCreateSimpleJobService(this WebApplicationBuilder builder)
{
var schedulerFactory = new StdSchedulerFactory();
// TODO 如果使用异步必须使用await 和 async
// var scheduler = schedulerFactory.GetScheduler();
var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();
var jobDetail = JobBuilder.Create<SimpleJob>()
@ -28,9 +26,5 @@ public static class SimpleJobService
// 使用同步方法
scheduler.ScheduleJob(jobDetail, trigger).GetAwaiter().GetResult();
scheduler.Start().GetAwaiter().GetResult();
// TODO 使用异步
// await _scheduler.ScheduleJob(jobDetail, trigger);
// await _scheduler.Start();
}
}

View File

@ -2,6 +2,7 @@
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
&lt;Assembly Path="D:\software\Microsoft\Visual Studio\packages\stackexchange.redis\2.7.33\lib\net6.0\StackExchange.Redis.dll" /&gt;&#xD;
&lt;Assembly Path="D:\software\Microsoft\Visual Studio\packages\minio\6.0.2\lib\net7.0\Minio.dll" /&gt;&#xD;
&lt;Assembly Path="C:\Program Files\dotnet\packs\Microsoft.AspNetCore.App.Ref\8.0.7\ref\net8.0\Microsoft.AspNetCore.dll" /&gt;&#xD;
&lt;/AssemblyExplorer&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=22382ad8_002Dcbf4_002D4254_002Da582_002D16e6116533ce/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="TestJob #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;

View File

@ -3,6 +3,7 @@ using Bunny.Common.Filter;
using Bunny.Service.Filter;
using Bunny.Service.IService;
using Bunny.Service.IService.Service;
using Bunny.Service.Job.JobService;
using Lazy.Captcha.Core;
using Lazy.Captcha.Core.Generator;
using SkiaSharp;
@ -32,10 +33,9 @@ public static class ServiceRegistration
public static void AddApplicationBackendServices(this WebApplicationBuilder builder)
{
// 测试注入后台服务
// builder.Services.AddHostedService<TemplateBackgroundModule>().Clear();
// https://github.com/quartznet/quartznet/tree/main/database/tables
// builder.AddCreateSimpleJobService();
// builder.AddCronJobService();
builder.AddCronJobService();
}
/// <summary>

View File

@ -1,4 +1,7 @@
using Microsoft.Extensions.Hosting;
设置后台服务,原生写法
```csharp
using Microsoft.Extensions.Hosting;
namespace Bunny.Service.BackgroundModule;
@ -18,4 +21,5 @@ public class TemplateBackgroundModule : BackgroundService
Console.WriteLine("--------------------------------");
}
}
}
}
```