Reactive Messaging Patterns with Actor Model — 4 訊息丟失了怎麼辦

Reactive Messaging Patterns with Actor Model — 4 訊息丟失了怎麼辦_第1张图片
Lost-Mail.png

關於信息/信件這擋事,讓我很快地想起古代的飛鴿傳書,電視電影常常都會帶到關鍵時刻讓救火信鴿扮演傳神的奇兵,然而他要是被人抓了煮熟吃了該怎麼辦 ?

在訊息導向中間件(MOM)的設計當中,多半會特別去考慮到當信息有發出,而對方沒收到時該怎麼做? Akka 也對此有基本的處理方式因應,對於無法正確投遞的信件,會把它視為Dead Letter,系統開發者可以針對特定的信息類型設計他對應的處理機制,使得信息丟失時可被掌握。

package solid.humank.actor;

import akka.actor.AbstractActor;
import akka.actor.AbstractLoggingActor;
import akka.actor.Actor;
import akka.actor.DeadLetter;

/**
 * Created by kim on 2017/4/25.
 */
public class MyDeadLetterListener extends AbstractLoggingActor {

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(DeadLetter.class, deadLetter -> {
                    log().info("received something: {}", deadLetter.message().toString());
                })
                .build();
    }

}

在上面這個自訂的 Dead Letter 當中,我們可以透過這方式去監控死掉(已發送但收件者並沒有收件)的訊息,並自由決定你該對他做後續哪些處理。

 @Test
    public void dealWithDeadLetterByListener() {
        final ActorSystem system = ActorSystem.create("sample");

        final Props props = Props.create(ExplainActorLifeCycle.class);
        final Props deadProps = Props.create(MyDeadLetterListener.class);

        final TestActorRef ref = TestActorRef.create(system, props, "test");
        final ActorRef deadLetterListener = TestActorRef.create(system, deadProps, "deadLetter");

        system.eventStream().subscribe(deadLetterListener, DeadLetter.class);
        assertTrue(system.eventStream().hasSubscriptions(deadLetterListener));

        ref.tell(new String("Hello"), ref);
        ref.tell(new String("Stop"), ref);
        ref.tell(new String("Hello"), ref);
    }

完成letter操作設計之後,試圖來做些驗證,主要做法是把要通知的對象停停機,並試圖去看他的執行結果:

  1. 對系統註冊你的 DeadLetter Listener,並告知要處理的類型(若有客製化內容需要,可自行extends DeadLetter class)。
  2. akka framework 提供了方法讓你檢查你當前的系統是否有註冊特定的事件,拿來查驗還算可用的方法。
.... (略)
2017-05-15 15:13:06,134:INFO sample-akka.actor.default-dispatcher-2 (Slf4jLogger.scala:83) - received dead letter that receiever is not online : Hello
2017-05-15 15:13:06,135:INFO sample-akka.actor.default-dispatcher-2 (Slf4jLogger.scala:83) - Starting coordinated shutdown from JVM shutdown hook
2017-05-15 15:13:06,159 Thread-1 WARN Unable to register Log4j shutdown hook because JVM is shutting down. Using SimpleLogger
2017-05-15 15:13:06,170:INFO sample-akka.actor.default-dispatcher-2 (Slf4jLogger.scala:83) - Cluster Node [akka.tcp://[email protected]:4711] - Exiting completed
2017-05-15 15:13:06,172:INFO sample-akka.actor.default-dispatcher-4 (Slf4jLogger.scala:83) - Cluster Node [akka.tcp://[email protected]:4711] - Shutting down...
2017-05-15 15:13:06,174:INFO sample-akka.actor.default-dispatcher-4 (Slf4jLogger.scala:83) - Cluster Node [akka.tcp://[email protected]:4711] - Successfully shut down
2017-05-15 15:13:06,183:INFO sample-akka.actor.default-dispatcher-4 (Slf4jLogger.scala:83) - Shutting down remote daemon.
2017-05-15 15:13:06,184:INFO sample-akka.actor.default-dispatcher-4 (Slf4jLogger.scala:83) - Remote daemon shut down; proceeding with flushing remote transports.
2017-05-15 15:13:06,203:INFO sample-akka.actor.default-dispatcher-3 (Slf4jLogger.scala:83) - Remoting shut down
[DEBUG] [05/15/2017 15:13:06.204] [sample-akka.actor.default-dispatcher-4] [EventStream] shutting down: StandardOutLogger started
2017-05-15 15:13:06,204:INFO sample-akka.actor.default-dispatcher-3 (Slf4jLogger.scala:83) - Remoting shut down.
2017-05-15 15:13:06,205:DEBUG sample-akka.actor.default-dispatcher-3 (Slf4jLogger.scala:88) - shutting down: StandardOutLogger started
[DEBUG] [05/15/2017 15:13:06.206] [sample-akka.actor.default-dispatcher-4] [EventStream] all default loggers stopped

Process finished with exit code 0

received dead letter that receiever is not online : Hello

**延伸思考,對於這類的非預期的結果(這不是意味著等同於錯誤或例外),在Enterprise Integration Pattern當中多有介紹,接下來開始透過akka來實踐這幾種實作模式。 **

你可能感兴趣的:(Reactive Messaging Patterns with Actor Model — 4 訊息丟失了怎麼辦)