Merge "Introduce checked sendMessage() method"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / SingletonHolder.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.binding.codegen.impl;
9
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.ThreadFactory;
14 import java.util.concurrent.ThreadPoolExecutor;
15 import java.util.concurrent.TimeUnit;
16
17 import javassist.ClassPool;
18
19 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
20 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
21
22 import com.google.common.util.concurrent.ListeningExecutorService;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import com.google.common.util.concurrent.ThreadFactoryBuilder;
25
26 public class SingletonHolder {
27
28     public static final ClassPool CLASS_POOL = new ClassPool();
29     public static final org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator RPC_GENERATOR_IMPL = new org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator(
30             CLASS_POOL);
31     public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
32     public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
33
34     public static final int CORE_NOTIFICATION_THREADS = 4;
35     public static final int MAX_NOTIFICATION_THREADS = 32;
36     public static final int NOTIFICATION_THREAD_LIFE = 15;
37
38     private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
39     private static ListeningExecutorService COMMIT_EXECUTOR = null;
40     private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
41
42     /**
43      * @deprecated This method is only used from configuration modules and thus callers of it
44      *             should use service injection to make the executor configurable.
45      */
46     @Deprecated
47     public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
48         if (NOTIFICATION_EXECUTOR == null) {
49             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
50             ExecutorService executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
51                     NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), factory);
52             NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
53         }
54         return NOTIFICATION_EXECUTOR;
55     }
56
57     /**
58      * @deprecated This method is only used from configuration modules and thus callers of it
59      *             should use service injection to make the executor configurable.
60      */
61     @Deprecated
62     public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
63         if (COMMIT_EXECUTOR == null) {
64             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
65             /*
66              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
67              *        ordering guarantees, which means that using a concurrent threadpool results
68              *        in application data being committed in random order, potentially resulting
69              *        in inconsistent data being present. Once proper primitives are introduced,
70              *        concurrency can be reintroduced.
71              */
72             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
73             COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
74         }
75
76         return COMMIT_EXECUTOR;
77     }
78
79     public static ExecutorService getDefaultChangeEventExecutor() {
80         if (CHANGE_EVENT_EXECUTOR == null) {
81             ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
82             /*
83              * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
84              *        ordering guarantees, which means that using a concurrent threadpool results
85              *        in application data being committed in random order, potentially resulting
86              *        in inconsistent data being present. Once proper primitives are introduced,
87              *        concurrency can be reintroduced.
88              */
89             ExecutorService executor = Executors.newSingleThreadExecutor(factory);
90             CHANGE_EVENT_EXECUTOR  = MoreExecutors.listeningDecorator(executor);
91         }
92
93         return CHANGE_EVENT_EXECUTOR;
94     }
95 }