View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.util.internal;
    17
    18  import java.util.concurrent.BlockingQueue;
    19
    20  import org.jboss.netty.logging.InternalLogger;
    21  import org.jboss.netty.logging.InternalLoggerFactory;
    22
    23  /**
    24   * This factory should be used to create the "optimal" {@link BlockingQueue}
    25   * instance for the running JVM.
    26   */
    27  @Deprecated
    28  public final class QueueFactory {
    29
    30      private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
    31      private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(QueueFactory.class);
    32
    33      private QueueFactory() {
    34          // only use static methods!
    35      }
    36
    37
    38      /**
    39       * Create a new unbound {@link BlockingQueue}
    40       *
    41       * @param itemClass  the {@link Class} type which will be used as {@link BlockingQueue} items
    42       * @return queue     the {@link BlockingQueue} implementation
    43       */
    44      public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
    45          // if we run in java >=7 its the best to just use the LinkedTransferQueue which
    46          // comes with java bundled. See #273
    47          if (DetectionUtil.javaVersion() >= 7)  {
    48              return new java.util.concurrent.LinkedTransferQueue<T>();
    49          }
    50
    51          try {
    52              if (useUnsafe) {
    53                  return new LinkedTransferQueue<T>();
    54              }
    55          } catch (Throwable t) {
    56              // For whatever reason an exception was thrown while loading the LinkedTransferQueue
    57              //
    58              // This mostly happens because of a custom classloader or security policy that did not
    59              // allow us to access the com.sun.Unmisc class. So just log it and fallback to the old
    60              // LegacyLinkedTransferQueue that works in all cases
    61              if (LOGGER.isDebugEnabled()) {
    62                  LOGGER.debug("Unable to instance LinkedTransferQueue, fallback to LegacyLinkedTransferQueue", t);
    63              }
    64          }
    65
    66          return new LegacyLinkedTransferQueue<T>();
    67
    68      }
    69  }