-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
Currently does not support separate database Close this issue, regarding multi-tenant support, we will have a unified discussion into #699 |
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();
}
} |
Closed
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The text was updated successfully, but these errors were encountered: