Multiple subscribers in NServiceBus - nservicebus

I'm getting started with NServiceBus and have a question about the Pubsub sample.
My intention was to have multiple instances of Publisher1 running and receiving the message sent from the publisher. I also hacked the Publisher to only send messages of the eventMessage type.
But if I start the publisher and three instances of Subscriber1, only one of them gets the message at a time.
why is that? Is it a config setting or something else?
This is what I've tried which returns an exception "Exception when starting endpoint, error has been logged. Reason: Cannot configure property before the component has been configured. Please call 'Configure' first.":
using NServiceBus;
namespace Subscriber1
{
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{
}
public class OverrideInputQueue : IWantCustomInitialization
{
public void Init()
{
Configure
.Instance
.Configurer
.ConfigureProperty<NServiceBus.Config.MsmqTransportConfig>(t => t.InputQueue, "testQueue");
}
}
}
/J

NServiceBus assumes that you have one input queue per process. Make sure that each of your subscribers are configured with a unique input queue. If not all three will be polling the same queue producing the behavior you're describing.
To do this you would probably have to copy paste sub1 to 3 different folders, modfying the app.config and start them up.
Hope this helps!

You should use this-
Configure.Instance.Configurer.ConfigureProperty<NServiceBus.Unicast.Transport.Msmq.MsmqTransport>(msmq => msmq.InputQueue, "SomeQueueHere");
Make sure you use MsmqTransport and not MsmqTransportConfig as you mentioned.

Related

How to set custom headers on RabbitMQ message using Apache Camel?

I'm trying to add custom headers on my message, so whenever an exception occurs and it ends up in the dead-letter-queue, I can see what the exception was. However all my attempts at this have failed.
using .setHeader()
setting header on the outMessage
setting property of the exchange
Setting the exception as a property in the payload is not allowed.
#Component
public class ProcessRoute extends RouteBuilder {
...
#Override
public void configure() throws Exception {
onException(Exception.class)
.log("Error for ${body}! Requeue")
.redeliveryDelay(2000)
.maximumRedeliveries(3)
.handled(true)
.setHeader("TEST", constant("TEST"))
.process(e -> {
e.getOut().setHeader("TEST", "TEST");
e.setProperty("TEST","TEST");
});
from(SOME_ROUTE)
.doSomeStuff()
.to(RABBITMQ);
}
...
}
RABBITMQ-string:
rabbitmq://foo
?exchangeType=topic
&addresses=localhost:1234
&routingKey=#
&autoDelete=false
&queue=bar
&autoAck=false
&deadLetterExchange=DLX
&deadLetterQueue=bar.dlq
&deadLetterExchangeType=direct
&deadLetterRoutingKey=#
&username=foo
&password=bar
Resulting message on the dead-letter-queue:
If you use a header key following the pattern that the Camel RabbitMQ component has established, then your custom header will get picked up when the message is published to RabbitMQ.
Taking from your code above, instead of:
.setHeader("TEST", constant("TEST"))
Do this:
.setHeader("rabbitmq.TEST", constant("TEST"))
The Camel RabbitMQ component seems to ignore all the other non- "rabbitmq.*" headers that might be on the Camel exchange, and probably for good reason. There could be quite a few and most of them wouldn't make sense in the context of a message published to RabbitMQ.

NServiceBus 5.2.14 - Scheduler fails to trigger delegate

While using the NServiceBus Scheduler I have been unsuccessful in triggering the delegate defined. I used the documentation at the link below to setup the EndpointScheduler class.
The endpoints corresponding timeout queue is created and a message successfully enters the queue. No errors are encountered during execution, but the scheduler does not trigger the delegate. I am currently using NServiceBus 5.2.14, a similar test works using NServiceBus 3.2.7. Any ideas why the Scheduler isn't triggering the delegate?
http://docs.particular.net/nservicebus/scheduling/
public class EndpointScheduler : IWantToRunWhenBusStartsAndStops, ILoggable
{
public EndpointScheduler(Schedule schedule)
{
this.schedule = schedule;
}
public void Start()
{
schedule.Every(
TimeSpan.FromMinutes(1),
"Test",
() =>
{
Debug.WriteLine("I'm testing the scheduler");
}
);
}
public void Stop()
{
}
}
Thanks to #DavidBoike for pointing out a few potential setup issues.
The endpoint configuration contained:
configuration.DisableFeature<TimeoutManager>()
Removing it corrected the issue I encountered.
The reason for this is that the scheduler is dependent upon the TimeoutManager. It works by deferring a message to be processed later (using the TimeoutManager) and when that message is received, the delegate is invoked. Without the TimeoutManager activated, this can't work.

Automatic subscription not working

trying to start with nservicebus. My intent is to at least get the log message that the endpoint is trying to subscribe. So I created message handler for message like
namespace BusStop.Billing
{
public class OrderAcceptedHandler : IHandleMessages<OrderAccepted>
{
public void Handle(OrderAccepted message)
and the contract OrderAccepted is defined as
namespace BusStop.Sales.Contracts
{
public class OrderAccepted : IMessage
{
As per NServiceBus documentation, it should be sufficient to modify the subscriber .config file
<UnicastBusConfig>
<MessageEndpointMappings>
<add Messages="BusStop.Sales.Contracts" Endpoint="BusStop.Backend"/>
however when i start subscriber host NServiceBus.Host.exe I am not getting log message that it is trying to subscribe nor there is subscription request message in busstop.backend queue.
Thanks.
Ok, so the problem was you have to also mark your message with IEvent interface, because this is

MassTransit & Unity - How can I avoid duplicate messages from being sent to consumers?

We are using MassTransit/RabbitMQ and Unity. I've noticed that each consumer is receiving a single message twice. I have looked at other posts where other's are describing this issue as well. I'm clear as to what, specifically is the cause. However all fingers seem to be pointed to MT's SubScriptionBusConfigurator.LoadFrom(IUnityContainer container) method.
I am registering several consumers with my Unity container like so:
container.RegisterType<ConsumerA>();
container.RegisterType<ConsumerB>();
container.RegisterType<ConsumerC>();
My consumers are all listening for the same message:
public class ConsumerA : Consumes<SomeMessage>.All
{
public void Consume(SomeMessage message)
{
...
}
}
My service bus registration looks like:
container.RegisterType<IServiceBus>(new ContainerControlledLifetimeManager(), new InjectionFactory(x => ServiceBusFactory.New(sbc =>
{
sbc.ReceiveFrom("rabbitmq://localhost/service_bus");
sbc.UseRabbitMq();
sbc.UseNLog();
sbc.Subscribe(s => s.LoadFrom(container));
sbc.Subscribe(s => s.Handler<IFault>((context, message) =>
_logger.Error(string.Format("ServiceBus Fault: {0}", message.FaultType))));
})));
After some looking around it seems that my subscription is the problem:
sbc.Subscribe(s => s.LoadFrom(container));
So what is the correct/preferred way to register my consumers so that messages are not sent to each consumer twice??
Thanks!!!
The container specific LoadFrom makes a lot of assumptions. Some of them might be incorrect for you.
I'd suggest changing the calls to sbc.Subscribe(s => container.Resolve<ConsumerA>()), etc. LoadFrom is a convenience method and if it's no longer convenient for whatever reason, just skip it.

Is there a global exception handler for NServiceBus?

The current advice on handling exceptions in NServiceBus is to use the built in facilities. Errored out messages go to the error message queue, and a log is written to disk.
But what if I want to send my errors to a service like AirBrake which has better functionality for grouping similar exceptions, metrics, and other good stuff? Is there a global exception handler I can tap into?
As mentioned in the original post the recommended solution is to use ServicePulse for monitoring errors. The client I currently work for is using a custom made centralized logger, and we want NServiceBus to log to this log store when messages are forwarded to the error queue.
We could have achieved this by just editing the log4net config if the exception from NServiceBus had included the original exception, currently NServiceBus just logs a generic error message with no details about what caused the failure.
NServiceBus has a class named NServiceBus.Faults.ErrorsNotifications which contains the following observables:
MessageSentToErrorQueue
MessageHasFailedAFirstLevelRetryAttempt
MessageHasBeenSentToSecondLevelRetries
You can subscribe to these observables when the endpoint starts, like in the following example which logs an error when messages are sent to the error queue:
public class GlobalErrorHandler : IWantToRunWhenBusStartsAndStops
{
private readonly ILogger _logger;
private readonly BusNotifications _busNotifications;
readonly List<IDisposable> _notificationSubscriptions = new List<IDisposable>();
public GlobalErrorHandler(ILogger logger, BusNotifications busNotifications)
{
_logger = logger;
_busNotifications = busNotifications;
}
public void Start()
{
_notificationSubscriptions.Add(_busNotifications.Errors.MessageSentToErrorQueue.Subscribe(LogWhenMessageSentToErrorQueue));
}
public void Stop()
{
foreach (var subscription in _notificationSubscriptions)
{
subscription.Dispose();
}
}
private void LogWhenMessageSentToErrorQueue(FailedMessage message)
{
var properties = new
{
MessageType = message.Headers["NServiceBus.EnclosedMessageTypes"],
MessageId = message.Headers["NServiceBus.MessageId"],
OriginatingMachine = message.Headers["NServiceBus.OriginatingMachine"],
OriginatingEndpoint = message.Headers["NServiceBus.OriginatingEndpoint"],
ExceptionType = message.Headers["NServiceBus.ExceptionInfo.ExceptionType"],
ExceptionMessage = message.Headers["NServiceBus.ExceptionInfo.Message"],
ExceptionSource = message.Headers["NServiceBus.ExceptionInfo.Source"],
TimeSent = message.Headers["NServiceBus.TimeSent"]
};
_logger.Error("Message sent to error queue. " + properties, message.Exception);
}
}
The observable is implemented by using Reactive Extensions, so you will have to install the NuGet package Rx-Core for this to work.
Could you maybe use a custom log4net configuration to do it?
http://help.airbrake.io/discussions/suggestions/157-net-plug-in-for-api-v2
There is an interface in NServiceBus called IManageMessageFailures, but I don't believe you can use it in conjucntion with the Second Level Retries so you would probably lose that if you did go for your own.
You could write the errors to SQL Server with log4net and the from there you could forward them to AirBrake using their API marking each error in the error table as sent afterwards maybe?
We switched to Serilog, it has perfect logging ans tracing support for NServiceBus. Creating a custom Serilog sink that will send your log events wherever you want is quite easy.
I would suggest that you create a custom endpoint that feeds off the error queue and uploads the data to AirBrake.
Side note: v4.0|4.1 will come with a nice Rest-Api that gives you nice management/querying capabilities for the errors. Tools like the Profiler and Ops will use this api to present the data to you in various ways.
http://particular.net/service-platform