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 static java.util.Objects.requireNonNull;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.actor.Cancellable;
15 import akka.actor.Scheduler;
16 import com.google.common.annotations.Beta;
17 import com.google.common.base.Ticker;
18 import java.util.concurrent.TimeUnit;
19 import javax.annotation.concurrent.ThreadSafe;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
22 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
23 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
24 import org.opendaylight.controller.cluster.messaging.MessageSlicer;
25 import org.opendaylight.yangtools.concepts.Identifiable;
26 import scala.concurrent.ExecutionContext;
27 import scala.concurrent.duration.FiniteDuration;
30 * An actor context associated with this {@link AbstractClientActor}.
33 * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
34 * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
35 * a {@link Ticker}, which can be obtained via {@link #ticker()}.
37 * @author Robert Varga
41 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
42 private final ExecutionContext executionContext;
43 private final ClientIdentifier identifier;
44 private final Scheduler scheduler;
45 private final Dispatchers dispatchers;
46 private final ClientActorConfig config;
47 private final MessageSlicer messageSlicer;
49 // Hidden to avoid subclassing
50 ClientActorContext(final ActorRef self, final String persistenceId, final ActorSystem system,
51 final ClientIdentifier identifier, final ClientActorConfig config) {
52 super(self, persistenceId);
53 this.identifier = requireNonNull(identifier);
54 this.scheduler = requireNonNull(system).scheduler();
55 this.executionContext = system.dispatcher();
56 this.dispatchers = new Dispatchers(system.dispatchers());
57 this.config = requireNonNull(config);
59 messageSlicer = MessageSlicer.builder().messageSliceSize(config.getMaximumMessageSliceSize())
60 .logContext(persistenceId).expireStateAfterInactivity(config.getRequestTimeout(), TimeUnit.NANOSECONDS)
61 .fileBackedStreamFactory(new FileBackedOutputStreamFactory(config.getFileBackedStreamingThreshold(),
62 config.getTempFileDirectory())).build();
66 public ClientIdentifier getIdentifier() {
70 public @NonNull ClientActorConfig config() {
74 public @NonNull Dispatchers dispatchers() {
78 public @NonNull MessageSlicer messageSlicer() {
83 * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
84 * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
85 * {@link com.google.common.base.Stopwatch}.
87 * @return Client actor time source
89 public @NonNull Ticker ticker() {
90 return Ticker.systemTicker();
94 * Execute a command in the context of the client actor.
96 * @param command Block of code which needs to be execute
97 * @param <T> BackendInfo type
99 public <T extends BackendInfo> void executeInActor(final @NonNull InternalCommand<T> command) {
100 self().tell(requireNonNull(command), ActorRef.noSender());
103 public <T extends BackendInfo> Cancellable executeInActor(final @NonNull InternalCommand<T> command,
104 final FiniteDuration delay) {
105 return scheduler.scheduleOnce(requireNonNull(delay), self(), requireNonNull(command),
106 executionContext, ActorRef.noSender());