@Override public ActorRef toActor(final ActorRefFactory context) { ActorRef ref = context.actorFor(stegklasse.getSimpleName()); if (ref.isTerminated()) { if (understeg != null) { ref = context.actorOf( new Props( new UntypedActorFactory() { @Override public Actor create() throws Exception { return stegklasse .getConstructor(ActorRef.class) .newInstance(understeg.toActor(context)); } }), stegklasse.getSimpleName()); } else { ref = context.actorOf(new Props(stegklasse), stegklasse.getSimpleName()); } } return ref; }
public static void main(String[] args) throws Exception { ApplicationContext context = SpringApplication.run(App.class, args); ActorSystem system = context.getBean(ActorSystem.class); final LoggingAdapter log = Logging.getLogger(system, App.class.getSimpleName()); log.info("Starting up"); SpringExtension ext = context.getBean(SpringExtension.class); // Use the Spring Extension to create props for a named actor bean ActorRef calculator = system.actorOf(ext.props("calculator").withMailbox("akka.priority-mailbox")); /* * Create a completion service instance to await all futures */ final ExecutionContext ec = system.dispatcher(); Timeout timeout = new Timeout(120, TimeUnit.SECONDS); List<Long> ids = new ArrayList<>(); ArrayList<Future<Object>> futures = new ArrayList<Future<Object>>(); for (int i = 1; i <= 10; i++) { Future<Object> future = Patterns.ask(calculator, new CompilationRequest(i + " + 5"), timeout); future.onSuccess( new OnSuccess<Object>() { public void onSuccess(Object result) { if (result instanceof CompilationResult) { synchronized (ids) { log.debug("Compilation result {} ", result.toString()); ids.add(((CompilationResult) result).getExpressionId()); } } else { log.info("Compilation result is unknown type {} ", result.toString()); } } }, ec); futures.add(future); } Future<Iterable<Object>> seq = Futures.sequence(futures, ec); Await.result(seq, Duration.create(30, SECONDS)); log.info("======================================================"); log.info("Done waiting for compilations...{} ids", ids.size()); log.info("======================================================"); futures.clear(); long start = System.nanoTime(); List<Double> results = new ArrayList<>(); Long count = 1_000_000L; for (long i = 1; i <= count; i++) { Future<Object> future = Patterns.ask( calculator, new CalculationRequest(i, ids.get((int) (i % ids.size())), null), timeout); future.onSuccess( new OnSuccess<Object>() { public void onSuccess(Object result) { if (result instanceof CalculationResult) { log.debug("Calculation result {} ", result.toString()); // synchronized(results) // { // results.add((Double) ((CalculationResult) result).getValue()); // } } else { log.info("Calculation result is unknown type {} ", result.toString()); } } }, ec); futures.add(future); } seq = Futures.sequence(futures, ec); Await.result(seq, Duration.create(600, SECONDS)); calculator.tell(PoisonPill.getInstance(), null); while (!calculator.isTerminated()) { Thread.sleep(100); } long end = System.nanoTime(); // //int count = context.getBean(JdbcTemplate.class).queryForObject("SELECT COUNT(*) FROM // tasks", Integer.class); Long elapsed = TimeUnit.MILLISECONDS.convert((end - start), TimeUnit.NANOSECONDS); Double tps = count.doubleValue() / (elapsed.doubleValue() / Double.parseDouble("1000")); log.info("{} calculations in {}ms {}tps", count, elapsed, tps); log.info("Shutting down ------------------------------> {}", results.size()); Thread.sleep(10000); system.shutdown(); system.awaitTermination(); }
// FSM logic. @Override public void onReceive(final Object message) throws Exception { final Class<?> klass = message.getClass(); final State state = fsm.state(); final ActorRef sender = sender(); if (logger.isInfoEnabled()) { logger.info( "********** Media Group " + self().path() + " Current State: \"" + state.toString()); logger.info( "********** Media Group " + self().path() + " Processing Message: \"" + klass.getName() + " sender : " + sender.getClass()); } if (Observe.class.equals(klass)) { observe(message); } else if (StopObserving.class.equals(klass)) { stopObserving(message); } else if (MediaGroupStatus.class.equals(klass)) { if (active.equals(state)) { sender().tell(new MediaGroupStateChanged(MediaGroupStateChanged.State.ACTIVE), self()); } else { sender().tell(new MediaGroupStateChanged(MediaGroupStateChanged.State.INACTIVE), self()); } } else if (StartMediaGroup.class.equals(klass)) { if (logger.isInfoEnabled()) { logger.info( "MediaGroup: " + self().path() + " got StartMediaGroup from: " + sender().path() + " endpoint: " + endpoint.path() + " isTerminated: " + endpoint.isTerminated()); } fsm.transition(message, acquiringIvr); } else if (Join.class.equals(klass)) { fsm.transition(message, acquiringInternalLink); } else if (MediaGatewayResponse.class.equals(klass)) { if (acquiringIvr.equals(state)) { fsm.transition(message, acquiringLink); } else if (acquiringLink.equals(state)) { fsm.transition(message, initializingLink); } else if (acquiringInternalLink.equals(state)) { fsm.transition(message, initializingInternalLink); } } else if (LinkStateChanged.class.equals(klass)) { final LinkStateChanged response = (LinkStateChanged) message; if (LinkStateChanged.State.CLOSED == response.state()) { if (initializingLink.equals(state)) { fsm.transition(message, openingLink); } else if (openingLink.equals(state) || deactivating.equals(state) || updatingLink.equals(state)) { fsm.transition(message, inactive); } if (initializingInternalLink.equals(state)) { fsm.transition(message, openingInternalLink); } } else if (LinkStateChanged.State.OPEN == response.state()) { if (openingLink.equals(state)) { fsm.transition(message, updatingLink); } else if (updatingLink.equals(state)) { fsm.transition(message, active); } if (openingInternalLink.equals(state)) { fsm.transition(message, updatingInternalLink); } if (updatingInternalLink.equals(state)) { fsm.transition(message, active); } } } else if (StopMediaGroup.class.equals(klass)) { if (acquiringLink.equals(state) || initializingLink.equals(state)) { fsm.transition(message, inactive); } else if (active.equals(state) || openingLink.equals(state) || updatingLink.equals(state)) { fsm.transition(message, deactivating); } } else if (EndpointStateChanged.class.equals(klass)) { onEndpointStateChanged((EndpointStateChanged) message, self(), sender); } else if (active.equals(state)) { if (Play.class.equals(klass)) { play(message); } else if (Collect.class.equals(klass)) { collect(message); } else if (Record.class.equals(klass)) { record(message); } else if (Stop.class.equals(klass)) { stop(lastEvent); // Send message to originator telling media group has been stopped // Needed for call bridging scenario, where inbound call must stop // ringing before attempting to perform join operation. sender().tell(new MediaGroupResponse<String>("stopped"), self()); } else if (IvrEndpointResponse.class.equals(klass)) { notification(message); } } else if (ivrInUse) { if (Stop.class.equals(klass)) { stop(lastEvent); } } }