2 * Copyright (c) 2013 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.config.yang.netty.eventexecutor;
10 import com.google.common.reflect.AbstractInvocationHandler;
11 import com.google.common.reflect.Reflection;
12 import io.netty.util.concurrent.EventExecutor;
13 import java.lang.reflect.Method;
14 import java.util.concurrent.TimeUnit;
16 public interface AutoCloseableEventExecutor extends EventExecutor, AutoCloseable {
19 public static class CloseableEventExecutorMixin implements AutoCloseable {
20 public static final int DEFAULT_SHUTDOWN_SECONDS = 1;
21 private final EventExecutor eventExecutor;
23 public CloseableEventExecutorMixin(EventExecutor eventExecutor) {
24 this.eventExecutor = eventExecutor;
29 eventExecutor.shutdownGracefully(0, DEFAULT_SHUTDOWN_SECONDS, TimeUnit.SECONDS);
33 public static AutoCloseable createCloseableProxy(final EventExecutor eventExecutor) {
34 final CloseableEventExecutorMixin closeableGlobalEventExecutorMixin =
35 new CloseableEventExecutorMixin(eventExecutor);
36 return Reflection.newProxy(AutoCloseableEventExecutor.class, new AbstractInvocationHandler() {
38 protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
39 if (method.getName().equals("close")) {
40 closeableGlobalEventExecutorMixin.close();
43 return method.invoke(eventExecutor, args);