View
218
Download
0
Category
Preview:
Citation preview
8/3/2019 nsb-skillsmatter-20081127
1/34
Asynchronous .NETapplications with
NServiceBus
David de Florinier (dave@deflorinier.me.uk)
Gojko Adzic (gojko@gojko.com)
Skills Matter 27/11/2008
8/3/2019 nsb-skillsmatter-20081127
2/34
Don't be afraid of messaging
8/3/2019 nsb-skillsmatter-20081127
3/34
http://www.sxc.hu/photo/1084274
Fire and forget...
8/3/2019 nsb-skillsmatter-20081127
4/34
Fire and forget
Use when the second part of a process has to becompleted, but we don't really have to wait for itto finish. This typically involves an external
system or batch processing.
Resilience
Easier to test Option to go offline
8/3/2019 nsb-skillsmatter-20081127
5/34
Just leave it there when you're done
http://www.sxc.hu/photo/319039
8/3/2019 nsb-skillsmatter-20081127
6/34
Just leave it there when you're done
Use when the second part of a process is slow ortalks to an external system, but completing thetask is important to continue the work.
Better UI responsiveness
Performance
Better resource usage
8/3/2019 nsb-skillsmatter-20081127
7/34
Share the load
http://www.flickr.com/photos/library_of_congress/2179123671/
8/3/2019 nsb-skillsmatter-20081127
8/34
Share the load
Use when lots of different agents can submit orprocess work. Decouple workers andrequesters.
Performance
Scalability
More flexible
Easier to extend
8/3/2019 nsb-skillsmatter-20081127
9/34
http://www.sxc.hu/photo/1035790
The key to efficient asyncarchitectures!
8/3/2019 nsb-skillsmatter-20081127
10/34
Who? ... What? ...Where?
Written by Udi Dahan
It is an open source communications framework
Get it from http://www.nservicebus.com
Released under the Apache Licence 2.0
Current Release is 1.8
8/3/2019 nsb-skillsmatter-20081127
11/34
Getting Started
Download fromhttp://www.nservicebus.com/Downloads.aspx
Extract the files from the zip, then build using
build.bat (this uses msbuild) The assemblies will now be in the build folder
(and subfolders)
Look through the Sample projects, these coverall the simple usage cases (for Spring)
8/3/2019 nsb-skillsmatter-20081127
12/34
What is included...
8/3/2019 nsb-skillsmatter-20081127
13/34
IBuilder
Various parts of the framework (e.g. passingmessages to handlers) use a service locatorpattern.
This is done using the IBuilder interface An implementation using Spring is provided:
ObjectBuilder.SpringFramework
An exampleof a implementation for CastleWindsor is in the Demo source
8/3/2019 nsb-skillsmatter-20081127
14/34
ITransport
Responsibility for sending and receivingmessages is delegated to a transportcomponent
Unicast Implementations for MSMQ and Httpare provided
There is an IMulticastTransport interface, butthere are no implementations provided
8/3/2019 nsb-skillsmatter-20081127
15/34
IMessageSerializer
Responsibilty for serialization of messages isdelegated to a serialization component
Implementations for Binary and Xml
serialization are provided Has the following methods
void Initialize(params Type[] types);
void Serialize(IMessage[] messages, Stream stream);
IMessage[] Deserialize(Stream stream);
8/3/2019 nsb-skillsmatter-20081127
16/34
IBus
The entry point for your code
Send, Subscribe, Unsubscribe, Publish
IUnicastBus and IMulticastBus interfaces inherit
from IBus,
Use IUnicastBus with IUnicastTransport
Use IMulticastBus IMulticastTransport
8/3/2019 nsb-skillsmatter-20081127
17/34
Publish/Subscribe
Messages should implement theIMessage marker interface
Use IBus.Publish(IMessage)
Use IBus.Subscribe(Type[,predicate])
Message handlers implementIMessageHandler
Implement IBuilder interface to createan handler builder if not using
Spring.Net
8/3/2019 nsb-skillsmatter-20081127
18/34
ISubscriptionStorage
Needs to be configured for publishers
Persistence and retrieval of subscriptions isdelegated to ISubscriptionStorage
Db and Msmq implementations are provided
Db implementation allows multiple publisherinstances to share one subscription store
8/3/2019 nsb-skillsmatter-20081127
19/34
Publish/Subscribe config Always needed
Transport : NumberOfWorkerThreads, IsTransactional, InputQueue
Bus: ITransport, IMessageSerializer.
On Publisher:
SubscriptionStorage : persistence for subscriptions
Bus: ISubscriptionStorage
On Subscriber
Transport : NumberOfWorkerThreads, IsTransactional, InputQueue
no need for ISubscriptionStorage.
IBuilder must be able to build Handlers
Bus: MessageOwners: where subscription messages are sent
Bus: AddTypesFromAssembly for handler types
8/3/2019 nsb-skillsmatter-20081127
20/34
Publish Subscribe demo
8/3/2019 nsb-skillsmatter-20081127
21/34
Full Duplex
Use IBus.Send(IMessage).Register(Callback) to makeasynchronous request
Use IBus.Reply(Response) to send response
8/3/2019 nsb-skillsmatter-20081127
22/34
Full Duplex code
Sending message from the client
bus.Send(["worker@localhost", ]msg).Register(callback, msg);
Handling the message on the server
_bus.Reply(response);
Handling the callback on the clientprivate void callback(IAsyncResult ar) {
....
CompletionResult result = asyncResult.AsyncState as CompletionResult;
Message request = result.State as Message;
...
foreach (var message in result.Messages) { ...}
....
}
8/3/2019 nsb-skillsmatter-20081127
23/34
Full Duplex demo
8/3/2019 nsb-skillsmatter-20081127
24/34
Saga
Framework for long lived message workflows
Saga Messages implement ISagaMessage
Implement ISaga to handle saga messages
NserviceBus.Testing.Saga contains Mock objects for use
in unit tests
8/3/2019 nsb-skillsmatter-20081127
25/34
ISagaPersister
The responsibility of persisting and rehydratingSaga entities is delegated to a classimplementing ISagaPersister
DbBlobSagaPersister implementation provided
8/3/2019 nsb-skillsmatter-20081127
26/34
ISaga
Saga Enitity, which will handle the various stepsin the saga workflow, will implement ISagafor each workflow step
Also a class extending SagaMessageHandlermay be used. This will typically override theHandle and NeedToHandle methods.
The SagaMessageHandler uses ISagaPersister
to rehydrate the ISaga implementation, andcall it's Handle method
8/3/2019 nsb-skillsmatter-20081127
27/34
Saga interaction
8/3/2019 nsb-skillsmatter-20081127
28/34
Saga Demo
8/3/2019 nsb-skillsmatter-20081127
29/34
Distributor
A way of scaling costlytransactions
Workers report for work
Distributor sends work tofirst available worker
When workers have
completed, they report forwork again
An executable is provided
8/3/2019 nsb-skillsmatter-20081127
30/34
Conclusions
Positives Source code is well structured and easy to
understand
Main objects can be extended, flexible architecture
Has performed well in load tests
Few dependencies
Negatives
There is little documentation
8/3/2019 nsb-skillsmatter-20081127
31/34
Some alternatives?
ActiveMQ and NMS
http://activemq.apache.org/nms.html
SimpleServiceBus
http://www.codeplex.com/SimpleServiceBus
MassTransithttp://code.google.com/p/masstransit/
8/3/2019 nsb-skillsmatter-20081127
32/34
Links
http://www.nservicebus.com
http://www.udidahan.com
http://ayende.com/Blog/archive/2008/03/24/NSe
rviceBus-Review.aspx
http://gojko.net
8/3/2019 nsb-skillsmatter-20081127
33/34
Next events
TDD in .NET, 17/Dec
OpenSource .NET Exchange 22/Jan
Fitnesse.NET tips and tricks, 17/Feb
For more info: http://ukdotnet.ning.com/
8/3/2019 nsb-skillsmatter-20081127
34/34
Questions?
Recommended