0% found this document useful (0 votes)
83 views25 pages

Implementing A Service Bus With Masstransit: Roland Guijt

This document discusses implementing a service bus with MassTransit. It covers service bus concepts, sending and receiving messages, endpoints and queues, monitoring messages, handling failures, and request/response patterns. MassTransit provides features like scheduling, dependency injection, performance counters, and retry policies. It uses a service bus approach for messaging that hides transport complexity and supports multiple transports like RabbitMQ.

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)
83 views25 pages

Implementing A Service Bus With Masstransit: Roland Guijt

This document discusses implementing a service bus with MassTransit. It covers service bus concepts, sending and receiving messages, endpoints and queues, monitoring messages, handling failures, and request/response patterns. MassTransit provides features like scheduling, dependency injection, performance counters, and retry policies. It uses a service bus approach for messaging that hides transport complexity and supports multiple transports like RabbitMQ.

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/ 25

Implementing a Service Bus

with MassTransit

Roland Guijt
INDEPENDENT SOFTWARE DEVELOPER AND TRAINER

@rolandguijt www.rmgsolutions.nl
Sending and receiving
Module Service bus concepts
Overview Type support
Scheduling
Monitoring
Dependency Injection
Failure
Request/Response
Service Bus Framework for .NET
Endpoints and queues
Not like Biztalk
Gateway to transport
Multiple transports
Optimized for testing
Built-in features
Service Bus vs Native Transport API

Service Bus Native Transport API


A ready to go framework for messaging Low level: Create framework yourself
Hides complexity of transport Exposure to complexity of transport
Part of transport features supported Full transport feature support
More geared towards type system Heavy use of strings
Supports multiple transports One transport supported
Easy to unit test Not designed with unit testing in mind
MassTransit and RabbitMQ

Exchange
Queue 1
For queue 1

Exchange Exchange
Queue 2
Message type For queue 2

Exchange
Queue 3
For queue 2
Delivery of messages at a later time

Scheduling Quartz.net

Messages MassTransit Quartz service


In memory
Scheduling in Code

cfg.UseMessageScheduler(new Uri(“rabbitmq://localhost/quartz”));
or
cfg.UseInMemoryMessageScheduler();

context.ScheduleMessage(destination, when, message);


or
schedulerEndpoint.ScheduleSent(destination, when, message);
RabbitMQ management plugin

Monitoring Observer interfaces


Performance counters
Intercept messages
Read only

Observer IReceiveObserver

Interfaces IConsumeObserver
IConsumeMessageObserver<T>
ISendObserver
IPublishObserver
Message Observers in Code
public interface ISendObserver

Task Presend<T>(SendContext<T> context);

` Task PostSend<T>(SendContext<T> context);

Task SendFault<T>(SendContext<T> context, Exception exception);

var observer = new SendObserver();

bus.ConnectSendObserver(observer);
Observes bus activities

Bus Observer Implement IBusObserver


Register with cfg.BusObserver
Performance Counters

bus.EnablePerformanceCounters();
Avoid using bus object
Extra NuGet package
Dependency Adds extension methods to
Injection IReceiveEndpointConfigurator
Autofac, Ninject, StructureMap, Unity,
Castle Windsor
Dependency Injection in Code

//create container
//register consumers
cfg.ReceiveEndpoint(“queuename”, e =>
{
e.LoadFrom(container);
or
e.Consumer<ConsumerType>(container);
}
Connection management
Skipped queue
Responding Retries
to Failure
Error queue
Fault<T> message
Happy Flow

Producer Queue Consumer


Delivery Problem

Producer Queue

queue_skipped
Consumer Problem

Producer Queue Consumer

Retry
policy

Fault<T>
message queue_error
How to Specify Fault and Response Addresses

context.Publish<IOrderRegisteredEvent>(orderRegisteredEvent,
c => c.FaultAddress = urlToEndpoint);
context.Publish<IOrderRegisteredEvent>(orderRegisteredEvent,
c => c.ResponseAddress = urlToEndpoint);
How to Set a Retry Policy

cfg.ReceiveEndpoint(host, queuename, e =>


{
e.UseRetry(Retry.Except<ArgumentException>().Immediate(20));
e.Consumer<OrderRegisteredConsumer>();
});
Exception Selectors

Except Selected

All Filter
Retry Policies

Immediate Intervals

Exponential Incremental
Producer waits until consumer replies
Against asynchronous nature of
Request/ Microservices
Response Supported with MessageRequestClient
Awaitable
Request/Response in Code

var client = new MessageRequestClient<commandMessageType,


resultMessageType>(bus, address, requestTimeout);

var result = await client.Request(commandObject);


Summary How to send and receive messages
Service bus pros and cons
MassTransit features
Failure
Request/Response

You might also like