Howto subscribe to a message? How does auto/manual subscribe work?
I have no trouble with the concept of publish/subscribe but I cannot get my head around the (auto)configuration.
Scenario
- I have a front-end service (F1) which does a SEND of a command message (M1).
- This command message is received by a back-end (B1). B1 does some processing and then does a PUBLISH开发者_JAVA技巧 of notification message M2.
- Two services (F1 and F2) should receive a this notification message and do their processing/tasks.
[F1] =(M1)=> [B1] =(M2)=> [F1 & F2]
How do F1 and F2 subscribe to notification message M2 which is published/broadcasted by B1?
I cannot find actual configuration in either .config files or code in the samples and I cannot find documentation about this on the NServiceBus webcast.
There is IBus.Subscribe<T>
but I cannot see how to subscribe a certain instance. I would expect that I need to supply a queue to which to send the subscribe message to so that I return can receive publishes/announcements.
In short
In short B1 does a PUBLISH of M2.
- How can F1 and F2 subscribe to this this message?
- How does it work when auto subscribe is not used? So from code or config?
When you do not auto-subscribe, you have to explicitly subscribe yourself in code. Start by specifying the IWantCustomInitialization interface on your EndpointConfig class. Then you tell NSB to not auto-subscribe:
NServiceBus.Configure.With()
.DefaultBuilder()
.XmlSerializer()
.MsmqTransport()
.IsTransactional(true)
.UnicastBus()
.DoNotAutoSubscribe()
.LoadMessageHandlers();
Then within your subscriber endpoint, implement the IWantToRunAtStartup interface. There you can subscribe to specific messages, for example:
#region IWantToRunAtStartup Members
public void Run()
{
this.Bus.Subscribe<IProductUpdatedEvent>();
this.Bus.Subscribe<IProductCreatedEvent>();
}
public void Stop()
{
this.Bus.Unsubscribe<IProductUpdatedEvent>();
this.Bus.Unsubscribe<IProductCreatedEvent>();
}
#endregion
In the config files of F1 and F2, in the UnicastBusConfig
section, under the MessageEndpointMappings
the following entry:
<add Messages="{type of M2}" Endpoint="{queue of B1}" />
Then you also need to have a message handler in F1 and F2 for M2. When the bus sees this, it will automatically send the subscription message to the queue of B1, where the bus on the receiving side will store the names of the queues of F1 and F2 as interested in M2.
You probably don't need the .DoNotAutoSubscribe()
bit for your scenario.
精彩评论