Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to init multiple CAP instances in one application? #998

Closed
HengzheLi opened this issue Sep 9, 2021 · 2 comments
Closed

Comments

@HengzheLi
Copy link

HengzheLi commented Sep 9, 2021

My case is that some of messages are less importance and can be missed in Client side. Some of messages are importance and it have to be handled by Client.
So I want to use one CAP instance to delivery less importance messages. This instance use memory as storage and these messages have short duration of lifetime and no need to retry to send when the sending is fail.
And another CAP instance to delivery more importance message. This instance use database as storage and these messages have long duration of lifetime and need the commitment from Client side.

I saw the documentation guides to use the CAP throught the IOC, and only one CAP instance is constructed. Looks like it only support one CAP instance per application.

@yang-xiaodong
Copy link
Member

Currently does not support separate database

Close this issue, regarding multi-tenant support, we will have a unified discussion into #699

@yang-xiaodong
Copy link
Member

yang-xiaodong commented Dec 8, 2021

A carzy way to anyone who arrives here.

MySqlRabbitMqCapPublisher.cs

public class MySqlRabbitMqCapPublisher : ICapPublisher
{
    public static readonly MySqlRabbitMqCapPublisher instance = new MySqlRabbitMqCapPublisher();

    public static MySqlRabbitMqCapPublisher Instance
    {
        get
        {
            return instance;
        }
    }

    private readonly ICapPublisher _capPublisher;

    public IServiceProvider ServiceProvider { get; init; }

    private MySqlRabbitMqCapPublisher()
    {
        var services = new ServiceCollection();

        services.AddLogging(q =>
        {
            q.AddConsole();                
        });

        services.AddCap(cap =>
        {
            cap.UseMySql(ConnectionString);
            cap.UseRabbitMQ("");
        }); 

        ServiceProvider = services.BuildServiceProvider();
        _capPublisher = ServiceProvider.GetRequiredService<ICapPublisher>();
        ServiceProvider.GetRequiredService<IBootstrapper>().BootstrapAsync().Wait();
    }

    public AsyncLocal<ICapTransaction> Transaction => _capPublisher.Transaction;        

    public void Publish<T>(string name, T contentObj, string callbackName = null)
    {
        _capPublisher.Publish(name, contentObj, callbackName);
    }

    public void Publish<T>(string name, T contentObj, IDictionary<string, string> headers)
    {
        _capPublisher.Publish(name, contentObj, headers);
    }

    public async Task PublishAsync<T>(string name, T contentObj, string callbackName = null, CancellationToken cancellationToken = default)
    {
        await _capPublisher.PublishAsync(name, contentObj, callbackName, cancellationToken);
    }

    public async Task PublishAsync<T>(string name, T contentObj, IDictionary<string, string> headers, CancellationToken cancellationToken = default)
    {
        await _capPublisher.PublishAsync(name, contentObj, headers, cancellationToken);
    }
}

SqlServerRabbitMqCapPublisher .cs

public class SqlServerRabbitMqCapPublisher : ICapPublisher
{
    public static readonly SqlServerRabbitMqCapPublisher instance = new SqlServerRabbitMqCapPublisher();

    public static SqlServerRabbitMqCapPublisher Instance
    {
        get
        {
            return instance;
        }
    }
    private readonly ICapPublisher _capPublisher;

    public IServiceProvider ServiceProvider { get; init; }

    static SqlServerRabbitMqCapPublisher()
    {

    }

    private SqlServerRabbitMqCapPublisher()
    {
        var services = new ServiceCollection();

        services.AddLogging(q =>
        {
            q.AddSimpleConsole();
            q.AddConsole();                
        });

        services.AddCap(cap =>
        {
            cap.UseSqlServer(ConnectionString);
            cap.UseRabbitMQ("");
        });

        ServiceProvider = services.BuildServiceProvider();
        _capPublisher = ServiceProvider.GetRequiredService<ICapPublisher>();
        ServiceProvider.GetRequiredService<IBootstrapper>().BootstrapAsync().Wait();
    }

    public AsyncLocal<ICapTransaction> Transaction => _capPublisher.Transaction;        

    public void Publish<T>(string name, T contentObj, string callbackName = null)
    {
        _capPublisher.Publish(name, contentObj, callbackName);
    }

    public void Publish<T>(string name, T contentObj, IDictionary<string, string> headers)
    {
        _capPublisher.Publish(name, contentObj, headers);
    }

    public async Task PublishAsync<T>(string name, T contentObj, string callbackName = null, CancellationToken cancellationToken = default)
    {
        await _capPublisher.PublishAsync(name, contentObj, callbackName, cancellationToken);
    }

    public async Task PublishAsync<T>(string name, T contentObj, IDictionary<string, string> headers, CancellationToken cancellationToken = default)
    {
        await _capPublisher.PublishAsync(name, contentObj, headers, cancellationToken);
    }
}

Usage

[Route("api/[controller]")]
public class ValuesController : Controller
{
    private readonly SqlServerRabbitMqCapPublisher _sqlserver;
    private readonly MySqlRabbitMqCapPublisher _mysql;

    public ValuesController()
    {
         _sqlserver = SqlServerRabbitMqCapPublisher.Instance;
        _mysql = MySqlRabbitMqCapPublisher.Instance;
    }

    [Route("~/without/sqlserver")]
    public async Task<IActionResult> WithoutTransaction()
    {
        await _sqlserver.PublishAsync("sample.rabbitmq.sqlserver", 123123);

        return Ok();
    }

    [Route("~/without/mysql")]
    public async Task<IActionResult> WithoutTransaction2()
    {
        await _mysql.PublishAsync("sample.rabbitmq.mysql", 123123);

        return Ok();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants