Merge "Ganymed patch fix"
[controller.git] / opendaylight / config / threadpool-config-impl / src / main / java / org / opendaylight / controller / config / threadpool / util / CloseableEventBus.java
1 /*
2  * Copyright (c) 2013 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.config.threadpool.util;
10
11 import java.io.Closeable;
12
13 import org.opendaylight.controller.config.yang.threadpool.impl.EventBusRuntimeMXBean;
14 import org.opendaylight.controller.config.yang.threadpool.impl.EventBusRuntimeRegistration;
15 import org.opendaylight.controller.config.yang.threadpool.impl.EventBusRuntimeRegistrator;
16
17 import com.google.common.eventbus.DeadEvent;
18 import com.google.common.eventbus.EventBus;
19 import com.google.common.eventbus.Subscribe;
20
21 /**
22  * Closeable {@link EventBus}.
23  */
24 public class CloseableEventBus extends EventBus implements Closeable {
25
26     private final EventBusRuntimeRegistration rootRegistration;
27
28     public CloseableEventBus(String identifier, EventBusRuntimeRegistrator rootRegistrator) {
29         super(identifier);
30         rootRegistration = rootRegistrator.register(new EventBusRuntimeMXBean() {
31             private long deadEventsCounter = 0;
32
33             @Subscribe
34             public void increaseDeadEvents(DeadEvent deadEvent) {
35                 deadEventsCounter++;
36             }
37
38             @Override
39             public Long countDeadEvents() {
40                 return deadEventsCounter;
41             }
42         });
43
44     }
45
46     @Override
47     public void close() {
48         rootRegistration.close();
49
50     }
51 }