Since the EasyNetQ
first release, it can publish / subscribe to a particular type of message.
bus.Subscribe<MyMessage>("subscriptionId",
x => Console.WriteLine(x.Text)); bus.Publish<MyMessage>(theMessage);
However, during the operation, how to find the message you type? For example: You may have some system to load external plugins, hoping to subscribe to their own message types. EasyNetQ To this provides a non-generic publish and subscribe methods.
Just add this using statement:
using EasyNetQ.NonGeneric;
It will provide you with some subscription extension methods.
public static IDisposable Subscribe(
this IBus bus,
Type messageType,
string subscriptionId, Action<object> onMessage) public static IDisposable Subscribe( this IBus bus, Type messageType, string subscriptionId, Action<object> onMessage, Action<ISubscriptionConfiguration> configure) public static IDisposable SubscribeAsync( this IBus bus, Type messageType, string subscriptionId, Func<object, Task> onMessage) public static IDisposable SubscribeAsync( this IBus bus, Type messageType, string subscriptionId, Func<object, Task> onMessage, Action<ISubscriptionConfiguration> configure)
Release also includes an extension method:
public static void Publish(
this IBus bus, Type messageType, object message) public static void Publish( this IBus bus, Type messageType, object message, string topic) public static Task PublishAsync( this IBus bus, Type messageType, object message) public static Task PublishAsync( this IBus bus, Type messageType, object message, string topic)
They are like IBus
the Publish
and Subscribe
methods, except a Type parameter as an alternative generic parameters, the message processor replaced with Action Action.
var messageType = typeof(MyMessage);
bus.Subscribe(messageType, "my_subscriptionId", x =>
{
var message = (MyMessage)x; Console.Out.WriteLine("Got Message: {0}", x.Text); });
Here's an example, using a non-generic Publish.
var messageType = typeof(MyMessage);
bus.Publish(messageType, theMessage);
This article let us look at the various errors may occur in the message system, look at EasyNetQ
how to deal with them.
Subscription services hung up
When you write a windows
service, subscribe to a NewCustomerMessage
message.
What happens if the service failure? For efficiency, EasyNetQ
for the subscription feature is implemented based on the use of internal memory queue. After receiving a message from the network to the RabbitMQ, put this message into the queue memory. Subscribers take a thread after a message from the memory queue, the callback message handling code to provide their own. Once this correction is completed, EasyNetQ
it will be sent 'Ack'
to RabbitMQ
. Not received 'Ack'
before, this message is not from RabbitMQ
being deleted message queue. If you hang up the service, the news while processing this message (all messages in EasyNetQ
memory queue) will stay in the RabbitMQ
queue. After the service until you connect again, these messages will be sent again.
My speed subscribers consuming messages slower than the speed of news release
EasyNetQ
Take advantage of the RabbitMQ
characteristics of the service, set the prefetch-count
value of a more usable value (currently 50). This means that most will not be more than 50 messages in the consumer's memory queue. This will prevent you being subscribed application memory beyond exception. Once not Ack
the accumulated number of messages to this value, RabbitMQ
stops sending messages, these messages only retained EasyNetQ
in the memory queue. Of course, this ultimately will eat your queue RabbitMQ
all the disk space on the server. What you should do in this place under surveillance, to ensure that this happens before you can get an alert occurs.
Network failure between my subscribers and RabbitMQ agent
As with the previous article EasyNetQ
is connected RabbitMQ
to the, EasyNetQ
to achieve a delay connection policy. This strategy assumes that the agent will not always available. When you first use by RabbitHutch.CreateBus
connecting to the proxy, EasyNetQ
open a connection, try to connect to a proxy continuous cycle, if you specify the address of the proxy connection string is not in a usable state, you will see a message from the message log 'Trying to Connect'
. Subscribers can use the bus.Subscribe
method to subscribe, even when the agent is not available. This subscription details will be EasyNetQ
cached. When an agent becomes available, this cycle is constantly trying to connect will successfully connect to the proxy, and caching all subscriptions recovery together.
Similarly, when EasyNetQ
the time and the proxy connection is interrupted, it will return to the loop connection, you seen in the log 'Trying to Connect'
information. Once the connection is re-established, cached subscribers will be established again. Finally conclusion is that, in the running environment, no matter when disconnected from the network, or if you need to be your RabbitMQ
time Deny, you can keep your subscribers.
When a consumer news, subscribe to the callback throws an exception
If you subscribe to the callback throws an exception, EasyNetQ
you will get the message that is being consumed, the packaging of this message to a specific error message. This error message will be posted to EasyNetQ
the error queue (name EasyNetQ_Default_Error_Queue
). You should monitor this error message in the queue. This error message includes all the necessary information, such as the need to re-release the original message, as well as the type of exception, the exception information and stack information. You may use the EasyNetQ.Hosepipe
tool to re-issue an error message. Refer to the articleEasyNetQ.Hosepipe