Skip to content

Commit

Permalink
Use IBackgroundJobClient instead of BackgroundJob
Browse files Browse the repository at this point in the history
  • Loading branch information
gwerren committed Apr 21, 2020
1 parent 776be74 commit 7e7d299
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions HangFireServiceBusFacade.Web/ServiceBus/MessagePublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ IMessageTypeConfigurator<TMessage> Consumer<TConsumer>()

public class MessagePublisher : IMessagePublisher
{
private readonly IBackgroundJobClient backgroundJobClient;

private readonly IDictionary<Type, IMessageConsumerSet> consumerSetsByMessageType
= new Dictionary<Type, IMessageConsumerSet>();

public MessagePublisher(IBackgroundJobClient backgroundJobClient)
{
this.backgroundJobClient = backgroundJobClient;
}

public MessagePublisher For<TMessage>(Action<IMessageTypeConfigurator<TMessage>> configure)
where TMessage: class, new()
where TMessage : class, new()
{
if (!this.consumerSetsByMessageType.TryGetValue(typeof(TMessage), out var consumerSet))
{
Expand All @@ -35,7 +42,7 @@ public MessagePublisher For<TMessage>(Action<IMessageTypeConfigurator<TMessage>>
public Task Publish<TMessage>(TMessage message) where TMessage : class, new()
{
if (this.consumerSetsByMessageType.TryGetValue(typeof(TMessage), out var consumerSet))
((MessageConsumerSet<TMessage>)consumerSet).Publish(message);
((MessageConsumerSet<TMessage>)consumerSet).Publish(this.backgroundJobClient, message);

return Task.CompletedTask;
}
Expand All @@ -54,24 +61,24 @@ public IMessageTypeConfigurator<TMessage> Consumer<TConsumer>()
return this;
}

public void Publish(TMessage message)
public void Publish(IBackgroundJobClient backgroundJobClient, TMessage message)
{
foreach (var consumer in this.consumers)
consumer.Publish(message);
consumer.Publish(backgroundJobClient, message);
}

private interface IConsumerWrapper
{
void Publish(TMessage message);
void Publish(IBackgroundJobClient backgroundJobClient, TMessage message);
}

private class ConsumerWrapper<TConsumer> : IConsumerWrapper
where TConsumer : IMessageConsumer<TMessage>
{
public void Publish(TMessage message)
public void Publish(IBackgroundJobClient backgroundJobClient, TMessage message)
{
// Here we create the HangFire jobs for the registered consumers
BackgroundJob.Enqueue<TConsumer>(c => c.Consume(message));
backgroundJobClient.Enqueue<TConsumer>(c => c.Consume(message));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion HangFireServiceBusFacade.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void ConfigureServices(IServiceCollection services)
// Add the message publisher and configure the message consumers, note
// that it is possible to have multiple consumers for a single message.
services.AddSingleton<IMessagePublisher>(
o => new MessagePublisher()
o => new MessagePublisher(o.GetService<IBackgroundJobClient>())
.For<TestEvent>(
messageConfig => messageConfig
.Consumer<TestEventConsumer1>()
Expand Down

0 comments on commit 7e7d299

Please sign in to comment.