Hibernating Rhinos | Rhino Service Bus – API Introduction

image

IServiceBus

  • CurrentMessageInformation - Stores message and information about the message for the current message being processed.
  • Endpoint - The endpoint for the bus being hosted.
  • AddInstanceSubscription - This method will keep a reference locally to the OcassionalConsumerOf<T> that is subscribing. The method returns IDisposable and when disposed will remove the subscription. The method sends a AddInstanceSubscription message to the confgured owner.
  • ConsumeMessages - This will directly invoke consumers on the local service bus without using any queuing or transport.
  • DelaySend - Waits to send the message until the specified time. This can be a great way to replace timers in your application because the service bus takes care of it for you.
  • HandleCurrentMessageLater - Puts the message back into the queue. This is essentially a loop where you can wait to process a message until a condition has been met.
  • Notify - Will send a message to each consumer / endpoint that has subscribed to the message argument.
  • Publish - Same as Notify. However, if no subscribers are found a MessagePublicationException will be thrown.
  • Reply - Will send a message back to the CurrentMessageInformation.Source endpoint.
  • Send - Sends a message to the message owner specified in the configuration, or to the Endpoint specified in the parameter arguments.
  • Subscribe<T> - Will send an AddSubscription message to the configured message owner for the message type being subscribed. This is typically handled for you automatically for any ConsumerOf<T>, InitiatedBy<T>, and Orchestrates<T>.
  • Unsubscribe<T> - Will send a  RemoveSubscription message to the configured owner.

IStartableServiceBus

  • Start - Used when the bus must receive messages (not using IOnewayBus), calling Start will begin listening for messages. When using the service bus host this is not needed.

Consumers

Consumers are similar to how you might think of an endpoint contract in WCF, but is really just a simple way to say I want to handle this message type.  One important detail is that it doesn't matter how the message reaches an endpoint for instance Send, Reply, Publish, or Notify. Below describes each kind of consumer in more detail.

 

image

  • ConsumerOf<TMsg> - Consumes messages for the specified type. The bus on startup will automatically send AddSubscription messages to the message owner.
  • OccasionalConsumerOf<TMsg> - Consumes messages for the specified type, but only when programmatically subscribed by calling bus.AddInstanceSubscription. It is your responsibility to remove the subscription as well. If a message is sent while not subscribed the message will be placed in the Discarded sub-queue.
  • InitiatedBy<TMsg> - Similar to ConsumerOf<TMsg>, but used to start a Saga.
  • Orchestrates<TMsg> - Similar to ConsumerOf<TMsg> but in order to consume the message the Saga must be started and IsComplete must be false. Otherwise the message will be placed in the Discarded sub-queue.
  • Consumer<T>.SkipAutomaticSubscription - Consumes messages via Send / Reply only and no subscription is required. Typically there is no harm in having a subscription, so using this is only necessary if you really want it.

Sample Send / Reply

This sample demonstrates messaging similar to Request / Response.

using Demo.Messages;
using Rhino.ServiceBus;

namespace Demo.Client
{
    public class PingInputScreen
    {
        private readonly IServiceBus bus;

        public PingInputScreen(IServiceBus bus)
        {
            this.bus = bus;
        }

        public void SendMessage()
        {
            bus.Send(new PingMessage());
        }
    }
}

using System;
using Demo.Messages;
using Rhino.ServiceBus;

namespace Demo.Server
{
    public class PingConsumer : ConsumerOf<PingMessage>
    {
        private readonly IServiceBus bus;

        public PingConsumer(IServiceBus bus)
        {
            this.bus = bus;
        }

        public void Consume(PingMessage message)
        {
            Console.WriteLine("Received ping message");
            bus.Reply(new PongMessage());
            Console.WriteLine("Sent reply pong message");
        }
    }
}

Sample Pub / Sub

The sample below demonstrates using the service bus to publish events to subscribed listeners. Note that you must publish messages from the service owner. Note also the difference between the command (something happening) and the event (something that has happened).

using Demo.Messages;
using Rhino.ServiceBus;

namespace Demo.Client
{
    public class CustomerAddressScreen
    {
        private readonly IServiceBus bus;

        public CustomerAddressScreen(IServiceBus bus)
        {
            this.bus = bus;
        }

        public void CustomerMoving(CustomerMovingModel customerMovingModel)
        {
            bus.Send(new CustomerMovingCommand
            {
                CustomerId = customerMovingModel.CustomerId,
                Line1 = customerMovingModel.Line1,
                Line2 = customerMovingModel.Line2,
                City = customerMovingModel.City,
                State = customerMovingModel.State,
                Zip = customerMovingModel.Zip,
            });
        }
    }
}

using Demo.Messages;
using Rhino.ServiceBus;

namespace Demo.Server
{
    public class CustomerMovingCommandConsumer : ConsumerOf<CustomerMovingCommand>
    {
        private readonly IServiceBus bus;
        private readonly IRepository repository;

        public CustomerMovingCommandConsumer(IServiceBus bus, IRepository repository)
        {
            this.bus = bus;
            this.repository = repository;
        }

        public void Consume(CustomerMovingCommand message)
        {
            var customer = repository.Get<Customer>(message.CustomerId);
            customer.Moved(message.Line1, message.Line2, message.City, message.State, message.Zip);
            bus.Publish(new CustomerMovedMessage{CustomerId = message.CustomerId});
        }
    }
}
Last update: 6/13/2010 4:49:30 PM