0% found this document useful (0 votes)
113 views18 pages

Extending The Message Pipeline With Masstransit Middleware: Roland Guijt

This document discusses how middleware can extend the message pipeline in MassTransit. It describes how middleware is composed using filters and specifications and applied to the pipeline. It provides examples of adding common middleware like circuit breakers, rate limiters, and latest filters to handle errors, throttle message rates, and ensure the latest message is processed. Middleware allows processing and handling of messages within the pipeline in a configurable way.

Uploaded by

Phuc Vinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views18 pages

Extending The Message Pipeline With Masstransit Middleware: Roland Guijt

This document discusses how middleware can extend the message pipeline in MassTransit. It describes how middleware is composed using filters and specifications and applied to the pipeline. It provides examples of adding common middleware like circuit breakers, rate limiters, and latest filters to handle errors, throttle message rates, and ensure the latest message is processed. Middleware allows processing and handling of messages within the pipeline in a configurable way.

Uploaded by

Phuc Vinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Extending the Message Pipeline

with MassTransit Middleware

Roland Guijt
INDEPENDENT SOFTWARE DEVELOPER AND TRAINER

@rolandguijt www.rmgsolutions.nl
Module
Overview Middleware and pipeline
Composition of middleware
Circuit breaker
Rate limiter
Latest filter
Used to process messages
Consist of asynchronous middleware

Pipelines Configure using configurator


Extend the pipeline
Out-of-the-box or custom-made
A Pipeline

MassTransit Added
middleware middleware

Probe context
Composition of Middleware (1/3)
public class MyFilter<T> : IFilter<T>
where T : class, PipeContext
{
public void Probe(ProbeContext context)
{
//Manipulate probe context
}

public async Task Send(T context, IPipe<T> next)


{
//do something before next middlewares
await next.Send(context);
//do something after next middlewares
}
}
Composition of Middleware (2/3)

public class MyFilterSpec<T>: IPipeSpecification<T>


where T : class, PipeContext
{
public IEnumerable<ValidationResult> Validate()
{
//perform validation
}

public void Apply(IPipeBuilder<T> builder)


{
builder.AddFilter(new MyFilter<T>())
}
}
Composition of Middleware (3/3)

public static class ExampleMiddlewareConfiguratorExtensions


{
public static void UseMyFilter<T>(
this IPipeConfigurator<T> configurator)
where T : class, PipeContext
{
configurator.AddPipeSpecification(
new MyFilterSpec<T>());
}
}
Adding Middleware to the Pipeline

var bus = BusConfigurator.ConfigureBus((cfg, host) =>


{
cfg.ReceiveEndpoint(host,queuename, e =>
{
e.Consumer<ConsumerType>();
});
});
Adding Middleware to the Pipeline

var bus = BusConfigurator.ConfigureBus((cfg, host) =>


{
cfg.UseMyFilter();
cfg.ReceiveEndpoint(host,queuename, e =>
{
e.Consumer<ConsumerType>();
});
});
Adding Middleware to the Pipeline

var bus = BusConfigurator.ConfigureBus((cfg, host) =>


{
cfg.ReceiveEndpoint(host,queuename, e =>
{
e.UseMyFilter();
e.Consumer<ConsumerType>();
});
});
Adding Middleware to the Pipeline

var bus = BusConfigurator.ConfigureBus((cfg, host) =>


{
cfg.ReceiveEndpoint(host,queuename, e =>
{
e.Consumer<ConsumerType>(c => c.UseMyFilter());
});
});
Circuit Breaker

Timeout Exception

Consumer Web service


Circuit Breaker

Circuit breaker

Timeout
Exception

Consumer Web service


Circuit Breaker in Code

cfg.ReceiveEndpoint(host, queue, e =>


{
e.UseCircuitBreaker(cb =>
{
cb.TripThreshold = 15;
cb.ResetInterval = TimeSpan.FromMinutes(5);
cb.TrackingPeriod = TimeSpan.FromMinutes(1);
cb.ActiveThreshold = 10;
});
}
Rate Limiter

Rate Limiter
100 messages
per second

Consumer Web service


Rate Limiter in Code

cfg.ReceiveEndpoint(host, queuename, e =>


{
e.UseRateLimit(100, TimeSpan.FromSeconds(1));
});
Latest Filter in Code

//class level
private ILatestFilter<ConsumeContext> latestContext;

cfg.ReceiveEndpoint(host, queuename, e =>


{
e.UseLatest(lg => lg.Create =
filter => latestContext = filter);
});
Summary How middleware and pipeline fit together
Middleware composition
Circuit breaker
Rate limiter
Latest filter

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy