EasyNetQ use (nine) [non-generic publish & subscribe extension method, an error occurs]

Since the EasyNetQfirst 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 IBusthe Publishand Subscribemethods, 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 EasyNetQhow to deal with them.

Subscription services hung up

When you write a windows service, subscribe to a NewCustomerMessagemessage.
What happens if the service failure? For efficiency, EasyNetQfor 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, EasyNetQit will be sent 'Ack'to RabbitMQ. Not received 'Ack'before, this message is not from RabbitMQbeing deleted message queue. If you hang up the service, the news while processing this message (all messages in EasyNetQmemory queue) will stay in the RabbitMQqueue. 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

EasyNetQTake advantage of the RabbitMQcharacteristics of the service, set the prefetch-countvalue 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 Ackthe accumulated number of messages to this value, RabbitMQstops sending messages, these messages only retained EasyNetQin the memory queue. Of course, this ultimately will eat your queue RabbitMQall 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 EasyNetQis connected RabbitMQto the, EasyNetQto achieve a delay connection policy. This strategy assumes that the agent will not always available. When you first use by RabbitHutch.CreateBusconnecting to the proxy, EasyNetQopen 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.Subscribemethod to subscribe, even when the agent is not available. This subscription details will be EasyNetQcached. 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 EasyNetQthe 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 RabbitMQtime 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, EasyNetQyou 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 EasyNetQthe 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.Hosepipetool to re-issue an error message. Refer to the articleEasyNetQ.Hosepipe

Guess you like

Origin www.cnblogs.com/lhxsoft/p/11881866.html