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 io.netty.util.concurrent.GlobalEventExecutor;
14 import io.netty.util.concurrent.ImmediateEventExecutor;
15 import java.lang.reflect.Method;
16 import java.util.concurrent.TimeUnit;
18 public interface AutoCloseableEventExecutor extends EventExecutor, AutoCloseable {
19 static AutoCloseableEventExecutor globalEventExecutor() {
20 return createCloseableProxy(GlobalEventExecutor.INSTANCE);
23 static AutoCloseableEventExecutor immediateEventExecutor() {
24 return createCloseableProxy(ImmediateEventExecutor.INSTANCE);
27 private static AutoCloseableEventExecutor createCloseableProxy(final EventExecutor eventExecutor) {
28 return Reflection.newProxy(AutoCloseableEventExecutor.class, new AbstractInvocationHandler() {
30 protected Object handleInvocation(final Object proxy, final Method method, final Object[] args)
32 if (method.getName().equals("close")) {
33 eventExecutor.shutdownGracefully(0, 1, TimeUnit.SECONDS);
36 return method.invoke(eventExecutor, args);