Skip to content

Instantly share code, notes, and snippets.

@hardbyte
Last active October 26, 2017 03:15
Show Gist options
  • Save hardbyte/f5cc5c85fcb8e6893ff7afbea9756c51 to your computer and use it in GitHub Desktop.
Save hardbyte/f5cc5c85fcb8e6893ff7afbea9756c51 to your computer and use it in GitHub Desktop.
Statefull Akka Prime Example
package com.lightbend.akka.sample;
import akka.actor.*;
public class StatefulPrime {
static class IsPrimeActor extends AbstractLoggingActor {
// Define the actor protocol
static class IsPrimeRequest {
long candidate;
IsPrimeRequest(long candidate){this.candidate = candidate;}
}
static class IsPrimeResponse {
long candidate;
boolean isPrime;
IsPrimeResponse(long candidate, boolean isPrime){
this.candidate = candidate;
this.isPrime = isPrime;
}
}
// The actor can have "state"
private int numberOfPrimeRequests;
public static Props props() {
return Props.create(IsPrimeActor.class);
}
@Override
public void preStart() {
log().info("Starting the isPrime actor");
numberOfPrimeRequests = 0;
}
private void onPrimeRequest(IsPrimeRequest msg) {
numberOfPrimeRequests++;
long candidate = msg.candidate;
boolean prime = isPrime(candidate);
log().info("Request {} '{}' prime? {}", + numberOfPrimeRequests, candidate, prime);
getSender().tell(new IsPrimeResponse(candidate, prime), getSelf());
}
@Override
public Receive createReceive() {
return receiveBuilder()
.match(IsPrimeRequest.class, this::onPrimeRequest)
.build();
}
private boolean isPrime(long n) {
if (n <= 3) {
return n > 1;
} else if (n % 2 == 0 || n % 3 == 0) {
return false;
} else {
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
}
}
}
package com.lightbend.akka.sample;
import static org.junit.Assert.*;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.testkit.javadsl.TestKit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.*;
public class StatefulPrimeTest {
static ActorSystem system;
@BeforeClass
public static void setup() {
system = ActorSystem.create();
}
@AfterClass
public static void teardown() {
TestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void testReplyWithSmallCandidates() {
TestKit probe = new TestKit(system);
ActorRef primeActor = system.actorOf(StatefulPrime.IsPrimeActor.props());
primeActor.tell(new StatefulPrime.IsPrimeActor.IsPrimeRequest(42L), probe.getRef());
StatefulPrime.IsPrimeActor.IsPrimeResponse response = probe.expectMsgClass(StatefulPrime.IsPrimeActor.IsPrimeResponse.class);
assertEquals(42L, response.candidate);
assertEquals(false, response.isPrime);
}
@Test
public void testReplyWithMultipleCandidates() {
TestKit probe = new TestKit(system);
ActorRef primeActor = system.actorOf(StatefulPrime.IsPrimeActor.props());
for (long i = 0; i < 100; i++) {
primeActor.tell(new StatefulPrime.IsPrimeActor.IsPrimeRequest(i), probe.getRef());
StatefulPrime.IsPrimeActor.IsPrimeResponse response = probe.expectMsgClass(StatefulPrime.IsPrimeActor.IsPrimeResponse.class);
assertEquals(i, response.candidate);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment