public static void startFrontend(Address joinAddress) { ActorSystem system = ActorSystem.create(systemName); Cluster.get(system).join(joinAddress); ActorRef frontend = system.actorOf(Props.create(Frontend.class), "frontend"); system.actorOf(Props.create(WorkProducer.class, frontend), "producer"); system.actorOf(Props.create(WorkResultConsumer.class), "consumer"); }
@Test public void testingWithCustomProps() { TestProbe probe = new TestProbe(system); Props childProps = Props.create(MockedChild.class); TestActorRef<DependentParent> parent = TestActorRef.create(system, Props.create(DependentParent.class, childProps)); probe.send(parent, "pingit"); // test some parent state change assertTrue(parent.underlyingActor().ponged == true || parent.underlyingActor().ponged == false); }
@Test public void testingWithoutParent() { TestProbe probe = new TestProbe(system); ActorRef child = system.actorOf(Props.create(DependentChild.class, probe.ref())); probe.send(child, "ping"); probe.expectMsg("pong"); }
public static void startWorker(Address contactAddress) { ActorSystem system = ActorSystem.create(systemName); Set<ActorSelection> initialContacts = new HashSet<ActorSelection>(); initialContacts.add(system.actorSelection(contactAddress + "/user/receptionist")); ActorRef clusterClient = system.actorOf(ClusterClient.defaultProps(initialContacts), "clusterClient"); system.actorOf(Worker.props(clusterClient, Props.create(WorkExecutor.class)), "worker"); }
@Test public void useAsk() throws Exception { ActorRef testActor = system.actorOf(Props.create(JavaAPITestActor.class), "test"); assertEquals( "Ask should return expected answer", JavaAPITestActor.ANSWER, Await.result(ask(testActor, "hey!", 3000), Duration.create(3, "seconds"))); }
@Test public void useAskWithActorSelection() throws Exception { ActorRef testActor = system.actorOf(Props.create(JavaAPITestActor.class), "test2"); ActorSelection selection = system.actorSelection("/user/test2"); ActorIdentity id = (ActorIdentity) Await.result(ask(selection, new Identify("yo!"), 3000), Duration.create(3, "seconds")); assertEquals("Ask (Identify) should return the proper ActorIdentity", testActor, id.getRef()); }
static Props props(final List<Pair<ActorRef, ActorRef>> targetPorts, final Bolt bolt) { return Props.create( new Creator<LatencyMonitor>() { private static final long serialVersionUID = 1L; public LatencyMonitor create() throws Exception { return new LatencyMonitor(targetPorts, bolt); } }); }
@Test public void testProbeParentTest() throws Exception { // #test-TestProbe-parent JavaTestKit parent = new JavaTestKit(system); ActorRef child = parent.childActorOf(Props.create(Child.class)); parent.send(child, "ping"); parent.expectMsgEquals("pong"); // #test-TestProbe-parent }
@Test public void fabricatedParentTestsItsChildResponses() throws Exception { // didn't put final on these in order to make the parent fit in one line in the html docs // #test-fabricated-parent TestProbe proxy = new TestProbe(system); ActorRef parent = system.actorOf(Props.create(new FabricatedParentCreator(proxy))); proxy.send(parent, "ping"); proxy.expectMsg("pong"); // #test-fabricated-parent }
public Master() { final ActorRef reader = getContext().actorOf(Props.create(FileReader.class), "fileReader"); final ActorRef writer = getContext().actorOf(Props.create(FileWriter.class), "fileWriter"); receive( ReceiveBuilder.match( ReadFile.class, readFile -> { reader.tell(new GetMap(), self()); }) .match( Result.class, result -> { writer.tell(result, self()); }) .match( End.class, end -> { context().system().shutdown(); }) .build()); }
public void exampleProdActorFactoryFunction() throws Exception { // #child-maker-prod Function<ActorRefFactory, ActorRef> maker = new Function<ActorRefFactory, ActorRef>() { @Override public ActorRef apply(ActorRefFactory f) throws Exception { return f.actorOf(Props.create(Child.class)); } }; ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker)); // #child-maker-prod }
public static void main(String[] args) { if (args.length != 6) { System.err.println("Incorrect input args"); System.exit(-1); } final String fileName = args[0]; final int minId = Integer.valueOf(args[1]); final int maxId = Integer.valueOf(args[2]); final double minAmount = Double.parseDouble(args[3]); final double maxAmount = Double.parseDouble(args[4]); final int recordsAmount = Integer.parseInt(args[5]); final ActorSystem system = ActorSystem.create("FileGeneration"); ActorRef detailsFileWriter = system.actorOf(Props.create(RandomInformationFileWriter.class), "detailsFileWriter"); system.actorOf(Props.create(Terminator.class, detailsFileWriter), "terminator"); RandomInformationFileWriter.Details details = new RandomInformationFileWriter.Details( fileName, minId, maxId, minAmount, maxAmount, recordsAmount); detailsFileWriter.tell(details, null); }
// #test-example static class Parent extends UntypedActor { final ActorRef child = context().actorOf(Props.create(Child.class), "child"); boolean ponged = false; @Override public void onReceive(Object message) throws Exception { if ("pingit".equals(message)) { child.tell("ping", self()); } else if ("pong".equals(message)) { ponged = true; } else { unhandled(message); } } }
@Test public void testingWithChildProbe() throws Exception { final TestProbe probe = new TestProbe(system); // #child-maker-test Function<ActorRefFactory, ActorRef> maker = new Function<ActorRefFactory, ActorRef>() { @Override public ActorRef apply(ActorRefFactory param) throws Exception { return probe.ref(); } }; ActorRef parent = system.actorOf(Props.create(GenericDependentParent.class, maker)); // #child-maker-test probe.send(parent, "pingit"); probe.expectMsg("ping"); }
public static Props props( ActorRef clusterClient, Props workExecutorProps, FiniteDuration registerInterval) { return Props.create(Worker.class, clusterClient, workExecutorProps, registerInterval); }
public static void main(String[] args) { ActorSystem system = ActorSystem.create("Hello"); ActorRef a = system.actorOf(Props.create(HelloWorld.class), "helloWorld"); system.actorOf(Props.create(Terminator.class, a), "terminator"); }
public static Props props(ActorRef out, String username) { return Props.create(ChatWebSocketActor.class, out, username); }
/** * Create a Props for the specified actorBeanName using the SpringActorProducerByName class. * * @param actorClass The name of the actor bean to create Props for * @return a Props that will create the named actor bean using Spring */ public Props props(Class<? extends Actor> actorClass) { return Props.create(SpringActorProducerByType.class, applicationContext, actorClass); }
/** * Create a Props for the specified actorBeanName using the SpringActorProducerByName class. * * @param actorBeanName The name of the actor bean to create Props for * @return a Props that will create the named actor bean using Spring */ public Props props(String actorBeanName) { return Props.create(SpringActorProducerByName.class, applicationContext, actorBeanName); }