Easynetq Use (ix) "Non-generic publish & Subscribe extension methods, error occurred"

Source: Internet
Author: User
Tags ack message queue rabbitmq

Since the first version of EASYNETQ, it has been able to publish/subscribe to a specific type of message.

Bus. Subscribe<mymessage> ("SubscriptionId",
        x => Console.WriteLine (X.text));

Bus. Publish<mymessage> (themessage);

However, during the run, how do you find the message type. For example: You may have some systems that load external plug-ins and want to be able to subscribe to their own message types. EASYNETQ provides Non-generic publishing and subscription methods for this purpose.

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)

Also includes publishing extension methods:

The public static void Publish (this
    IBus bus, 
    Type messagetype, Object message) is public

 static void Publish ( This
    is IBus bus, 
    Type MessageType, 
    object message, 
    string topic) The public

 static Task Publishasync (this
    IBus bus, 
    Type MessageType, 
    object message)

 the public static Task Publishasync (this
    IBus bus, 
    type MessageType , 
    object message, 
    string topic)

They are like the publish and subscribe methods on IBUs, in addition to providing a type parameter as an alternative to generic parameters, the message processor replaces the action with an 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 of using a non-generic publish.

var messagetype = typeof (Mymessage);
Bus. Publish (MessageType, themessage);

In this article let's look at the various errors that may occur in the message system and see how EASYNETQ handles them. subscription service hung up.

When you write a Windows service, use it to subscribe to a newcustomermessage message.
What happens if this service fails. For efficiency, EASYNETQ implements a memory-based, internal-use queue for subscription functionality. When a message is received over the network from the RABBITMQ, the message is placed in the memory queue. After a subscriber thread takes a message from this memory queue, it recalls the message processing code it provided. Once this callback is complete, EASYNETQ sends an ' ACK ' to RABBITMQ. This message is not removed from the RABBITMQ message queue until the ' Ack ' is received. If your service hangs up while processing the message, the message (all messages are in the EASYNETQ memory queue) will remain in the RABBITMQ queue. These messages will be sent again until your service is connected again. my subscribers consume messages at a slower rate than message publishing

EASYNETQ leverages the features of the RABBITMQ service, setting the Prefetch-count value to a more available value (currently 50). This means that there will not be more than 50 messages in the consumer's memory queue. This prevents memory overruns in your application that is being subscribed to. Once the number of RABBITMQ messages accumulates to this value, the message is stopped from being sent, leaving only the messages in the EASYNETQ memory queue. Of course, eventually this queue will eat up all the disk space on your RABBITMQ server. You should be monitoring this place to make sure you get an alert before this happens. A network failure occurred between my subscriber and the RABBITMQ agent

As described in the previous article with easynetq connection Rabbitmq , EASYNETQ implements a deferred connection strategy. This policy assumes that this agent is not always available. When you first connect to this agent by using Rabbithutch.createbus, EASYNETQ opens a connection and loops through the connection agent, and if you specify a proxy for the connection string address that is not available, you will see ' trying ' from the message log To Connect '. Subscribers can use bus. Subscribe method to subscribe, even when the agent is not available. The details of this subscription will be cached by EASYNETQ. When the agent recovery is available, the ongoing cyclic attempt to connect will successfully connect to the agent, and all cached subscriptions are restored together.
Similarly, when EASYNETQ and proxy connections are interrupted, it is returned to the circular connection, where you see ' trying to connect ' information. Once the connection is established, the cached subscriber is created again. The final conclusion is that in a running environment, you can keep your subscribers either when the network is disconnected or when you need to be rejected by your RABBITMQ agent. the subscription callback throws an exception when a message is consumed

If your subscription callback throws an exception, EASYNETQ will get the message that is being consumed, wrapping the message into a specified error message. This error message will be published to the EASYNETQ error queue (the name is Easynetq_default_error_queue). You should monitor the message in the error queue. This error message includes all the necessary information, such as the original message that needs to be republished, and the type of exception, exception information, and stack information. You may use the Easynetq.hosepipe tool to republish the error message. Refer to the next article Easynetq.hosepipe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.