2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
2012-02-02 09:40:17 +01:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
package jdocs.remoting;
|
2012-02-02 09:40:17 +01:00
|
|
|
|
2013-05-02 17:12:36 +02:00
|
|
|
import akka.testkit.AkkaJUnitActorSystemResource;
|
2017-03-16 09:30:00 +01:00
|
|
|
import jdocs.AbstractJavaTest;
|
2013-05-02 17:12:36 +02:00
|
|
|
import org.junit.ClassRule;
|
2012-02-02 09:40:17 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2012-10-05 11:56:59 -07:00
|
|
|
import com.typesafe.config.ConfigFactory;
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
// #import
|
2012-02-02 09:40:17 +01:00
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.Address;
|
2012-02-27 10:28:20 +01:00
|
|
|
import akka.actor.AddressFromURIString;
|
2012-02-02 09:40:17 +01:00
|
|
|
import akka.actor.Deploy;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.remote.RemoteScope;
|
2019-01-12 04:00:53 +08:00
|
|
|
// #import
|
2012-02-02 09:40:17 +01:00
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
import akka.actor.AbstractActor;
|
2012-09-21 15:08:56 +02:00
|
|
|
|
2017-03-02 11:55:03 +01:00
|
|
|
public class RemoteDeploymentDocTest extends AbstractJavaTest {
|
2012-09-21 15:08:56 +02:00
|
|
|
|
2017-02-04 11:51:30 +05:00
|
|
|
public static class SampleActor extends AbstractActor {
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
2019-01-12 04:00:53 +08:00
|
|
|
return receiveBuilder().matchAny(message -> getSender().tell(getSelf(), getSelf())).build();
|
2012-09-21 15:08:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-05-02 17:12:36 +02:00
|
|
|
|
|
|
|
|
@ClassRule
|
|
|
|
|
public static AkkaJUnitActorSystemResource actorSystemResource =
|
2019-01-12 04:00:53 +08:00
|
|
|
new AkkaJUnitActorSystemResource("RemoteDeploymentDocTest");
|
2013-05-02 17:12:36 +02:00
|
|
|
|
|
|
|
|
private final ActorSystem system = actorSystemResource.getSystem();
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2016-09-30 18:20:47 +02:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
void makeAddress() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #make-address-artery
|
2016-09-30 18:20:47 +02:00
|
|
|
Address addr = new Address("akka", "sys", "host", 1234);
|
|
|
|
|
addr = AddressFromURIString.parse("akka://sys@host:1234"); // the same
|
2019-01-12 04:00:53 +08:00
|
|
|
// #make-address-artery
|
2016-09-30 18:20:47 +02:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2012-02-02 09:40:17 +01:00
|
|
|
@Test
|
|
|
|
|
public void demonstrateDeployment() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #make-address
|
2019-05-15 18:01:34 +02:00
|
|
|
Address addr = new Address("akka", "sys", "host", 1234);
|
|
|
|
|
addr = AddressFromURIString.parse("akka://sys@host:1234"); // the same
|
2019-01-12 04:00:53 +08:00
|
|
|
// #make-address
|
|
|
|
|
// #deploy
|
|
|
|
|
ActorRef ref =
|
|
|
|
|
system.actorOf(
|
|
|
|
|
Props.create(SampleActor.class).withDeploy(new Deploy(new RemoteScope(addr))));
|
|
|
|
|
// #deploy
|
2012-02-02 09:40:17 +01:00
|
|
|
assert ref.path().address().equals(addr);
|
|
|
|
|
}
|
2012-09-21 15:08:56 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void demonstrateSampleActor() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #sample-actor
|
2012-09-21 15:08:56 +02:00
|
|
|
|
2013-04-14 22:56:41 +02:00
|
|
|
ActorRef actor = system.actorOf(Props.create(SampleActor.class), "sampleActor");
|
2013-06-05 16:59:25 +02:00
|
|
|
actor.tell("Pretty slick", ActorRef.noSender());
|
2019-01-12 04:00:53 +08:00
|
|
|
// #sample-actor
|
2012-09-21 15:08:56 +02:00
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
2012-10-05 11:56:59 -07:00
|
|
|
@Test
|
|
|
|
|
public void demonstrateProgrammaticConfig() {
|
2019-01-12 04:00:53 +08:00
|
|
|
// #programmatic
|
2019-05-01 08:12:09 +01:00
|
|
|
ConfigFactory.parseString("akka.remote.classic.netty.tcp.hostname=\"1.2.3.4\"")
|
2012-10-05 11:56:59 -07:00
|
|
|
.withFallback(ConfigFactory.load());
|
2019-01-12 04:00:53 +08:00
|
|
|
// #programmatic
|
|
|
|
|
|
|
|
|
|
// #programmatic-artery
|
2016-09-30 18:20:47 +02:00
|
|
|
ConfigFactory.parseString("akka.remote.artery.canonical.hostname=\"1.2.3.4\"")
|
|
|
|
|
.withFallback(ConfigFactory.load());
|
2019-01-12 04:00:53 +08:00
|
|
|
// #programmatic-artery
|
2012-10-05 11:56:59 -07:00
|
|
|
}
|
2013-01-09 01:47:48 +01:00
|
|
|
}
|