SlideShare a Scribd company logo
1 of 34
Download to read offline
NSERVICEBUS

Inspired by: The course authored by Udi Dahan




                 Oslo/Fagdag
          Espen Ekvang/Tomas Jansson
                  01/03/2013
AGENDA                   2




• Intro
• Messaging and queues
• Testing
• SOA
• Saga
INTRO                                  4




• Fallacies of distributed computing
• Why NServiceBus?
• Bus vs. Broker
• Service orientation
• Excercises
FALLACIES OF DISTRIBUTED COMPUTING                       5




1.   The network is reliable
2.   Latency isn’t a problem
3.   Bandwidth isn’t a problem
4.   The network is secure
5.   The topology won’t change
6.   The administrator will know what to do
7.   Transport cost isn’t a problem
8.   The network is homogeneous




            Cant’ assume WHEN the message will arrive,
                                  IF AT ALL
WHY NSERVICEBUS                                                                      6




1.   The network is reliable
2.   Latency isn’t a problem             NServiceBus addresses the first five directly
3.   Bandwidth isn’t a problem
4.   The network is secure
5.   The topology won’t change
6.   The administrator will know what to do
7.   Transport cost isn’t a problem
8.   The network is homogeneous




The most developer-friendly service bus for SOA on .NET
BUS VS. BROKER                                                         7



•    Bus is not necessarily physically separate
•    Simpler; no routing or service fail over
•    No single point of failure
                                                        App

                App
               Buss.dll




                                                        Broker




     App                   App
    Buss.dll              Buss.dll                App            App
TENETS OF SERVICE ORIENTATION                    8



•   Services are autonomous
•   Share contract & schema, not class or type
•   Boundaries are explicit
•   Compatibitility is base on Policy
LAYERS & COUPLING                                    9




                                  Tight coupling
                                 Loose coupling

                    Sales    Shipping      CRM
   UI
   BL
   DAL

     DB                     Referential Integrity
                             Reintroduces coupling
WHEN CAN I WRITE SOME CODE?                                               10




• Getting started
  •   New class library
  •   Install-Package NServiceBus.Host
• Logging
  •   NServiceBus uses log4net, you can configure logging in app.config
  •   Default output is console
EXERCISES     11




HELLO WORLD


LOGGING
MESSAGING AND QUEUES           12




• Store & forward
• Dangers of store & forward
• Request/Response
• Messaging and NServiceBus
• Exercises
STORE & FORWARD                                                13



                                SERVER




                                MSMQ
         OUTGOING                                   INCOMING




                    Store & Forward writes to disk
                    Resilient in the face of failures




                                MSMQ
         OUTGOING                                   INCOMING




                                CLIENT
DANGERS OF STORE & FORWARD                                                           14




• If target is offline for an extended period of timemessages can fill up the disk
  • Can cause a server to crash


• Especially problematic in B2B integration
  • 1 MB/message, 100 message/sec = 6GB/minute


• Solution – discard messages after a while
  •   Use [TimeToBeReceived("00:01:00")] on the message definition
REQUEST/RESPONSE                                                     15



                               SERVER




                               MSMQ
                    OUTGOING            INCOMING




Client can’t assume
when a response                                    Equivalent to 2
will arrive, if at all                             one-way messages




                               MSMQ
                    OUTGOING            INCOMING




                               CLIENT
REQUEST/RESPONSE                                                                  16




• Message is sent from the server to the client’s queue
  If the client is offline, message sits in the server machine’s outgoing queue

• Client is not blocked until response arrives

• If two requests were sent, responses may arrive out of order
WARNING! THIS IS NOT RPC                                                                17




• Do NOT try to implement regular request/response patterns on top of messaging
• The client should be designed so that it can continue operating if a response never
  comes


Differences from RPC
• RPC is easy to code
  • After invoking a web service
  • Next line of code assumes we’ve got a response


• RPC problems
  • Can’t reason about the time between one line of code and another


• Messaging makes this all explicit
MESSAGING AND NSERVICEBUS                  18



                       SERVER




                       MSMQ
            OUTGOING            INCOMING


   Transaction




                       MSMQ
            OUTGOING            INCOMING




                       CLIENT
DEFINE A MESSAGE                                                         19




• Preferably inherit from IEvent or ICommand


• Use IMessage when replying using Bus.Reply()


• Also possible to define your own convention
               •   Configure.DefiningMessagesAs(t=>MyOwnConvention(t))


• Add properties like a regular class/interface


• Keep contract definintions in their own assembly/project



public class MyEvent: IEvent              {}
INSTANTIATE A MESSAGE                                20




• var myMessage = new MyMessage();

• var myMessage = Bus.CreateInstance<MyMessage>();
SEND A MESSAGE                                               21




Bus.Send(messageObject);



Can instantiate and send together (useful for interfaces):

Bus.Send<IMessage>((message) =>
{
    message.Prop1 = value1;
    message.Prop2 = value2;
});
SPECIFY DESTINATION                                                    22




1. Bus.Send(destination, messages);
   Requires that application manages routing



2. Configure destination for message type.
   In <UnicastBusConfig>, under <MessageEndpointMappings> specify one of
   the following:
   - <add Messages="assembly" Endpoint="destination"/>
   - <add Messages="type" Endpoint="destination"/>


3. Specify destination using
   - QueueName@ServerName , or
   - Just QueueName for the same machine
HANDLE A MESSAGE                                                             23




Write a class that implements IHandleMessages<T> where T is a message type



public class MyHandler : IHandleMessages<MyMessage>
{
    public void Handle(MyMessage message)
    {


    }
}

Remember to specify in <UnicastBusConfig>, under
<MessageEndpointMappings> one of the following:
- <add Messages="assembly" Endpoint="source"/>
- <add Messages="type" Endpoint="source"/>
CONFIGURING AN ENDPOINT                                                                  24




When configuring an endpoint inherit from
1. Using AsA_Client will

   - use non-transactional MsmqTransport
   - purge its queue of messages on startup
   - processes messages using its own permissions, not those of the message sender


2. Using AsA_Server will

   - use transactional MsmqTransport
   - not purge its queue of messages on startup, hence fault-tolerant
   - processes messages using the permissions of the sender (impersonation)


3. Using AsA_Publisher will

   - extends AsA_Server
   - indicates to the infrastructure that a storage for subscription request is to be set up
EXERCISES                      25




ONE-WAY MESSAGING (CLIENT)


PROCESSING MESSAGES (SERVER)


EXCEPTIONS
UNIT TESTING MESSAGE HANDLERS                                                     26




Available from NuGet using


Install-Package NServiceBus.Testing


Provides the ability to set expectations around how message handlers handle messages
• Expect: Send, Reply, Publish, etc...


Test.Initialize();
Test.Handler<MyHandler>()
     .ExpectPublish<MyMessage>(message => message.Prop1 == value1)
     .OnMessage<SomeEvent>(someEvent =>
     {
         someEvent.Prop1 = inputValue1;
     });
EXERCISE       27




UNIT TESTING
SAGA                 28




• Definition
• Saga declaration
• Saga ending
• Saga testing
• Exercise
SAGA - DEFINITION                                                                29




A Saga:

•   Is a pattern for implementing long-lived transaction by using a series of
    shorter transactions


•   Holds relevant state to needed to process mulitple messages in a ”saga entity”

•   Are initiated by a message (event/command)
SAGA - DECLARATION                                                                30




public class MyPolicy : Saga<MyPolicyData>,
         IAmStartedByMessages<MyMessage1>,
         IHandleMessages<MyMessage2>
{
     public void Handle(MyMessage1 order)
     public void Handle(MyMessage2 order)
}



•   Methods are like regular message handling logic
•   Sagas can be started by multiple messages (IAmStartedByMessages<T>)
•   First messages should start saga, following messages should be processed by
    the same one
SAGA – DECLARATION CONT.                               31




public class MyPolicyData : ISagaEntity
{
       public Guid Id { get; set; }
       public string Originator { get; set; }
       public string OriginalMessageId { get; set; }
}
ENDING A SAGA                                                                        32




MarkAsComplete();


•   Can call this from any method
•   Causes the saga to be deleted
•   Any data that you want retained should be sent on (or published) via a message
UNIT TESTING A SAGA                                          33




Test.Saga<MyPolicy>()
    .ExpectPublish<Message1>(/* check values */)
    .ExpectSend<Message2>(/* check values */)
    .ExpectReplyToOriginator<Message3>(/* check values */)
    .When(saga => saga.Handle(myMessage));



/* check values */
message => return(message.Data == someValue);
EXERCISE - SAGAS ROCK   34
EXTRA EXERCISES               35




TIMEOUT
CUSTOM XML NAMESPACE
CONFIGURABLE ROUTING
DEPENDENCY INJECTION
WEB-APP HOSTING
FULL DUPLEX
DISTRIBUTION GROUP EXERCISE

More Related Content

What's hot

Secure Messages with IBM WebSphere MQ Advanced Message Security
Secure Messages with IBM WebSphere MQ Advanced Message SecuritySecure Messages with IBM WebSphere MQ Advanced Message Security
Secure Messages with IBM WebSphere MQ Advanced Message SecurityMorag Hughson
 
IBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ ClustersIBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ ClustersDavid Ware
 
IBM MQ: Using Publish/Subscribe in an MQ Network
IBM MQ: Using Publish/Subscribe in an MQ NetworkIBM MQ: Using Publish/Subscribe in an MQ Network
IBM MQ: Using Publish/Subscribe in an MQ NetworkDavid Ware
 
IBM MQ v8 and JMS 2.0
IBM MQ v8 and JMS 2.0IBM MQ v8 and JMS 2.0
IBM MQ v8 and JMS 2.0Matthew White
 
Scalable Persistent Message Brokering with WSO2 Message Broker
Scalable Persistent Message Brokering with WSO2 Message BrokerScalable Persistent Message Brokering with WSO2 Message Broker
Scalable Persistent Message Brokering with WSO2 Message BrokerSrinath Perera
 
Apache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache CamelApache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache CamelOmi Om
 
Making communications across boundaries simple with NServiceBus
Making communications across boundaries simple with NServiceBusMaking communications across boundaries simple with NServiceBus
Making communications across boundaries simple with NServiceBusParticular Software
 
WebSphere MQ CHLAUTH - including V8 changes
WebSphere MQ CHLAUTH - including V8 changesWebSphere MQ CHLAUTH - including V8 changes
WebSphere MQ CHLAUTH - including V8 changesMorag Hughson
 
3429 How to transform your messaging environment to a secure messaging envi...
3429   How to transform your messaging environment to a secure messaging envi...3429   How to transform your messaging environment to a secure messaging envi...
3429 How to transform your messaging environment to a secure messaging envi...Robert Parker
 
MQ Security Overview
MQ Security OverviewMQ Security Overview
MQ Security OverviewMarkTaylorIBM
 
IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...
IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...
IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...T.Rob Wyatt
 
DataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep DiveDataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep DiveMorag Hughson
 
IBM WebSphere MQ for z/OS - Security
IBM WebSphere MQ for z/OS - SecurityIBM WebSphere MQ for z/OS - Security
IBM WebSphere MQ for z/OS - SecurityDamon Cross
 
Jms deep dive [con4864]
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]Ryan Cuprak
 
IBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM MQ: Managing Workloads, Scaling and Availability with MQ ClustersIBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM MQ: Managing Workloads, Scaling and Availability with MQ ClustersDavid Ware
 
IBM MQ - better application performance
IBM MQ - better application performanceIBM MQ - better application performance
IBM MQ - better application performanceMarkTaylorIBM
 
What's New in IBM MQ - Version 8
What's New in IBM MQ - Version 8What's New in IBM MQ - Version 8
What's New in IBM MQ - Version 8MarkTaylorIBM
 
Secure Your Messages with IBM MQ Advanced Message Security
Secure Your Messages with IBM MQ Advanced Message SecuritySecure Your Messages with IBM MQ Advanced Message Security
Secure Your Messages with IBM MQ Advanced Message SecurityMorag Hughson
 
IBM MQ V8 Security: Latest Features Deep-Dive
IBM MQ V8 Security: Latest Features Deep-DiveIBM MQ V8 Security: Latest Features Deep-Dive
IBM MQ V8 Security: Latest Features Deep-DiveMorag Hughson
 

What's hot (20)

Secure Messages with IBM WebSphere MQ Advanced Message Security
Secure Messages with IBM WebSphere MQ Advanced Message SecuritySecure Messages with IBM WebSphere MQ Advanced Message Security
Secure Messages with IBM WebSphere MQ Advanced Message Security
 
IBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ ClustersIBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM WebSphere MQ: Managing Workloads, Scaling and Availability with MQ Clusters
 
IBM MQ: Using Publish/Subscribe in an MQ Network
IBM MQ: Using Publish/Subscribe in an MQ NetworkIBM MQ: Using Publish/Subscribe in an MQ Network
IBM MQ: Using Publish/Subscribe in an MQ Network
 
IBM MQ v8 and JMS 2.0
IBM MQ v8 and JMS 2.0IBM MQ v8 and JMS 2.0
IBM MQ v8 and JMS 2.0
 
Scalable Persistent Message Brokering with WSO2 Message Broker
Scalable Persistent Message Brokering with WSO2 Message BrokerScalable Persistent Message Brokering with WSO2 Message Broker
Scalable Persistent Message Brokering with WSO2 Message Broker
 
Apache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache CamelApache ActiveMQ and Apache Camel
Apache ActiveMQ and Apache Camel
 
IBM MQ V8 annd JMS 2.0
IBM MQ V8 annd JMS 2.0IBM MQ V8 annd JMS 2.0
IBM MQ V8 annd JMS 2.0
 
Making communications across boundaries simple with NServiceBus
Making communications across boundaries simple with NServiceBusMaking communications across boundaries simple with NServiceBus
Making communications across boundaries simple with NServiceBus
 
WebSphere MQ CHLAUTH - including V8 changes
WebSphere MQ CHLAUTH - including V8 changesWebSphere MQ CHLAUTH - including V8 changes
WebSphere MQ CHLAUTH - including V8 changes
 
3429 How to transform your messaging environment to a secure messaging envi...
3429   How to transform your messaging environment to a secure messaging envi...3429   How to transform your messaging environment to a secure messaging envi...
3429 How to transform your messaging environment to a secure messaging envi...
 
MQ Security Overview
MQ Security OverviewMQ Security Overview
MQ Security Overview
 
IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...
IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...
IBM MQ CONNAUTH/CHLAUTH Doesn't Work Like You Think it Does (and if you aren'...
 
DataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep DiveDataPower-MQ Integration Deep Dive
DataPower-MQ Integration Deep Dive
 
IBM WebSphere MQ for z/OS - Security
IBM WebSphere MQ for z/OS - SecurityIBM WebSphere MQ for z/OS - Security
IBM WebSphere MQ for z/OS - Security
 
Jms deep dive [con4864]
Jms deep dive [con4864]Jms deep dive [con4864]
Jms deep dive [con4864]
 
IBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM MQ: Managing Workloads, Scaling and Availability with MQ ClustersIBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
IBM MQ: Managing Workloads, Scaling and Availability with MQ Clusters
 
IBM MQ - better application performance
IBM MQ - better application performanceIBM MQ - better application performance
IBM MQ - better application performance
 
What's New in IBM MQ - Version 8
What's New in IBM MQ - Version 8What's New in IBM MQ - Version 8
What's New in IBM MQ - Version 8
 
Secure Your Messages with IBM MQ Advanced Message Security
Secure Your Messages with IBM MQ Advanced Message SecuritySecure Your Messages with IBM MQ Advanced Message Security
Secure Your Messages with IBM MQ Advanced Message Security
 
IBM MQ V8 Security: Latest Features Deep-Dive
IBM MQ V8 Security: Latest Features Deep-DiveIBM MQ V8 Security: Latest Features Deep-Dive
IBM MQ V8 Security: Latest Features Deep-Dive
 

Similar to NServiceBus workshop presentation

Introduction to NServiceBus
Introduction to NServiceBusIntroduction to NServiceBus
Introduction to NServiceBusEspen Ekvang
 
WSO2 Message Broker - Product Overview
WSO2 Message Broker - Product OverviewWSO2 Message Broker - Product Overview
WSO2 Message Broker - Product OverviewWSO2
 
Making communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service BusMaking communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service BusParticular Software
 
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...Peter Broadhurst
 
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0WSO2
 
Docker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsDocker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsFederico Michele Facca
 
Rabbitmq an amqp message broker
Rabbitmq an amqp message brokerRabbitmq an amqp message broker
Rabbitmq an amqp message brokerANASYS
 
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE PlatformsFIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE PlatformsFIWARE
 
The Overview of Microservices Architecture
The Overview of Microservices ArchitectureThe Overview of Microservices Architecture
The Overview of Microservices ArchitectureParia Heidari
 
Cloud Messaging Service: Technical Overview
Cloud Messaging Service: Technical OverviewCloud Messaging Service: Technical Overview
Cloud Messaging Service: Technical OverviewMessaging Meetup
 
Connecting IBM MessageSight to the Enterprise
Connecting IBM MessageSight to the EnterpriseConnecting IBM MessageSight to the Enterprise
Connecting IBM MessageSight to the EnterpriseAndrew Schofield
 
Cloud computing Module 2 First Part
Cloud computing Module 2 First PartCloud computing Module 2 First Part
Cloud computing Module 2 First PartSoumee Maschatak
 
AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...
AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...
AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...wangbo626
 
Connecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQConnecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQRob Davies
 
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
 HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen... HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...Matt Leming
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to MicroservicesMahmoudZidan41
 

Similar to NServiceBus workshop presentation (20)

Introduction to NServiceBus
Introduction to NServiceBusIntroduction to NServiceBus
Introduction to NServiceBus
 
WSO2 Message Broker - Product Overview
WSO2 Message Broker - Product OverviewWSO2 Message Broker - Product Overview
WSO2 Message Broker - Product Overview
 
Making communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service BusMaking communication across boundaries simple with Azure Service Bus
Making communication across boundaries simple with Azure Service Bus
 
Mini-Training: Message Brokers
Mini-Training: Message BrokersMini-Training: Message Brokers
Mini-Training: Message Brokers
 
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
IBM IMPACT 2014 - AMC-1882 Building a Scalable & Continuously Available IBM M...
 
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
WSO2 Product Release webinar - WSO2 Message Broker 2.2.0
 
Docker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platformsDocker Swarm secrets for creating great FIWARE platforms
Docker Swarm secrets for creating great FIWARE platforms
 
Rabbitmq an amqp message broker
Rabbitmq an amqp message brokerRabbitmq an amqp message broker
Rabbitmq an amqp message broker
 
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE PlatformsFIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
FIWARE Tech Summit - Docker Swarm Secrets for Creating Great FIWARE Platforms
 
The Overview of Microservices Architecture
The Overview of Microservices ArchitectureThe Overview of Microservices Architecture
The Overview of Microservices Architecture
 
Cloud Messaging Service: Technical Overview
Cloud Messaging Service: Technical OverviewCloud Messaging Service: Technical Overview
Cloud Messaging Service: Technical Overview
 
Connecting IBM MessageSight to the Enterprise
Connecting IBM MessageSight to the EnterpriseConnecting IBM MessageSight to the Enterprise
Connecting IBM MessageSight to the Enterprise
 
Cloud computing Module 2 First Part
Cloud computing Module 2 First PartCloud computing Module 2 First Part
Cloud computing Module 2 First Part
 
AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...
AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...
AME-1934 : Enable Active-Active Messaging Technology to Extend Workload Balan...
 
Connecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQConnecting Applications Everywhere with ActiveMQ
Connecting Applications Everywhere with ActiveMQ
 
zeromq
zeromqzeromq
zeromq
 
Azure Messaging Services #1
Azure Messaging Services #1Azure Messaging Services #1
Azure Messaging Services #1
 
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
 HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen... HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
HHM-3540: The IBM MQ Light API: From Developer Laptop to Enterprise Data Cen...
 
Mq Lecture
Mq LectureMq Lecture
Mq Lecture
 
Introduction to Microservices
Introduction to MicroservicesIntroduction to Microservices
Introduction to Microservices
 

More from Tomas Jansson

Functional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suaveFunctional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suaveTomas Jansson
 
F# as our day job by 2016
F# as our day job by 2016F# as our day job by 2016
F# as our day job by 2016Tomas Jansson
 
What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5Tomas Jansson
 
OWIN Web API with Linky
OWIN Web API with LinkyOWIN Web API with Linky
OWIN Web API with LinkyTomas Jansson
 
File -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus DeployFile -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus DeployTomas Jansson
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETTomas Jansson
 
Deployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCityDeployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCityTomas Jansson
 
SignalR - Building an async web app with .NET
SignalR - Building an async web app with .NETSignalR - Building an async web app with .NET
SignalR - Building an async web app with .NETTomas Jansson
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web APIREST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web APITomas Jansson
 

More from Tomas Jansson (12)

Functional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suaveFunctional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suave
 
F# as our day job by 2016
F# as our day job by 2016F# as our day job by 2016
F# as our day job by 2016
 
What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5
 
Polyglot heaven
Polyglot heavenPolyglot heaven
Polyglot heaven
 
OWIN Web API with Linky
OWIN Web API with LinkyOWIN Web API with Linky
OWIN Web API with Linky
 
Roslyn
RoslynRoslyn
Roslyn
 
File -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus DeployFile -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NET
 
Deployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCityDeployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCity
 
State or intent
State or intentState or intent
State or intent
 
SignalR - Building an async web app with .NET
SignalR - Building an async web app with .NETSignalR - Building an async web app with .NET
SignalR - Building an async web app with .NET
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web APIREST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web API
 

NServiceBus workshop presentation

  • 1. NSERVICEBUS Inspired by: The course authored by Udi Dahan Oslo/Fagdag Espen Ekvang/Tomas Jansson 01/03/2013
  • 2. AGENDA 2 • Intro • Messaging and queues • Testing • SOA • Saga
  • 3. INTRO 4 • Fallacies of distributed computing • Why NServiceBus? • Bus vs. Broker • Service orientation • Excercises
  • 4. FALLACIES OF DISTRIBUTED COMPUTING 5 1. The network is reliable 2. Latency isn’t a problem 3. Bandwidth isn’t a problem 4. The network is secure 5. The topology won’t change 6. The administrator will know what to do 7. Transport cost isn’t a problem 8. The network is homogeneous Cant’ assume WHEN the message will arrive, IF AT ALL
  • 5. WHY NSERVICEBUS 6 1. The network is reliable 2. Latency isn’t a problem NServiceBus addresses the first five directly 3. Bandwidth isn’t a problem 4. The network is secure 5. The topology won’t change 6. The administrator will know what to do 7. Transport cost isn’t a problem 8. The network is homogeneous The most developer-friendly service bus for SOA on .NET
  • 6. BUS VS. BROKER 7 • Bus is not necessarily physically separate • Simpler; no routing or service fail over • No single point of failure App App Buss.dll Broker App App Buss.dll Buss.dll App App
  • 7. TENETS OF SERVICE ORIENTATION 8 • Services are autonomous • Share contract & schema, not class or type • Boundaries are explicit • Compatibitility is base on Policy
  • 8. LAYERS & COUPLING 9 Tight coupling Loose coupling Sales Shipping CRM UI BL DAL DB Referential Integrity Reintroduces coupling
  • 9. WHEN CAN I WRITE SOME CODE? 10 • Getting started • New class library • Install-Package NServiceBus.Host • Logging • NServiceBus uses log4net, you can configure logging in app.config • Default output is console
  • 10. EXERCISES 11 HELLO WORLD LOGGING
  • 11. MESSAGING AND QUEUES 12 • Store & forward • Dangers of store & forward • Request/Response • Messaging and NServiceBus • Exercises
  • 12. STORE & FORWARD 13 SERVER MSMQ OUTGOING INCOMING Store & Forward writes to disk Resilient in the face of failures MSMQ OUTGOING INCOMING CLIENT
  • 13. DANGERS OF STORE & FORWARD 14 • If target is offline for an extended period of timemessages can fill up the disk • Can cause a server to crash • Especially problematic in B2B integration • 1 MB/message, 100 message/sec = 6GB/minute • Solution – discard messages after a while • Use [TimeToBeReceived("00:01:00")] on the message definition
  • 14. REQUEST/RESPONSE 15 SERVER MSMQ OUTGOING INCOMING Client can’t assume when a response Equivalent to 2 will arrive, if at all one-way messages MSMQ OUTGOING INCOMING CLIENT
  • 15. REQUEST/RESPONSE 16 • Message is sent from the server to the client’s queue If the client is offline, message sits in the server machine’s outgoing queue • Client is not blocked until response arrives • If two requests were sent, responses may arrive out of order
  • 16. WARNING! THIS IS NOT RPC 17 • Do NOT try to implement regular request/response patterns on top of messaging • The client should be designed so that it can continue operating if a response never comes Differences from RPC • RPC is easy to code • After invoking a web service • Next line of code assumes we’ve got a response • RPC problems • Can’t reason about the time between one line of code and another • Messaging makes this all explicit
  • 17. MESSAGING AND NSERVICEBUS 18 SERVER MSMQ OUTGOING INCOMING Transaction MSMQ OUTGOING INCOMING CLIENT
  • 18. DEFINE A MESSAGE 19 • Preferably inherit from IEvent or ICommand • Use IMessage when replying using Bus.Reply() • Also possible to define your own convention • Configure.DefiningMessagesAs(t=>MyOwnConvention(t)) • Add properties like a regular class/interface • Keep contract definintions in their own assembly/project public class MyEvent: IEvent {}
  • 19. INSTANTIATE A MESSAGE 20 • var myMessage = new MyMessage(); • var myMessage = Bus.CreateInstance<MyMessage>();
  • 20. SEND A MESSAGE 21 Bus.Send(messageObject); Can instantiate and send together (useful for interfaces): Bus.Send<IMessage>((message) => { message.Prop1 = value1; message.Prop2 = value2; });
  • 21. SPECIFY DESTINATION 22 1. Bus.Send(destination, messages); Requires that application manages routing 2. Configure destination for message type. In <UnicastBusConfig>, under <MessageEndpointMappings> specify one of the following: - <add Messages="assembly" Endpoint="destination"/> - <add Messages="type" Endpoint="destination"/> 3. Specify destination using - QueueName@ServerName , or - Just QueueName for the same machine
  • 22. HANDLE A MESSAGE 23 Write a class that implements IHandleMessages<T> where T is a message type public class MyHandler : IHandleMessages<MyMessage> { public void Handle(MyMessage message) { } } Remember to specify in <UnicastBusConfig>, under <MessageEndpointMappings> one of the following: - <add Messages="assembly" Endpoint="source"/> - <add Messages="type" Endpoint="source"/>
  • 23. CONFIGURING AN ENDPOINT 24 When configuring an endpoint inherit from 1. Using AsA_Client will - use non-transactional MsmqTransport - purge its queue of messages on startup - processes messages using its own permissions, not those of the message sender 2. Using AsA_Server will - use transactional MsmqTransport - not purge its queue of messages on startup, hence fault-tolerant - processes messages using the permissions of the sender (impersonation) 3. Using AsA_Publisher will - extends AsA_Server - indicates to the infrastructure that a storage for subscription request is to be set up
  • 24. EXERCISES 25 ONE-WAY MESSAGING (CLIENT) PROCESSING MESSAGES (SERVER) EXCEPTIONS
  • 25. UNIT TESTING MESSAGE HANDLERS 26 Available from NuGet using Install-Package NServiceBus.Testing Provides the ability to set expectations around how message handlers handle messages • Expect: Send, Reply, Publish, etc... Test.Initialize(); Test.Handler<MyHandler>() .ExpectPublish<MyMessage>(message => message.Prop1 == value1) .OnMessage<SomeEvent>(someEvent => { someEvent.Prop1 = inputValue1; });
  • 26. EXERCISE 27 UNIT TESTING
  • 27. SAGA 28 • Definition • Saga declaration • Saga ending • Saga testing • Exercise
  • 28. SAGA - DEFINITION 29 A Saga: • Is a pattern for implementing long-lived transaction by using a series of shorter transactions • Holds relevant state to needed to process mulitple messages in a ”saga entity” • Are initiated by a message (event/command)
  • 29. SAGA - DECLARATION 30 public class MyPolicy : Saga<MyPolicyData>, IAmStartedByMessages<MyMessage1>, IHandleMessages<MyMessage2> { public void Handle(MyMessage1 order) public void Handle(MyMessage2 order) } • Methods are like regular message handling logic • Sagas can be started by multiple messages (IAmStartedByMessages<T>) • First messages should start saga, following messages should be processed by the same one
  • 30. SAGA – DECLARATION CONT. 31 public class MyPolicyData : ISagaEntity { public Guid Id { get; set; } public string Originator { get; set; } public string OriginalMessageId { get; set; } }
  • 31. ENDING A SAGA 32 MarkAsComplete(); • Can call this from any method • Causes the saga to be deleted • Any data that you want retained should be sent on (or published) via a message
  • 32. UNIT TESTING A SAGA 33 Test.Saga<MyPolicy>() .ExpectPublish<Message1>(/* check values */) .ExpectSend<Message2>(/* check values */) .ExpectReplyToOriginator<Message3>(/* check values */) .When(saga => saga.Handle(myMessage)); /* check values */ message => return(message.Data == someValue);
  • 33. EXERCISE - SAGAS ROCK 34
  • 34. EXTRA EXERCISES 35 TIMEOUT CUSTOM XML NAMESPACE CONFIGURABLE ROUTING DEPENDENCY INJECTION WEB-APP HOSTING FULL DUPLEX DISTRIBUTION GROUP EXERCISE

Editor's Notes

  1. The Fallacies of Distributed Computing are a set of assumptions that Peter Deutsch and others at Sun Microsystems originally asserted programmers new to distributed applications invariably make. These assumptions ultimately prove false, resulting either in the failure of the system, a substantial reduction in system scope, or in large, unplanned expenses required to redesign the system to meet its original goals.Self explanatoryLocal vs. globalLocal vs. globalThis is just false.Your network topology might change for some reasonMore like trial and errorCost in resources to serialize/deserialize data and cost in network trafficUse standard technologies. Maybe not relevant for Nservicebus since you must run on windows.
  2. http://support.nservicebus.com/customer/portal/articles/861018-overview
  3. Services are autonomous – changes on one services should not affect another service through encapsulationContract &amp; schema – don’t share implementation as contract, framework is fine to shareBoundaries – to communicate over boundaries should be an explicit actCompatibility – security, signing etc. is not defined in main code but as policies
  4. But do you really need referential integrity? What if you didn’t delete data, and modeled the business lifecycle of data instead?Products no longer for sale, though previous orders can continue processing, inventory liquidated, etc.
  5. Consider overriding ConfigureHowToFindSaga to be able to get the correct saga for processing messages