我有一个 .NET Core 6 Hangfire 作业项目,并且我使用两个数据库,这没有问题。但是两个仪表板具有相同的队列名称; “默认”。
我需要将每个服务器定义为不同的队列名称,但我不能。我需要帮助。
这是我的代码:
var hangfireUserStorageOptions = new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true,
SchemaName = "HangFireUser"
};
var hangfireSystemStorageOptions = new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true,
SchemaName = "HangFireSystem"
};
var hangfireUserStorage = new SqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireUserStorageOptions);
var hangfireSystemStorage = new SqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireSystemStorageOptions);
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireSystemStorageOptions));
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(_configuration.GetSection("HangfireSettings:HangfireConnection").Value, hangfireUserStorageOptions));
builder.Services.AddHangfireServer(hangfireSystemStorage); // I need to define queue name "Q1"
builder.Services.AddHangfireServer(hangfireUserStorage); // I need to define queue name "Q2"
要实现此目的,您应该在作业方法上设置
QueueAttribute
,然后为这些作业指定所需的队列名称。
[Queue("Q1")]
Hangfire
将确保作业由配置了相应队列名称的服务器处理。
当您将作业入队时,请务必指定相应的队列名称