In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the writing method of Ray-Handler message subscriber". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the method of writing Ray-Handler message subscribers?"
Message subscriber:
Ray is an ES/Actor framework designed based on Event Sourcing. Subscribers need to be processed after a message is published. There are two main types of subscribers:
CoreHandler message subscriber = RabbitSub+SubHandler
ToReadHandler message subscriber = RabbitSub+SQLToReadHandler (subclass of ToReadHandler)
RabbitSub characteristics
The RabbitSub feature is the RabbitMQ message queuing subscriber.
The RabbitSub feature has two constructors, which are commonly used:
Public RabbitSubAttribute (string group, string exchange, string queue, int queueCount = 1)
Group: usually used for classification. In the example, the group for X-CoreHandler is Core,X-ToReadHandler and Read.
The exchange name in exchange:RabbitMQ.
The queue name in queue:RabbitMQ.
QueueCount: number of message queues. Load balancing for messages.
Example:
[RabbitSub ("Core", "Account", "account")] public sealed class AccountCoreHandler: SubHandler {. }
RabbitSub can be used separately to subscribe to messages.
CoreHandler message subscriber
ESActor in Ray publishes events and delivers messages through the RaiseEvent method. Ray uses RabbitMQ to deliver messages by default. After ESActor initiates an event, CoreHandler subscribes to the event to handle the event.
The implementation is as follows:
Inherit SubHandler.
Add the RabbitSub property. The exchange name and queue name are consistent with the identification of the RabbitPub feature on ESGrain.
Add a constructor (required).
Public AccountCoreHandler (IServiceProvider svProvider): base (svProvider) {}
After the event is subscribed, it flows to the Tell method, and data is the event to be handled.
Public override Task Tell (byte [] bytes, IActorOwnMessage data, MessageInfo msg) {switch (data) {case AmountTransferEvent value: return Task.WhenAll (task, AmountAddEventHandler (value)); default: return task;}} ToReadHandler message subscriber
SQLToReadHandler
After ESActor initiates an event, X-ToReadHandler subscribes to the event to handle the event. X-ToReadHandler inherits from XmurSQLToReadHandler. XMurSQLToReadHandler inherits from ToReadHandler.
X-SQLToReadHandler requires users to inherit PartSubHandler and implement it themselves according to the relational database used. Ray provides PSQLToReadHandler for PostgreSQL by default. If you are using other relational databases such as MySQL, SQL Server, and so on, customize the implementation.
X-SQLToReadHandler implementation details:
Modify the Integrity Constraint Violation (violation of integrity constraints) exception of the corresponding relational database.
You can use PSQLToReadHandler in the instance as a X-ToReadHandler template and modify if (! (t.Exception.InnerException is Npgsql.PostgresException e & & e.SqlState = = "23505")).
Description:
When X-ToReadHandler subscribes to a message, the message will be replayed. If the message has been processed, the processed result already exists in the database. It is possible that an Integrity Constraint Violation (violation of integrity constraint) exception will be reported. By default, no handling will be done, and other exceptions will be thrown. This is the purpose of this code.
Sample template:
Public abstract class PSQLToReadHandler: PartSubHandler {public PSQLToReadHandler (IServiceProvider svProvider): base (svProvider) {} public override Task Notice (byte [] data) {return base.Notice (data) .ContinueWith (t = > {if (t.Exception! = null) {/ / based on using the database Modify this if to judge if (! (t.Exception.InnerException is Npgsql.PostgresException e & & e.SqlState = = "23505")) {throw t.Exception );}}
2. X-ToReadHandler
X-ToReadHandler subscribers are mainly used to subscribe to messages of interest and write data to the database.
The implementation is as follows:
Implement SQLToReadHandler
ToReadHandler inherits SQLToReadHandler (a subclass of ToReadHandler)
Add the RabbitSub property.
Add a constructor (required) to register the event of interest in the constructor.
Public AccountToReadHandler (IServiceProvider svProvider): base (svProvider) {Register (); Register ();}
The code is as follows:
[RabbitSub ("Read", "Account", "account")] public sealed class AccountToReadHandler: PSQLToReadHandler {public AccountToReadHandler (IServiceProvider svProvider): base (svProvider) {Register (); Register ();}} X-ToReadHandler message subscriber and CoreHandler message subscriber difference
When using the X-ToReadHandler message subscriber, you need to register the concerned events in the constructor, but not in X-CoreHandler, because the events need to be deserialized during processing. X-CoreHandler will deserialize all messages subscribed to by the RabbitSub parameter. X-ToReadHandler has further control on this, and only handles Register events in the subscribed messages. The reason for doing this: 1. Deserialization will consume some performance, and further control will help to improve performance. 2.Ray provides two implementation methods to provide reference for developers to extend custom source code.
At this point, I believe you have a deeper understanding of "what is the writing method of Ray-Handler message subscriber". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.