2 * Copyright (c) 2014 Brocade Communications 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.yangtools.util.concurrent;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableList;
13 import java.util.concurrent.Executor;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager.BatchedInvoker;
20 * This class manages queuing and dispatching notifications for multiple listeners concurrently.
21 * Notifications are queued on a per-listener basis and dispatched serially to each listener via an
24 * <p>This class optimizes its memory footprint by only allocating and maintaining a queue and executor
25 * task for a listener when there are pending notifications. On the first notification(s), a queue
26 * is created and a task is submitted to the executor to dispatch the queue to the associated
27 * listener. Any subsequent notifications that occur before all previous notifications have been
28 * dispatched are appended to the existing queue. When all notifications have been dispatched, the
29 * queue and task are discarded.
31 * @author Thomas Pantelis
33 * @param <L> the listener type
34 * @param <N> the notification type
37 abstract class AbstractQueuedNotificationManager<T, L, N> extends AbstractBatchingExecutor<T, N>
38 implements NotificationManager<L, N> {
40 private final QueuedNotificationManagerMXBean mxBean = new QueuedNotificationManagerMXBeanImpl(this);
41 private final BatchedInvoker<L, N> listenerInvoker;
43 AbstractQueuedNotificationManager(final String name, final Executor executor, final int maxQueueCapacity,
44 final BatchedInvoker<L, N> listenerInvoker) {
45 super(name, executor, maxQueueCapacity);
46 this.listenerInvoker = requireNonNull(listenerInvoker);
50 * Returns the {@link Executor} to used for notification tasks.
52 public final Executor getExecutor() {
57 * Returns the maximum listener queue capacity.
59 public final int getMaxQueueCapacity() {
60 return maxQueueCapacity();
64 * Return an {@link QueuedNotificationManagerMXBean} tied to this instance.
66 * @return An QueuedNotificationManagerMXBean object.
68 public final QueuedNotificationManagerMXBean getMXBean() {
73 public final void submitNotification(final L listener, final N notification) {
74 if (listener != null && notification != null) {
75 submitTask(wrap(listener), notification);
80 public final void submitNotifications(final L listener, final @Nullable Iterable<N> notifications) {
81 if (listener != null && notifications != null) {
82 submitTasks(wrap(listener), notifications);
87 final void executeBatch(final T key, final @NonNull ImmutableList<N> tasks) {
88 listenerInvoker.invokeListener(unwrap(key), tasks);
91 abstract T wrap(L listener);
93 abstract L unwrap(T key);