Search in sources :

Example 1 with ActorIdentity

use of akka.actor.ActorIdentity in project flink by apache.

the class AkkaRpcService method connect.

// this method does not mutate state and is thus thread-safe
@Override
public <C extends RpcGateway> Future<C> connect(final String address, final Class<C> clazz) {
    checkState(!stopped, "RpcService is stopped");
    LOG.debug("Try to connect to remote RPC endpoint with address {}. Returning a {} gateway.", address, clazz.getName());
    final ActorSelection actorSel = actorSystem.actorSelection(address);
    final scala.concurrent.Future<Object> identify = Patterns.ask(actorSel, new Identify(42), timeout.toMilliseconds());
    final scala.concurrent.Future<C> resultFuture = identify.map(new Mapper<Object, C>() {

        @Override
        public C checkedApply(Object obj) throws Exception {
            ActorIdentity actorIdentity = (ActorIdentity) obj;
            if (actorIdentity.getRef() == null) {
                throw new RpcConnectionException("Could not connect to rpc endpoint under address " + address + '.');
            } else {
                ActorRef actorRef = actorIdentity.getRef();
                final String address = AkkaUtils.getAkkaURL(actorSystem, actorRef);
                final String hostname;
                Option<String> host = actorRef.path().address().host();
                if (host.isEmpty()) {
                    hostname = "localhost";
                } else {
                    hostname = host.get();
                }
                InvocationHandler akkaInvocationHandler = new AkkaInvocationHandler(address, hostname, actorRef, timeout, maximumFramesize, null);
                // Rather than using the System ClassLoader directly, we derive the ClassLoader
                // from this class . That works better in cases where Flink runs embedded and all Flink
                // code is loaded dynamically (for example from an OSGI bundle) through a custom ClassLoader
                ClassLoader classLoader = AkkaRpcService.this.getClass().getClassLoader();
                @SuppressWarnings("unchecked") C proxy = (C) Proxy.newProxyInstance(classLoader, new Class<?>[] { clazz }, akkaInvocationHandler);
                return proxy;
            }
        }
    }, actorSystem.dispatcher());
    return new FlinkFuture<>(resultFuture);
}
Also used : ActorRef(akka.actor.ActorRef) RpcConnectionException(org.apache.flink.runtime.rpc.exceptions.RpcConnectionException) InvocationHandler(java.lang.reflect.InvocationHandler) Identify(akka.actor.Identify) RpcConnectionException(org.apache.flink.runtime.rpc.exceptions.RpcConnectionException) FlinkFuture(org.apache.flink.runtime.concurrent.impl.FlinkFuture) ActorSelection(akka.actor.ActorSelection) Option(scala.Option) ActorIdentity(akka.actor.ActorIdentity)

Example 2 with ActorIdentity

use of akka.actor.ActorIdentity in project controller by opendaylight.

the class TestActorFactory method verifyActorReady.

@SuppressWarnings("checkstyle:IllegalCatch")
private void verifyActorReady(ActorRef actorRef) {
    // Sometimes we see messages go to dead letters soon after creation - it seems the actor isn't quite
    // in a state yet to receive messages or isn't actually created yet. This seems to happen with
    // actorSelection so, to alleviate it, we use an actorSelection and send an Identify message with
    // retries to ensure it's ready.
    Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS);
    Throwable lastError = null;
    Stopwatch sw = Stopwatch.createStarted();
    while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
        try {
            ActorSelection actorSelection = system.actorSelection(actorRef.path().toString());
            Future<Object> future = Patterns.ask(actorSelection, new Identify(""), timeout);
            ActorIdentity reply = (ActorIdentity) Await.result(future, timeout.duration());
            Assert.assertNotNull("Identify returned null", reply.getRef());
            return;
        } catch (Exception | AssertionError e) {
            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
            lastError = e;
        }
    }
    throw new RuntimeException(lastError);
}
Also used : ActorSelection(akka.actor.ActorSelection) Timeout(akka.util.Timeout) Stopwatch(com.google.common.base.Stopwatch) Identify(akka.actor.Identify) InvalidActorNameException(akka.actor.InvalidActorNameException) ActorIdentity(akka.actor.ActorIdentity)

Aggregations

ActorIdentity (akka.actor.ActorIdentity)2 ActorSelection (akka.actor.ActorSelection)2 Identify (akka.actor.Identify)2 ActorRef (akka.actor.ActorRef)1 InvalidActorNameException (akka.actor.InvalidActorNameException)1 Timeout (akka.util.Timeout)1 Stopwatch (com.google.common.base.Stopwatch)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 FlinkFuture (org.apache.flink.runtime.concurrent.impl.FlinkFuture)1 RpcConnectionException (org.apache.flink.runtime.rpc.exceptions.RpcConnectionException)1 Option (scala.Option)1