Akka Hello World(Java 8)

将此依赖项添加到项目 POM:

<dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor_2.11</artifactId>
        <version>2.4.4</version>
</dependency>

创建一个 Actor

public class HelloWorldActor extends AbstractActor {

    public HelloActor() {
        receive(ReceiveBuilder
            .match(SayHello.class, this::sayHello)
            .match(SayBye.class, this::sayBye)
            .build());
    }

    private void sayHello(final SayHello message) {
        System.out.println("Hello World");
    }

    private void sayHello(final SayBye message) {
        System.out.println("Bye World");
    }

    public static Props props() {
        return Props.create(HelloWorldActor.class);
    }
}

为 actor 创建一个 Junit 测试

public class HelloActorTest {

    private ActorSystem actorSystem;

    @org.junit.Before
    public void setUp() throws Exception {
        actorSystem = ActorSystem.create();
    }

    @After
    public void tearDown() throws Exception {
        JavaTestKit.shutdownActorSystem(actorSystem);
    }

    @Test
    public void testSayHello() throws Exception {
        new JavaTestKit(actorSystem) {
            {
                ActorRef helloActorRef = actorSystem.actorOf(HelloWorldActor.props());
                helloActorRef.tell(new SayHello(), ActorRef.noSender());
                helloActorRef.tell(new SayBye(), ActorRef.noSender());
            }
        };
     }
 }