parent
e7cd2bcd9f
commit
63b46caeea
13 changed files with 326 additions and 94 deletions
@ -1,49 +1,46 @@ |
|||||||
using System; |
using System; |
||||||
using System.Collections.Generic; |
|
||||||
using System.Linq; |
|
||||||
using System.Reflection; |
using System.Reflection; |
||||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||||
|
|
||||||
namespace DotXxlJob.Core |
namespace DotXxlJob.Core |
||||||
{ |
{ |
||||||
public class DefaultJobHandlerFactory:IJobHandlerFactory |
public class DefaultJobHandlerFactory : IJobHandlerFactory |
||||||
{ |
{ |
||||||
private readonly IServiceProvider _provider; |
private readonly JobHandlerCache _handlerCache; |
||||||
private readonly Dictionary<string, IJobHandler> handlersCache = new Dictionary<string, IJobHandler>(); |
|
||||||
public DefaultJobHandlerFactory(IServiceProvider provider) |
public DefaultJobHandlerFactory(IServiceProvider provider, JobHandlerCache handlerCache = null) |
||||||
{ |
{ |
||||||
this._provider = provider; |
_handlerCache = handlerCache ?? new JobHandlerCache(); |
||||||
Initialize(); |
|
||||||
|
Initialize(provider); |
||||||
} |
} |
||||||
|
|
||||||
private void Initialize() |
private void Initialize(IServiceProvider provider) |
||||||
{ |
{ |
||||||
var list = this._provider.GetServices<IJobHandler>(); |
foreach (var handler in provider.GetServices<IJobHandler>()) |
||||||
if (list == null || !list.Any()) |
|
||||||
{ |
{ |
||||||
throw new TypeLoadException("IJobHandlers are not found in IServiceCollection"); |
_handlerCache.AddJobHandler(handler); |
||||||
} |
} |
||||||
|
|
||||||
foreach (var handler in list) |
if (_handlerCache.HandlersCache.Count < 1) |
||||||
{ |
|
||||||
var jobHandlerAttr = handler.GetType().GetCustomAttribute<JobHandlerAttribute>(); |
|
||||||
var handlerName = jobHandlerAttr == null ? handler.GetType().Name : jobHandlerAttr.Name; |
|
||||||
if (handlersCache.ContainsKey(handlerName)) |
|
||||||
{ |
{ |
||||||
throw new Exception($"same IJobHandler' name: [{handlerName}]"); |
throw new TypeLoadException("IJobHandlers are not found in IServiceCollection"); |
||||||
} |
|
||||||
handlersCache.Add(handlerName,handler); |
|
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
public IJobHandler GetJobHandler(string handlerName) |
public IJobHandler GetJobHandler(IServiceScopeFactory scopeFactory, string handlerName, out IServiceScope serviceScope) |
||||||
{ |
{ |
||||||
if (handlersCache.ContainsKey(handlerName)) |
serviceScope = null; |
||||||
{ |
|
||||||
return handlersCache[handlerName]; |
var jobHandler = _handlerCache.Get(handlerName); |
||||||
} |
|
||||||
return null; |
if (jobHandler == null) return null; |
||||||
|
|
||||||
|
if (jobHandler.JobHandler != null) return jobHandler.JobHandler; |
||||||
|
|
||||||
|
serviceScope = scopeFactory.CreateScope(); |
||||||
|
|
||||||
|
return (IJobHandler)ActivatorUtilities.CreateInstance(serviceScope.ServiceProvider, jobHandler.JobHandlerType, jobHandler.JobHandlerConstructorParameters); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
@ -1,7 +1,10 @@ |
|||||||
|
using System; |
||||||
|
using Microsoft.Extensions.DependencyInjection; |
||||||
|
|
||||||
namespace DotXxlJob.Core |
namespace DotXxlJob.Core |
||||||
{ |
{ |
||||||
public interface IJobHandlerFactory |
public interface IJobHandlerFactory |
||||||
{ |
{ |
||||||
IJobHandler GetJobHandler(string handlerName); |
IJobHandler GetJobHandler(IServiceScopeFactory scopeFactory, string handlerName, out IServiceScope serviceScope); |
||||||
} |
} |
||||||
} |
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
using System; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using DotXxlJob.Core.DefaultHandlers; |
||||||
|
using DotXxlJob.Core.Model; |
||||||
|
using Xunit; |
||||||
|
|
||||||
|
namespace DotXxlJob.Core.Tests |
||||||
|
{ |
||||||
|
public class JobHandlerCacheTest |
||||||
|
{ |
||||||
|
[Fact] |
||||||
|
public void Repeated_Job_Handler() |
||||||
|
{ |
||||||
|
var cache = new JobHandlerCache(); |
||||||
|
|
||||||
|
cache.AddJobHandler<SimpleHttpJobHandler>(); |
||||||
|
|
||||||
|
Assert.Throws<ArgumentException>(() => cache.AddJobHandler("simpleHttpJobHandler", new TestJobHandler())); |
||||||
|
} |
||||||
|
|
||||||
|
private class TestJobHandler : IJobHandler |
||||||
|
{ |
||||||
|
public void Dispose() { } |
||||||
|
|
||||||
|
public Task<ReturnT> Execute(JobExecuteContext context) |
||||||
|
{ |
||||||
|
throw new NotImplementedException(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -1,13 +0,0 @@ |
|||||||
using System; |
|
||||||
using Xunit; |
|
||||||
|
|
||||||
namespace DotXxlJob.Core.Tests |
|
||||||
{ |
|
||||||
public class UnitTest1 |
|
||||||
{ |
|
||||||
[Fact] |
|
||||||
public void Test1() |
|
||||||
{ |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue