Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
修复了QuartzNet定时任务 服务层注入到Job任务层时无法调起定时任务Bug
- Loading branch information
Showing
8 changed files
with
153 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Uwl.Admin/Uwl.QuartzNet.JobCenter/JobFactory/IOCJobFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Quartz; | ||
using Quartz.Spi; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Uwl.QuartzNet.JobCenter.JobFactory | ||
{ | ||
public class IOCJobFactory : IJobFactory | ||
{ | ||
/// <summary> | ||
/// 注入反射获取依赖对象 | ||
/// </summary> | ||
private readonly IServiceProvider _serviceProvider; | ||
public IOCJobFactory(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
/// <summary> | ||
/// 实现接口Job | ||
/// </summary> | ||
/// <param name="bundle"></param> | ||
/// <param name="scheduler"></param> | ||
/// <returns></returns> | ||
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) | ||
{ | ||
try | ||
{ | ||
//个人测试目前没有内存泄漏等问题,若是各位大佬有上生产环境的 请监控一下内存情况 | ||
var serviceScope = _serviceProvider.CreateScope(); | ||
var job = serviceScope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob; | ||
return job; | ||
//var job = _serviceProvider.GetService(bundle.JobDetail.JobType) as IJob; | ||
//return job; | ||
|
||
} | ||
catch (Exception e) | ||
{ | ||
throw e; | ||
} | ||
} | ||
|
||
public void ReturnJob(IJob job) | ||
{ | ||
var disposable = job as IDisposable; | ||
if(disposable!=null) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,37 @@ | ||
using System; | ||
using Quartz; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using Quartz; | ||
using Uwl.Common.Cache.RedisCache; | ||
using Uwl.Common.Subscription; | ||
using Uwl.Data.Server.MenuServices; | ||
|
||
namespace Uwl.ScheduledTask.Job | ||
{ | ||
public class TestJobOne : IJob | ||
{ | ||
public async Task Execute(IJobExecutionContext context) | ||
private readonly IRedisCacheManager _redisCacheManager; | ||
private readonly IMenuServer _menuServer; | ||
public TestJobOne(IRedisCacheManager redisCacheManager,IMenuServer menuServer) | ||
{ | ||
this._redisCacheManager = redisCacheManager; | ||
this._menuServer = menuServer; | ||
} | ||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
//记录Job时间 | ||
Stopwatch stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||
await Console.Out.WriteLineAsync(string.Format("测试任务1:任务分组:{0},任务名称:{1}任务状态:{2}", context.JobDetail.Key.Group, context.JobDetail.Key.Name, "正常执行中")); | ||
|
||
await Console.Out.WriteLineAsync("我是有Redis的注入测试任务"); | ||
var list = await _menuServer.GetMenuList(); | ||
await Console.Out.WriteLineAsync("菜单表里总数量" + list.Count.ToString()); | ||
stopwatch.Stop(); | ||
if (stopwatch.Elapsed.TotalMilliseconds > 0) | ||
{ | ||
//写入日志性能监控表和执行是否出错 | ||
} | ||
await Console.Out.WriteLineAsync("执行时间" + stopwatch.Elapsed.TotalMilliseconds); | ||
//if (stopwatch.Elapsed.TotalMilliseconds > 0) | ||
//{ | ||
// //写入日志性能监控表和执行是否出错 | ||
//} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.