nservicebus alt.net 20100511

49
Building enterprise applications with NServiceBus Andreas Öhlund onsdag den 12 maj 2010

Upload: andreasohlund

Post on 13-Jan-2015

2.634 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: NServiceBus Alt.Net 20100511

Building enterprise applications with

NServiceBusAndreas Öhlund

onsdag den 12 maj 2010

Page 2: NServiceBus Alt.Net 20100511

What is a distributed system?

“A distributed system is one in which the failure of a computer you didn't even know existed can render your own computer unusable”

-Leslie Lamport

onsdag den 12 maj 2010

Page 3: NServiceBus Alt.Net 20100511

The first rule of NServiceBus

onsdag den 12 maj 2010

Page 4: NServiceBus Alt.Net 20100511

The first rule of NServiceBus

There is NO Service Bus!

onsdag den 12 maj 2010

Page 5: NServiceBus Alt.Net 20100511

Well, at least no physical one

onsdag den 12 maj 2010

Page 6: NServiceBus Alt.Net 20100511

The second rule of NServiceBus

onsdag den 12 maj 2010

Page 7: NServiceBus Alt.Net 20100511

The second rule of NServiceBus

You DO NOT usesynchronous communication

onsdag den 12 maj 2010

Page 8: NServiceBus Alt.Net 20100511

The key to robust communication

onsdag den 12 maj 2010

Page 9: NServiceBus Alt.Net 20100511

Store and forward

onsdag den 12 maj 2010

Page 10: NServiceBus Alt.Net 20100511

Request/Response the NServiceBus way

Fire and forget X 2

onsdag den 12 maj 2010

Page 11: NServiceBus Alt.Net 20100511

The effect of tight coupling

TemporalBehavioral

onsdag den 12 maj 2010

Page 12: NServiceBus Alt.Net 20100511

Communication and coupling

onsdag den 12 maj 2010

Page 13: NServiceBus Alt.Net 20100511

Coupling NServiceBus style

onsdag den 12 maj 2010

Page 14: NServiceBus Alt.Net 20100511

Command oriented communication

OnlineSales OrderService

CompleteSaleCommand

onsdag den 12 maj 2010

Page 15: NServiceBus Alt.Net 20100511

Sending MessagesIBus.Send()

OnlineSales OrderService

CreateOrderCommandbus.Send(new CompleteSaleCommand

{ProductId = 123,Quantity = 10....

});

onsdag den 12 maj 2010

Page 16: NServiceBus Alt.Net 20100511

Receiving and processing of messages

OnlineSales OrderService

CreateOrderCommandpublic class CompleteSaleMessageHandler:

IHandleMessages<CompleteSaleCommand>{

public void Handle(CompleteSaleCommand c){

...}

}

onsdag den 12 maj 2010

Page 17: NServiceBus Alt.Net 20100511

Message Contracts

• Versioned by the owner

• Regular .Net Assembly

• Bound to a input queue

• class MyMessage : IMessage

onsdag den 12 maj 2010

Page 18: NServiceBus Alt.Net 20100511

Throttling the load• Manage traffic peaks

• Consumer controls the pace

• <MsmqTransportConfig NumberOfWorkerThreads="3" />

onsdag den 12 maj 2010

Page 19: NServiceBus Alt.Net 20100511

What happens when the load becomes to heavy for one server?

onsdag den 12 maj 2010

Page 20: NServiceBus Alt.Net 20100511

Scaling with the Distributor

onsdag den 12 maj 2010

Page 21: NServiceBus Alt.Net 20100511

Building consistent systems

• Do we produce predictable results even under failure conditions?

• What about non transactional sources?

onsdag den 12 maj 2010

Page 22: NServiceBus Alt.Net 20100511

Isolate non transactional sources as separate endpoints

public void Handle(CompleteSaleCommand cmd){ var order = ....

orderRepository.Save(order)

smtpClient.Send(new MailMessage{...})}

onsdag den 12 maj 2010

Page 23: NServiceBus Alt.Net 20100511

Isolate non transactional sources as separate endpoints

public void Handle(CompleteSaleCommand cmd){ var order = ....

orderRepository.Save(order)

smtpClient.Send(new MailMessage{...})}

public void Handle(CompleteSaleCommand cmd){ var order = ....

orderRepository.Save(order)

bus.Send(new NotifyCustomerRequest { EmailAddress = customer.Email, Body = “Order confirmation” });

}

onsdag den 12 maj 2010

Page 24: NServiceBus Alt.Net 20100511

Idempotent

“Idempotent operations are operations that can be applied multiple times without changing the result”

onsdag den 12 maj 2010

Page 25: NServiceBus Alt.Net 20100511

When things go wrong

onsdag den 12 maj 2010

Page 26: NServiceBus Alt.Net 20100511

Messaging gives you a chance to do better

• No more showing the users a WSOD

• Messages are replayable

• Async communication opens up a crucial windows of time for corrections

onsdag den 12 maj 2010

Page 27: NServiceBus Alt.Net 20100511

Event oriented communication

onsdag den 12 maj 2010

Page 28: NServiceBus Alt.Net 20100511

Publish and subscribe

• Everyone get’s a copy

• Removes behavioral coupling

onsdag den 12 maj 2010

Page 29: NServiceBus Alt.Net 20100511

Business eventsSales

SalesService

Billing

BillingService

Shipping

ShippingService

OrderAcceptedEvent

onsdag den 12 maj 2010

Page 30: NServiceBus Alt.Net 20100511

Finally a chance to discuss non functional requirements that business people understand

onsdag den 12 maj 2010

Page 31: NServiceBus Alt.Net 20100511

The mechanics of pub/sub

onsdag den 12 maj 2010

Page 32: NServiceBus Alt.Net 20100511

The mechanics of pub/sub

onsdag den 12 maj 2010

Page 33: NServiceBus Alt.Net 20100511

Becoming a subscriber

bus.Subcribe<IOrderAcceptedEvent>();

onsdag den 12 maj 2010

Page 34: NServiceBus Alt.Net 20100511

Publishingbus.Publish<IOrderAcceptedEvent>(x=>

{ProductId = 123,Quantity = 10....

});

onsdag den 12 maj 2010

Page 35: NServiceBus Alt.Net 20100511

It’s hard to get it right the first time around

onsdag den 12 maj 2010

Page 36: NServiceBus Alt.Net 20100511

Versioning events using interfaces

public interface IOrderAcceptedEvent2 : IOrderAcceptedEvent{

string SomeNewProperty{ get;set; }}

onsdag den 12 maj 2010

Page 37: NServiceBus Alt.Net 20100511

Long running transactions

onsdag den 12 maj 2010

Page 38: NServiceBus Alt.Net 20100511

Use sagas to model long running transactions

public  class  MySaga  :  Saga<MySagaData>,        IAmStartedByMessages<Message1>,        IHandleMessages<Message2>{        public  void  Handle(Message1  message)        {                //  code  to  handle  Message1        }

       public  void  Handle(Message2  message)        {                //  code  to  handle  Message2        }}

onsdag den 12 maj 2010

Page 39: NServiceBus Alt.Net 20100511

Storing state

public  class  MySagaData  :  IContainSagaData{        //  the  following  properties  are  mandatory        public  virtual  Guid  Id  {  get;  set;  }        public  virtual  string  Originator  {  get;  set;  }        public  virtual  string  OriginalMessageId  {  get;  set;  }}

onsdag den 12 maj 2010

Page 40: NServiceBus Alt.Net 20100511

Timeoutspublic  void  Handle(Message1  message){        this.Data.SomeID  =  message.SomeID;

       RequestTimeout(TimeSpan.FromHours(1),  "some  state");}

public  override  void  Timeout(object  state){      //  some  business  action  like:      if  (!Data.Message2Arrived)            ReplyToOriginator(new  TiredOfWaitingForMessage2());}

onsdag den 12 maj 2010

Page 41: NServiceBus Alt.Net 20100511

Configuration

Developers: use codeAdministrators: use config files

onsdag den 12 maj 2010

Page 42: NServiceBus Alt.Net 20100511

Hosting options

• Custom hosting

• Website

• Smartclient

• Commandline

• Generic host

onsdag den 12 maj 2010

Page 43: NServiceBus Alt.Net 20100511

Using the Generic Hostpublic class EndpointConfig: IConfigureThisEndpoint, AsA_Publisher

{

}

onsdag den 12 maj 2010

Page 44: NServiceBus Alt.Net 20100511

Using profiles to adapt to different environments

onsdag den 12 maj 2010

Page 45: NServiceBus Alt.Net 20100511

NServiceBus role in CQRS Style applications

onsdag den 12 maj 2010

Page 46: NServiceBus Alt.Net 20100511

NServiceBus is opinionated

onsdag den 12 maj 2010

Page 47: NServiceBus Alt.Net 20100511

Why all these opinions?

onsdag den 12 maj 2010

Page 48: NServiceBus Alt.Net 20100511

Don’t use messaging for queries

• Queries need to return relatively fast

• No benefit from the robustness that NSB gives

• Use NSB to cache data close and use native apis to get at that data

onsdag den 12 maj 2010

Page 49: NServiceBus Alt.Net 20100511

The end

• www.nservicebus.com

• www.udidahan.com/blog

• andreasohlund.blogspot.com

onsdag den 12 maj 2010