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.Cancellable;
12 import akka.actor.Scheduler;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Ticker;
16 import javax.annotation.Nonnull;
17 import javax.annotation.concurrent.ThreadSafe;
18 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
19 import org.opendaylight.yangtools.concepts.Identifiable;
20 import scala.concurrent.ExecutionContext;
21 import scala.concurrent.duration.FiniteDuration;
24 * An actor context associated with this {@link AbstractClientActor}.
27 * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
28 * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
29 * a {@link Ticker}, which can be obtained via {@link #ticker()}.
31 * @author Robert Varga
35 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
36 private final ExecutionContext executionContext;
37 private final ClientIdentifier identifier;
38 private final Scheduler scheduler;
40 // Hidden to avoid subclassing
41 ClientActorContext(final ActorRef self, final Scheduler scheduler, final ExecutionContext executionContext,
42 final String persistenceId, final ClientIdentifier identifier) {
43 super(self, persistenceId);
44 this.identifier = Preconditions.checkNotNull(identifier);
45 this.scheduler = Preconditions.checkNotNull(scheduler);
46 this.executionContext = Preconditions.checkNotNull(executionContext);
51 public ClientIdentifier getIdentifier() {
56 * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
57 * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
58 * {@link com.google.common.base.Stopwatch}.
60 * @return Client actor time source
63 public Ticker ticker() {
64 return Ticker.systemTicker();
68 * Execute a command in the context of the client actor.
70 * @param command Block of code which needs to be execute
71 * @param <T> BackendInfo type
73 public <T extends BackendInfo> void executeInActor(@Nonnull final InternalCommand<T> command) {
74 self().tell(Preconditions.checkNotNull(command), ActorRef.noSender());
77 public <T extends BackendInfo> Cancellable executeInActor(@Nonnull final InternalCommand<T> command,
78 final FiniteDuration delay) {
79 return scheduler.scheduleOnce(Preconditions.checkNotNull(delay), self(), Preconditions.checkNotNull(command),
80 executionContext, ActorRef.noSender());