parent
e7cd2bcd9f
commit
63b46caeea
13 changed files with 326 additions and 94 deletions
@ -1,49 +1,46 @@ |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using Microsoft.Extensions.DependencyInjection; |
||||
|
||||
namespace DotXxlJob.Core |
||||
{ |
||||
public class DefaultJobHandlerFactory:IJobHandlerFactory |
||||
public class DefaultJobHandlerFactory : IJobHandlerFactory |
||||
{ |
||||
private readonly IServiceProvider _provider; |
||||
private readonly Dictionary<string, IJobHandler> handlersCache = new Dictionary<string, IJobHandler>(); |
||||
public DefaultJobHandlerFactory(IServiceProvider provider) |
||||
private readonly JobHandlerCache _handlerCache; |
||||
|
||||
public DefaultJobHandlerFactory(IServiceProvider provider, JobHandlerCache handlerCache = null) |
||||
{ |
||||
this._provider = provider; |
||||
Initialize(); |
||||
_handlerCache = handlerCache ?? new JobHandlerCache(); |
||||
|
||||
Initialize(provider); |
||||
} |
||||
|
||||
private void Initialize() |
||||
private void Initialize(IServiceProvider provider) |
||||
{ |
||||
var list = this._provider.GetServices<IJobHandler>(); |
||||
if (list == null || !list.Any()) |
||||
foreach (var handler in provider.GetServices<IJobHandler>()) |
||||
{ |
||||
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}]"); |
||||
} |
||||
handlersCache.Add(handlerName,handler); |
||||
throw new TypeLoadException("IJobHandlers are not found in IServiceCollection"); |
||||
} |
||||
|
||||
} |
||||
|
||||
public IJobHandler GetJobHandler(string handlerName) |
||||
public IJobHandler GetJobHandler(IServiceScopeFactory scopeFactory, string handlerName, out IServiceScope serviceScope) |
||||
{ |
||||
if (handlersCache.ContainsKey(handlerName)) |
||||
{ |
||||
return handlersCache[handlerName]; |
||||
} |
||||
return null; |
||||
serviceScope = null; |
||||
|
||||
var jobHandler = _handlerCache.Get(handlerName); |
||||
|
||||
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 |
||||
{ |
||||
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