2 * Copyright (c) 2014 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.sal.binding.codegen.impl;
10 import com.google.common.util.concurrent.ListeningExecutorService;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import javassist.ClassPool;
14 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeGenerator;
15 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
17 import java.util.concurrent.BlockingQueue;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.LinkedBlockingQueue;
21 import java.util.concurrent.RejectedExecutionHandler;
22 import java.util.concurrent.ThreadFactory;
23 import java.util.concurrent.ThreadPoolExecutor;
24 import java.util.concurrent.TimeUnit;
26 public class SingletonHolder {
28 public static final ClassPool CLASS_POOL = ClassPool.getDefault();
29 public static final org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator RPC_GENERATOR_IMPL = new org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator(
31 public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
32 public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
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;
38 private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
39 private static ListeningExecutorService COMMIT_EXECUTOR = null;
40 private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
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.
47 public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
49 if (NOTIFICATION_EXECUTOR == null) {
50 // Overriding the queue since we need an unbounded queue
51 // and threadpoolexecutor would not create new threads if the queue is not full
52 BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>() {
54 public boolean offer(Runnable r) {
56 // if the queue is empty (or has just 1), no need to rampup the threads
57 return super.offer(r);
59 // if the queue is not empty, force the queue to return false.
60 // threadpoolexecutor will spawn a new thread if the queue.offer returns false.
66 ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
68 ThreadPoolExecutor executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
69 NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, queue , factory,
70 new RejectedExecutionHandler() {
71 // if the max threads are met, then it will raise a rejectedExecution. We then push to the queue.
73 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
75 executor.getQueue().put(r);
76 } catch (InterruptedException e) {
82 NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
85 return NOTIFICATION_EXECUTOR;
89 * @deprecated This method is only used from configuration modules and thus callers of it
90 * should use service injection to make the executor configurable.
93 public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
94 if (COMMIT_EXECUTOR == null) {
95 ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
97 * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
98 * ordering guarantees, which means that using a concurrent threadpool results
99 * in application data being committed in random order, potentially resulting
100 * in inconsistent data being present. Once proper primitives are introduced,
101 * concurrency can be reintroduced.
103 ExecutorService executor = Executors.newSingleThreadExecutor(factory);
104 COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
107 return COMMIT_EXECUTOR;
110 public static ExecutorService getDefaultChangeEventExecutor() {
111 if (CHANGE_EVENT_EXECUTOR == null) {
112 ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
114 * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
115 * ordering guarantees, which means that using a concurrent threadpool results
116 * in application data being committed in random order, potentially resulting
117 * in inconsistent data being present. Once proper primitives are introduced,
118 * concurrency can be reintroduced.
120 ExecutorService executor = Executors.newSingleThreadExecutor(factory);
121 CHANGE_EVENT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
124 return CHANGE_EVENT_EXECUTOR;