View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18
    19  package org.codehaus.activemq.util;
    20
    21  import EDU.oswego.cs.dl.util.concurrent.Executor;
    22  import org.apache.commons.logging.Log;
    23  import org.apache.commons.logging.LogFactory;
    24
    25  import java.lang.reflect.InvocationHandler;
    26  import java.lang.reflect.Method;
    27  import java.lang.reflect.Proxy;
    28
    29  /***
    30   * A proxy to some service which offers an asynchronous way to invoke methods which
    31   * are void and don't throw exceptions. Calling the method results in a command object
    32   * being added to a queue for later execution.
    33   *
    34   * @version $Revision: 1.1 $
    35   */
    36  public class AsyncProxy implements InvocationHandler {
    37
    38      private Object realObject;
    39      private Executor executor;
    40      private Log log;
    41
    42      public static Object createProxy(Class interfaceType, Object realObject, Executor executor) {
    43          return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
    44                  new Class[]{interfaceType},
    45                  new AsyncProxy(realObject, executor));
    46      }
    47
    48      public AsyncProxy(Object realObject, Executor executor) {
    49          this(realObject, executor, LogFactory.getLog(AsyncProxy.class));
    50      }
    51
    52      public AsyncProxy(Object realObject, Executor executor, Log log) {
    53          this.realObject = realObject;
    54          this.executor = executor;
    55          this.log = log;
    56      }
    57
    58      public AsyncProxy(Executor executor, Log log) {
    59          this.executor = executor;
    60          this.log = log;
    61      }
    62
    63      public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
    64          if (method.getReturnType() == void.class) {
    65              executor.execute(new Runnable() {
    66                  public void run() {
    67                      doAsyncMethodInvoke(method, args);
    68                  }
    69              });
    70              return null;
    71          }
    72          else {
    73              return method.invoke(realObject, args);
    74          }
    75      }
    76
    77      protected void doAsyncMethodInvoke(Method method, Object[] args) {
    78          try {
    79              method.invoke(realObject, args);
    80          }
    81          catch (Throwable e) {
    82              log.warn("Caught exception: " + e, e);
    83          }
    84      }
    85  }