Skip to content

ravenyue/Hangfire.SpecifyQueue

Repository files navigation

Hangfire.SpecifyQueue

Hangfire创建任务时,可以指定队列名称

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddHangfire(config =>
    {
        config.UseSqlServerStorage("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=hangfire;Integrated Security=True");
        config.UseFilter(new AutomaticRetryAttribute { Attempts = 1 });
    });

    services.AddHangfireSpecifyQueue();

    services.AddHangfireServer(options =>
    {
        options.ServerName = "SpecifyQueue_Sample";
        options.Queues = new string[] { "hello" };
    });

    services.AddTransient<IHelloService, HelloService>();
}

Crete Job

using Hangfire;
using Hangfire.SpecifyQueue;

public class SomeService
{
    private readonly IBackgroundJobClient _jobClient;

    public SomeService(IBackgroundJobClient jobClient)
    {
        _jobClient = jobClient;
    }

    public string Enqueue()
    {
        var jobId = _jobClient.Enqueue<IHelloService>(queueName: "hello", x => x.Hello("jack"));

        return jobId
    }

    public string Delay()
    {
        var jobId = _jobClient.Schedule<IHelloService>(
                    queueName: "hello",
                    methodCall: x => x.Hello("jack"),
                    delay: TimeSpan.FromSeconds(5));

        return jobId;
    }
}

About

Hangfire创建任务时,可以指定队列名称

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages