瀏覽代碼

Refactor.

master
yangxiaodong 7 年之前
父節點
當前提交
05125fc6ae
共有 13 個文件被更改,包括 72 次插入42 次删除
  1. +2
    -2
      src/DotNetCore.CAP.Kafka/IProcessor.Producer.cs
  2. +2
    -2
      src/DotNetCore.CAP.RabbitMQ/RabbitMQProducerClient.cs
  3. +28
    -0
      src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs
  4. +1
    -1
      src/DotNetCore.CAP/CAP.Options.cs
  5. +5
    -4
      src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
  6. +3
    -3
      src/DotNetCore.CAP/CapStartContext.cs
  7. +2
    -2
      src/DotNetCore.CAP/IBootstrapper.Default.cs
  8. +4
    -4
      src/DotNetCore.CAP/IConsumerHandler.Default.cs
  9. +0
    -12
      src/DotNetCore.CAP/Infrastructure/IConsumerExcutorSelector.cs
  10. +18
    -5
      src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs
  11. +3
    -3
      src/DotNetCore.CAP/Internal/MethodMatcherCache.cs
  12. +2
    -2
      src/DotNetCore.CAP/Job/CronJobRegistry.Default.cs
  13. +2
    -2
      src/DotNetCore.CAP/Job/IProcessingServer.Job.cs

+ 2
- 2
src/DotNetCore.CAP.Kafka/IProcessor.Producer.cs 查看文件

@@ -16,7 +16,7 @@ namespace DotNetCore.CAP.Kafka
{
public class KafkaJobProcessor : IJobProcessor
{
private readonly ConsistencyOptions _options;
private readonly CapOptions _options;
private readonly CancellationTokenSource _cts;

private readonly IServiceProvider _provider;
@@ -26,7 +26,7 @@ namespace DotNetCore.CAP.Kafka
private TimeSpan _pollingDelay;

public KafkaJobProcessor(
IOptions<ConsistencyOptions> options,
IOptions<CapOptions> options,
ILogger<KafkaJobProcessor> logger,
IServiceProvider provider)
{


+ 2
- 2
src/DotNetCore.CAP.RabbitMQ/RabbitMQProducerClient.cs 查看文件

@@ -9,10 +9,10 @@ namespace DotNetCore.CAP.RabbitMQ
{
public class RabbitMQProducerClient : ICapProducerService
{
private readonly ConsistencyOptions _options;
private readonly CapOptions _options;
private readonly ILogger _logger;

public RabbitMQProducerClient(IOptions<ConsistencyOptions> options, ILoggerFactory loggerFactory)
public RabbitMQProducerClient(IOptions<CapOptions> options, ILoggerFactory loggerFactory)
{
_options = options.Value;
_logger = loggerFactory.CreateLogger(nameof(RabbitMQProducerClient));


+ 28
- 0
src/DotNetCore.CAP/Abstractions/IConsumerServiceSelector.cs 查看文件

@@ -0,0 +1,28 @@
using System.Collections.Generic;
using DotNetCore.CAP.Abstractions;

namespace DotNetCore.CAP.Abstractions
{
/// <summary>
/// Defines an interface for selecting an cosumer service method to invoke for the current message.
/// </summary>
public interface IConsumerServiceSelector
{
/// <summary>
/// Selects a set of <see cref="ConsumerExecutorDescriptor"/> candidates for the current message associated with
/// <paramref name="context"/>.
/// </summary>
/// <param name="context">The <see cref="CapStartContext"/> associated with the current message.</param>
/// <returns>A set of <see cref="ConsumerExecutorDescriptor"/> candidates or <c>null</c>.</returns>
IReadOnlyList<ConsumerExecutorDescriptor> SelectCandidates(CapStartContext context);

/// <summary>
/// Selects the best <see cref="ConsumerExecutorDescriptor"/> candidate from <paramref name="candidates"/> for the
/// current message associated with <paramref name="context"/>.
/// </summary>
/// <param name="key">topic or exchange router key.</param>
/// <param name="candidates">the set of <see cref="ConsumerExecutorDescriptor"/> candidates.</param>
/// <returns></returns>
ConsumerExecutorDescriptor SelectBestCandidate(string key, IReadOnlyList<ConsumerExecutorDescriptor> candidates);
}
}

src/DotNetCore.CAP/Infrastructure/ConsistencyOptions.cs → src/DotNetCore.CAP/CAP.Options.cs 查看文件

@@ -5,7 +5,7 @@ namespace DotNetCore.CAP.Infrastructure
/// <summary>
/// Represents all the options you can use to configure the system.
/// </summary>
public class ConsistencyOptions
public class CapOptions
{
public string BrokerUrlList { get; set; } = "localhost:9092";


+ 5
- 4
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs 查看文件

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using DotNetCore.CAP;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Abstractions.ModelBinding;
using DotNetCore.CAP.Infrastructure;
using DotNetCore.CAP.Internal;
@@ -22,7 +23,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// <returns>An <see cref="CapBuilder"/> for application services.</returns>
public static CapBuilder AddConsistency(this IServiceCollection services)
{
services.AddConsistency(x => new ConsistencyOptions());
services.AddConsistency(x => new CapOptions());

return new CapBuilder(services);
}
@@ -31,18 +32,18 @@ namespace Microsoft.Extensions.DependencyInjection
/// Adds and configures the consistence services for the consitence.
/// </summary>
/// <param name="services">The services available in the application.</param>
/// <param name="setupAction">An action to configure the <see cref="ConsistencyOptions"/>.</param>
/// <param name="setupAction">An action to configure the <see cref="CapOptions"/>.</param>
/// <returns>An <see cref="CapBuilder"/> for application services.</returns>
public static CapBuilder AddConsistency(
this IServiceCollection services,
Action<ConsistencyOptions> setupAction)
Action<CapOptions> setupAction)
{
services.TryAddSingleton<CapMarkerService>();
services.Configure(setupAction);

AddConsumerServices(services);

services.TryAddSingleton<IConsumerExcutorSelector, ConsumerExcutorSelector>();
services.TryAddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>();
services.TryAddSingleton<IModelBinder, DefaultModelBinder>();
services.TryAddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>();
services.TryAddSingleton<MethodMatcherCache>();


src/DotNetCore.CAP/TopicContext.cs → src/DotNetCore.CAP/CapStartContext.cs 查看文件

@@ -3,13 +3,13 @@ using System.Threading;

namespace DotNetCore.CAP
{
public class TopicContext
public class CapStartContext
{
public TopicContext()
public CapStartContext()
{
}

public TopicContext(IServiceProvider provider, CancellationToken cancellationToken)
public CapStartContext(IServiceProvider provider, CancellationToken cancellationToken)
{
ServiceProvider = provider;
CancellationToken = cancellationToken;

+ 2
- 2
src/DotNetCore.CAP/IBootstrapper.Default.cs 查看文件

@@ -17,7 +17,7 @@ namespace DotNetCore.CAP
private Task _bootstrappingTask;

public DefaultBootstrapper(
IOptions<ConsistencyOptions> options,
IOptions<CapOptions> options,
ICapMessageStore storage,
IApplicationLifetime appLifetime,
IServiceProvider provider)
@@ -41,7 +41,7 @@ namespace DotNetCore.CAP
});
}

protected ConsistencyOptions Options { get; }
protected CapOptions Options { get; }

protected ICapMessageStore Storage { get; }



+ 4
- 4
src/DotNetCore.CAP/IConsumerHandler.Default.cs 查看文件

@@ -20,13 +20,13 @@ namespace DotNetCore.CAP
private readonly ILogger _logger;

private readonly MethodMatcherCache _selector;
private readonly ConsistencyOptions _options;
private readonly CapOptions _options;
private readonly ICapMessageStore _messageStore;
private readonly CancellationTokenSource _cts;

public event EventHandler<ConsistencyMessage> MessageReceieved;

private TopicContext _context;
private CapStartContext _context;
private Task _compositeTask;
private bool _disposed;

@@ -37,7 +37,7 @@ namespace DotNetCore.CAP
ILoggerFactory loggerFactory,
ICapMessageStore messageStore,
MethodMatcherCache selector,
IOptions<ConsistencyOptions> options) {
IOptions<CapOptions> options) {
_selector = selector;
_logger = loggerFactory.CreateLogger<ConsumerHandler>();
_loggerFactory = loggerFactory;
@@ -54,7 +54,7 @@ namespace DotNetCore.CAP
}

public void Start() {
_context = new TopicContext(_serviceProvider, _cts.Token);
_context = new CapStartContext(_serviceProvider, _cts.Token);

var matchs = _selector.GetCandidatesMethods(_context);



+ 0
- 12
src/DotNetCore.CAP/Infrastructure/IConsumerExcutorSelector.cs 查看文件

@@ -1,12 +0,0 @@
using System.Collections.Generic;
using DotNetCore.CAP.Abstractions;

namespace DotNetCore.CAP.Infrastructure
{
public interface IConsumerExcutorSelector
{
IReadOnlyList<ConsumerExecutorDescriptor> SelectCandidates(TopicContext context);

ConsumerExecutorDescriptor SelectBestCandidate(string key, IReadOnlyList<ConsumerExecutorDescriptor> executeDescriptor);
}
}

src/DotNetCore.CAP/Internal/ConsumerExcutorSelector.cs → src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs 查看文件

@@ -3,26 +3,39 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DotNetCore.CAP.Abstractions;
using DotNetCore.CAP.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace DotNetCore.CAP.Internal
{
public class ConsumerExcutorSelector : IConsumerExcutorSelector
/// <summary>
/// A default <see cref="IConsumerServiceSelector"/> implementation.
/// </summary>
public class DefaultConsumerServiceSelector : IConsumerServiceSelector
{
private readonly IServiceProvider _serviceProvider;

public ConsumerExcutorSelector(IServiceProvider serviceProvider)
/// <summary>
/// Creates a new <see cref="DefaultConsumerServiceSelector"/>.
/// </summary>
/// <param name="serviceProvider"></param>
public DefaultConsumerServiceSelector(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public ConsumerExecutorDescriptor SelectBestCandidate(string key, IReadOnlyList<ConsumerExecutorDescriptor> executeDescriptor)
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="executeDescriptor"></param>
/// <returns></returns>
public ConsumerExecutorDescriptor SelectBestCandidate(string key,
IReadOnlyList<ConsumerExecutorDescriptor> executeDescriptor)
{
return executeDescriptor.FirstOrDefault(x => x.Attribute.Name == key);
}

public IReadOnlyList<ConsumerExecutorDescriptor> SelectCandidates(TopicContext context)
public IReadOnlyList<ConsumerExecutorDescriptor> SelectCandidates(CapStartContext context)
{
var consumerServices = context.ServiceProvider.GetServices<IConsumerService>();


+ 3
- 3
src/DotNetCore.CAP/Internal/MethodMatcherCache.cs 查看文件

@@ -7,14 +7,14 @@ namespace DotNetCore.CAP.Internal
{
public class MethodMatcherCache
{
private readonly IConsumerExcutorSelector _selector;
private readonly IConsumerServiceSelector _selector;

public MethodMatcherCache(IConsumerExcutorSelector selector)
public MethodMatcherCache(IConsumerServiceSelector selector)
{
_selector = selector;
}

public ConcurrentDictionary<string, ConsumerExecutorDescriptor> GetCandidatesMethods(TopicContext routeContext)
public ConcurrentDictionary<string, ConsumerExecutorDescriptor> GetCandidatesMethods(CapStartContext routeContext)
{
if (Entries.Count == 0)
{


+ 2
- 2
src/DotNetCore.CAP/Job/CronJobRegistry.Default.cs 查看文件

@@ -5,9 +5,9 @@ namespace DotNetCore.CAP.Job
{
public class DefaultCronJobRegistry : CronJobRegistry
{
private readonly ConsistencyOptions _options;
private readonly CapOptions _options;

public DefaultCronJobRegistry(IOptions<ConsistencyOptions> options)
public DefaultCronJobRegistry(IOptions<CapOptions> options)
{
_options = options.Value;



+ 2
- 2
src/DotNetCore.CAP/Job/IProcessingServer.Job.cs 查看文件

@@ -18,7 +18,7 @@ namespace DotNetCore.CAP
private IServiceProvider _provider;
private IJobProcessor[] _processors;
private CancellationTokenSource _cts;
private ConsistencyOptions _options;
private CapOptions _options;
private ProcessingContext _context;
private DefaultCronJobRegistry _defaultJobRegistry;
private Task _compositeTask;
@@ -29,7 +29,7 @@ namespace DotNetCore.CAP
ILoggerFactory loggerFactory,
IServiceProvider provider,
DefaultCronJobRegistry defaultJobRegistry,
IOptions<ConsistencyOptions> options)
IOptions<CapOptions> options)
{
_logger = logger;
_loggerFactory = loggerFactory;


Loading…
取消
儲存