-
-
Save jhyoty/b4e81e14fbe26a5c43b121ad635452d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
akka { | |
loglevel = "DEBUG" | |
actor { | |
provider = remote | |
allow-java-serialization = true | |
warn-about-java-serializer-usage = false | |
} | |
remote { | |
artery { | |
canonical.hostname = "127.0.0.1" | |
advanced.remove-quarantined-association-after = 15 seconds | |
} | |
default-remote-dispatcher { | |
fork-join-executor { | |
parallelism-min = 1 | |
parallelism-max = 1 | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorRef; | |
import akka.actor.ActorSelection; | |
import akka.actor.ActorSystem; | |
import akka.actor.Props; | |
import akka.actor.UntypedAbstractActor; | |
import akka.event.Logging; | |
import akka.event.LoggingAdapter; | |
import akka.pattern.Patterns; | |
import akka.util.Timeout; | |
import com.typesafe.config.ConfigFactory; | |
import com.typesafe.config.ConfigValueFactory; | |
import scala.concurrent.Await; | |
import scala.concurrent.duration.Duration; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.TimeoutException; | |
public class Main { | |
public static void main(String[] args) throws TimeoutException, InterruptedException { | |
runServer(); | |
// fill threadlocal caches with actor references | |
for (int i = 0; i < 2; i++) { | |
runClient(); | |
Thread.sleep(1000); | |
} | |
// wait for unused association cleanup | |
Thread.sleep(30000); | |
if (!runClient()) { | |
//no response (expected) | |
System.exit(1); | |
} else { | |
System.exit(0); | |
} | |
} | |
public static boolean runClient() throws TimeoutException, InterruptedException { | |
final Timeout timeout = Timeout.apply(5000, TimeUnit.MILLISECONDS); | |
final ActorSystem client = ActorSystem.create("Client", ConfigFactory | |
.defaultApplication() | |
.withValue("akka.remote.artery.canonical.port", ConfigValueFactory.fromAnyRef(42001))); | |
final ActorSelection actor = client.actorSelection("akka://Server@127.0.0.1:42002/user/Test"); | |
try { | |
client.log().info("Result: {}", Await.result(Patterns.ask(actor, "Ping", timeout), Duration.Inf())); | |
return true; | |
} catch (TimeoutException t) { | |
client.log().error("No response from {}", actor); | |
return false; | |
} finally { | |
Await.ready(client.terminate(), Duration.Inf()); | |
} | |
} | |
public static void runServer() throws TimeoutException, InterruptedException { | |
final ActorSystem server = ActorSystem.create("Server", ConfigFactory | |
.load() | |
.withValue("akka.remote.artery.canonical.port", ConfigValueFactory.fromAnyRef(42002))); | |
final ActorRef ref = server.actorOf(Props.create(Test.class), "Test"); | |
Await.result(Patterns.ask(ref, "Ping", 5000), Duration.Inf()); | |
} | |
public static class Test extends UntypedAbstractActor { | |
private LoggingAdapter logger = Logging.getLogger(getContext().getSystem(), this); | |
@Override | |
public void onReceive(Object message) { | |
if ("Ping".equals(message)) { | |
logger.info("Sending Pong to {}", sender()); | |
sender().tell("Pong", self()); | |
} else { | |
unhandled(message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment