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

Add Service Bus Sessions Example #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Sample.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void ConfigureServices(IServiceCollection services)
{
cfg.Host(Configuration.GetConnectionString("AzureServiceBus"));

cfg.Publish<OrderShippedBase>(configurator => configurator.Exclude = true);
cfg.Send<OrderShipped>(s => s.UseSessionIdFormatter(c => c.Message.OrderId.ToString("D")));
});
});
Expand Down
6 changes: 5 additions & 1 deletion src/Sample.Contracts/OrderShipped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ namespace Sample.Contracts
using System;


public record OrderShipped
public record OrderShippedBase
{
public Guid OrderId { get; init; }
public DateTimeOffset Timestamp { get; init; }
}

public record OrderShipped : OrderShippedBase
{
}
}
25 changes: 25 additions & 0 deletions src/Sample.Worker/Consumers/OrderShippedConsumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using MassTransit;
using Microsoft.Extensions.Logging;
using Sample.Contracts;

namespace Sample.Worker.Consumers
{
public class OrderShippedConsumer :
IConsumer<OrderShipped>
{
private readonly ILogger<OrderShippedConsumer> _logger;

public OrderShippedConsumer(ILogger<OrderShippedConsumer> logger)
{
_logger = logger;
}

public Task Consume(ConsumeContext<OrderShipped> context)
{
_logger.LogInformation("Order Shipped with OrderId: {OrderId}. SessionId {SessionId}.",
context.Message.OrderId, context.SessionId());
return Task.CompletedTask;
}
}
}
19 changes: 19 additions & 0 deletions src/Sample.Worker/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

namespace Sample.Worker
{
using System.Threading.Tasks;
Expand Down Expand Up @@ -29,6 +31,10 @@ public static IHostBuilder CreateHostBuilder(string[] args)

x.AddConsumer<SubmitOrderConsumer>();
x.AddConsumer<OrderSubmittedConsumer>();
x.AddConsumer<OrderShippedConsumer>(e =>
{
e.UseTimeout(c => c.Timeout = TimeSpan.FromSeconds(10));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really used and probably just confuses people.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't it fail the SB and not ack of the message handling takes to long?
Ie it will ensure failed process are put to failed?

But you are right it can be found easy enough, not needed here

});

x.AddSagaStateMachine<OrderShipmentStateMachine, OrderShipmentState, OrderShipmentSagaDefinition>()
.MessageSessionRepository();
Expand All @@ -48,6 +54,19 @@ public static IHostBuilder CreateHostBuilder(string[] args)
e.ConfigureConsumer<OrderSubmittedConsumer>(context);
});

cfg.Publish<OrderShippedBase>(configurator => configurator.Exclude = true);
cfg.SubscriptionEndpoint<OrderShipped>("order-shipped-consumer", e =>
{
e.ConfigureConsumeTopology = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also unnecessary noise.

Copy link
Author

@rsr-maersk rsr-maersk Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is needed otherwise it will make unwanted service bus topics for interfaces and inheritance

You have a discussion recommending this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm referring to the ConfigureConsumeTopology = false

e.AutoStart = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary, noise.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True

e.AutoDeleteOnIdle = TimeSpan.FromDays(30);
e.MaxDeliveryCount = 2;
e.RequiresSession = true;
e.MaxConcurrentCalls = 8; //like AZ Function default. 8 Concurrent consumers handling 1 ServiceBus Session
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, no, MaxConcurrentCallsPerSession is the number of concurrent calls per session, which defaults to unassigned.

ConcurrentMessageLimit ultimately maps down to MaxConcurrentCalls at the transport level. I would avoid using the SB specific property here, but it's a subscription endpoint, so whatever.

Copy link
Author

@rsr-maersk rsr-maersk Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConcurrentMessageLimit didn't respect sessions.
We had same session id coming on 2 threads.

I have a test / sample proving this. I will add here tomorrow

e.PrefetchCount = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a valid value you should specify.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you are right, it isn't needed AZ funcs prefect too with peek, as long as the prefect does not ack and respects sessions.
I believe it does, yes?

e.ConfigureConsumer<OrderShippedConsumer>(context);
});

cfg.ConfigureEndpoints(context);
});
});
Expand Down