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.base.Ticker;
17 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
20 import org.opendaylight.controller.cluster.common.actor.Dispatchers;
21 import org.opendaylight.controller.cluster.io.FileBackedOutputStreamFactory;
22 import org.opendaylight.controller.cluster.messaging.MessageSlicer;
23 import org.opendaylight.yangtools.concepts.Identifiable;
24 import scala.concurrent.ExecutionContext;
25 import scala.concurrent.duration.FiniteDuration;
28 * An actor context associated with this {@link AbstractClientActor}.
31 * Time-keeping in a client actor is based on monotonic time. The precision of this time can be expected to be the
32 * same as {@link System#nanoTime()}, but it is not tied to that particular clock. Actor clock is exposed as
33 * a {@link Ticker}, which can be obtained via {@link #ticker()}. This class is thread-safe.
35 public class ClientActorContext extends AbstractClientActorContext implements Identifiable<ClientIdentifier> {
36 private final ExecutionContext executionContext;
37 private final ClientIdentifier identifier;
38 private final Scheduler scheduler;
39 private final Dispatchers dispatchers;
40 private final ClientActorConfig config;
41 private final MessageSlicer messageSlicer;
43 // Hidden to avoid subclassing
44 ClientActorContext(final ActorRef self, final String persistenceId, final ActorSystem system,
45 final ClientIdentifier identifier, final ClientActorConfig config) {
46 super(self, persistenceId);
47 this.identifier = requireNonNull(identifier);
48 scheduler = requireNonNull(system).scheduler();
49 executionContext = system.dispatcher();
50 dispatchers = new Dispatchers(system.dispatchers());
51 this.config = requireNonNull(config);
53 messageSlicer = MessageSlicer.builder().messageSliceSize(config.getMaximumMessageSliceSize())
54 .logContext(persistenceId).expireStateAfterInactivity(config.getRequestTimeout(), TimeUnit.NANOSECONDS)
55 .fileBackedStreamFactory(new FileBackedOutputStreamFactory(config.getFileBackedStreamingThreshold(),
56 config.getTempFileDirectory())).build();
60 public ClientIdentifier getIdentifier() {
64 public @NonNull ClientActorConfig config() {
68 public @NonNull Dispatchers dispatchers() {
72 public @NonNull MessageSlicer messageSlicer() {
77 * Return the time ticker for this {@link ClientActorContext}. This should be used for in all time-tracking
78 * done within a client actor. Subclasses of {@link ClientActorBehavior} are encouraged to use
79 * {@link com.google.common.base.Stopwatch}.
81 * @return Client actor time source
83 public @NonNull Ticker ticker() {
84 return Ticker.systemTicker();
88 * Execute a command in the context of the client actor.
90 * @param command Block of code which needs to be execute
91 * @param <T> BackendInfo type
93 public <T extends BackendInfo> void executeInActor(final @NonNull InternalCommand<T> command) {
94 self().tell(requireNonNull(command), ActorRef.noSender());
97 public <T extends BackendInfo> Cancellable executeInActor(final @NonNull InternalCommand<T> command,
98 final FiniteDuration delay) {
99 return scheduler.scheduleOnce(requireNonNull(delay), self(), requireNonNull(command),
100 executionContext, ActorRef.noSender());