You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.4 KiB
45 lines
1.4 KiB
using System; |
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
namespace DotXxlJob.Core |
|
{ |
|
public class DefaultJobHandlerFactory : IJobHandlerFactory |
|
{ |
|
private readonly JobHandlerCache _handlerCache; |
|
|
|
public DefaultJobHandlerFactory(IServiceProvider provider, JobHandlerCache handlerCache = null) |
|
{ |
|
_handlerCache = handlerCache ?? new JobHandlerCache(); |
|
|
|
Initialize(provider); |
|
} |
|
|
|
private void Initialize(IServiceProvider provider) |
|
{ |
|
foreach (var handler in provider.GetServices<IJobHandler>()) |
|
{ |
|
_handlerCache.AddJobHandler(handler); |
|
} |
|
|
|
if (_handlerCache.IsEmpty) |
|
{ |
|
throw new TypeLoadException("IJobHandlers are not found in IServiceCollection"); |
|
} |
|
} |
|
|
|
public IJobHandler GetJobHandler(IServiceScopeFactory scopeFactory, string handlerName, out IServiceScope serviceScope) |
|
{ |
|
serviceScope = null; |
|
|
|
var jobHandler = _handlerCache.Get(handlerName); |
|
|
|
if (jobHandler == null) return null; |
|
|
|
if (jobHandler.JobHandler != null) return jobHandler.JobHandler; |
|
|
|
serviceScope = scopeFactory.CreateScope(); |
|
|
|
return (IJobHandler)serviceScope.ServiceProvider.GetRequiredService(jobHandler.JobHandlerType); |
|
} |
|
} |
|
} |