溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ray-Handler的CoreHandler編寫方法是什么

發(fā)布時間:2021-12-24 16:06:06 來源:億速云 閱讀:126 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“Ray-Handler的CoreHandler編寫方法是什么”,在日常操作中,相信很多人在Ray-Handler的CoreHandler編寫方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Ray-Handler的CoreHandler編寫方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

Ray中有兩類Handler(SubHandler和PartSubHandler),在使用中,SubHandler派生Actor的CoreHandler,PartSubHandler派生SQLToReadHandler,SQLToReadHandler派生Actor的ToReadHandler,使用Ray主要寫Actor的CoreHandler和ToReadHandler。

CoreHandler是復(fù)合消息路由器,包含的功能有:消息路由器、消息處理器、消息分離器、消息聚合器、消息過濾器、消息豐富器、副本同步協(xié)調(diào)器。

消息路由器:

Tell方法中的Switch塊,針對不同的Event類型,將Event事件中的數(shù)據(jù)分發(fā)給不同的事件處理方法CoreHandler承擔(dān)的是消息路由器的作用。如下代碼:

public override Task Tell(byte[] bytes, IActorOwnMessage<string> data, MessageInfo msg)
{
   switch (data)
   {
       case CoinAddressGeneratingResponse value: return AcceptCoinAddressAsync(value);
       case CoinWithdrawWithholdingFailedMsg value: return RollbackWithholdingAsync(value);
       case CoinWithdrawWithheldEvent value: return CreateWithdrawAsync(value);
       case CoinOrderCreatedEvent value: return CreateOrderAsync(value);
       case CoinOrderCreatedEventV1 value: return CreateOrderAsyncV1(value);
       case CoinPlanOrderCreatedEvent value: return CreatePlanOrderAsync(value);
       case CoinDepositIncreasedEvent value: return CoinDepositIncreased(value);
       case CoinIcoEvent value: return CoinIcoEventHandle(value);
       default: return Task.CompletedTask;
   }
}
消息處理器:

針對不同的Event類型,在該Actor的CoreHandler里處理該事件,CoreHandler承擔(dān)的是消息處理器的作用。如下代碼中,AmountAddEventHandler方法處理AmountTransferEvent事件。

在事件處理代碼中,可編寫的代碼如下:

  • 可以只針對當(dāng)前事件處理。

  • 可以獲得其他actor引用,調(diào)用其他actor的方法(包括actor的只讀方法和寫操作方法)。

  • 可以調(diào)用數(shù)據(jù)訪問層進行數(shù)據(jù)庫讀寫。(寫方法建議在ToReadHandler中進行)。

public override Task Tell(byte[] bytes, IActorOwnMessage<string> data, MessageInfo msg)
{
    switch (data)
    {
        case AmountTransferEvent value: return AmountAddEventHandler(value);
        default: return Task.CompletedTask;
    }
}

public Task AmountAddEventHandler(AmountTransferEvent value)
{
    var toActor = HandlerStart.Client.GetGrain<IAccount>(value.ToAccountId);
    return toActor.AddAmount(value.Amount, value.Id);
}
消息分離器:

當(dāng)需要將較大的消息分割成多個獨立的部分,并將這些獨立的部分作為其他actor處理的參數(shù)時,CoreHandler承擔(dān)的是分離器的作用。

消息聚合器:

當(dāng)需要對不同類型消息中的數(shù)據(jù)進行聚合統(tǒng)計時,CoreHandler承擔(dān)的是消息聚合器的作用。
例如:在加密貨幣交易的場景中,有ETH、BTC、USDT不同的市場,EOS在三個市場中都有交易,現(xiàn)在要統(tǒng)計本周內(nèi)每個用戶EOS的交易量,可以如下操作:

public override Task Tell(byte[] bytes, IActorOwnMessage<string> data, MessageInfo msg)
{
   switch (data)
   {
       case CoinTradeSoldEvent value: return ActivitySellStatistic(value);
       case CoinTradeBoughtEvent value: return ActivityBuyStatistic(value);
       default: return Task.CompletedTask;
   }
}
消息過濾器:

CoreHandler有可能收到它不感興趣額的消息,并且需要丟棄這些無用消息時,CoreHandler可以承擔(dān)消息過濾器的作用。

public override Task Tell(byte[] bytes, IActorOwnMessage<string> data, MessageInfo msg)
{
    switch (data)
    {
        case AmountTransferEvent value: return AmountAddEventHandler(value);
        default: return Task.CompletedTask;//消息過濾
    }
}
消息豐富器:

當(dāng)需要將收到的消息分進一步豐富,并將豐富后的消息作為其他actor處理的參數(shù)時,CoreHandler承擔(dān)的是消息豐富器的作用。

副本同步協(xié)調(diào)器:

Ray中有主actor和副本actor兩類actor,副本actor用于分擔(dān)主actor的壓力,執(zhí)行一些異步操作。當(dāng)使用副本actor,需要主actor與副本actor保持同步時,需要CoreHandler將關(guān)注的事件交給副本actor,此時CoreHandler承擔(dān)的是副本同步協(xié)調(diào)器的作用。

public override Task Tell(byte[] bytes, IActorOwnMessage<string> data, MessageInfo msg)
 {
     var replicatedRef = HandlerStart.Client.GetGrain<IAccountRep>(data.StateId);//獲得副本actor
     var task = replicatedRef.Tell(bytes);//通知副本同步
     switch (data)
     {
         case AmountTransferEvent value: return Task.WhenAll(task, AmountAddEventHandler(value));
         default: return task;
     }
 }

到此,關(guān)于“Ray-Handler的CoreHandler編寫方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI