Merge "BUG 2676 : Introduce API for accessing akka dispatchers"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / Dispatchers.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.cluster.datastore.utils;
10
11 import com.google.common.base.Preconditions;
12 import scala.concurrent.ExecutionContext;
13
14 public class Dispatchers {
15     public static final String DEFAULT_DISPATCHER_PATH = "akka.actor.default-dispatcher";
16     public static final String CLIENT_DISPATCHER_PATH = "client-dispatcher";
17     public static final String TXN_DISPATCHER_PATH = "txn-dispatcher";
18     public static final String SHARD_DISPATCHER_PATH = "shard-dispatcher";
19     public static final String NOTIFICATION_DISPATCHER_PATH = "notification-dispatcher";
20
21     private final akka.dispatch.Dispatchers dispatchers;
22
23     public static enum DispatcherType {
24         Client(CLIENT_DISPATCHER_PATH),
25         Transaction(TXN_DISPATCHER_PATH),
26         Shard(SHARD_DISPATCHER_PATH),
27         Notification(NOTIFICATION_DISPATCHER_PATH);
28
29         private final String path;
30         private DispatcherType(String path){
31             this.path = path;
32         }
33         private String path(akka.dispatch.Dispatchers dispatchers){
34             if(dispatchers.hasDispatcher(path)){
35                 return path;
36             }
37             return DEFAULT_DISPATCHER_PATH;
38         }
39
40         private ExecutionContext dispatcher(akka.dispatch.Dispatchers dispatchers){
41             if(dispatchers.hasDispatcher(path)){
42                 return dispatchers.lookup(path);
43             }
44             return dispatchers.defaultGlobalDispatcher();
45         }
46     }
47
48     public Dispatchers(akka.dispatch.Dispatchers dispatchers){
49         Preconditions.checkNotNull(dispatchers, "dispatchers should not be null");
50         this.dispatchers = dispatchers;
51     }
52
53     public ExecutionContext getDispatcher(DispatcherType dispatcherType){
54         return dispatcherType.dispatcher(this.dispatchers);
55     }
56
57     public String getDispatcherPath(DispatcherType dispatcherType){
58         return dispatcherType.path(this.dispatchers);
59     }
60 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.