2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.access.client;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.Cancellable;
13 import akka.actor.Scheduler;
14 import com.google.common.annotations.Beta;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Ticker;
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.ThreadSafe;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
21 import org.opendaylight.yangtools.concepts.Identifiable;
22 import scala.concurrent.ExecutionContext;
23 import scala.concurrent.duration.FiniteDuration;
26 * An actor context associated with this {@link AbstractClientActor}.
29 * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
30 * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
31 * a {@link Ticker}, which can be obtained via {@link #ticker()}.
33 * @author Robert Varga
37 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
38 private final ExecutionContext executionContext;
39 private final ClientIdentifier identifier;
40 private final Scheduler scheduler;
41 private final Dispatchers dispatchers;
42 private final ClientActorConfig config;
44 // Hidden to avoid subclassing
45 ClientActorContext(final ActorRef self, final String persistenceId, final ActorSystem system,
46 final ClientIdentifier identifier, final ClientActorConfig config) {
47 super(self, persistenceId);
48 this.identifier = Preconditions.checkNotNull(identifier);
49 this.scheduler = Preconditions.checkNotNull(system).scheduler();
50 this.executionContext = system.dispatcher();
51 this.dispatchers = new Dispatchers(system.dispatchers());
52 this.config = Preconditions.checkNotNull(config);
57 public ClientIdentifier getIdentifier() {
62 public ClientActorConfig config() {
67 public Dispatchers dispatchers() {
72 * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
73 * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
74 * {@link com.google.common.base.Stopwatch}.
76 * @return Client actor time source
79 public Ticker ticker() {
80 return Ticker.systemTicker();
84 * Execute a command in the context of the client actor.
86 * @param command Block of code which needs to be execute
87 * @param <T> BackendInfo type
89 public <T extends BackendInfo> void executeInActor(@Nonnull final InternalCommand<T> command) {
90 self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
93 public <T extends BackendInfo> Cancellable executeInActor(@Nonnull final InternalCommand<T> command,
94 final FiniteDuration delay) {
95 return scheduler.scheduleOnce(Preconditions.checkNotNull(delay), self(), Preconditions.checkNotNull(command),
96 executionContext, ActorRef.noSender());