Fallback to LegacyLinkedTransferQueue if using LinkedTransferQueue fails. See #268

This commit is contained in:
Norman Maurer 2012-04-17 14:04:16 +02:00
parent 5a2793e317
commit 3002a684df

View File

@ -18,6 +18,9 @@ package org.jboss.netty.util.internal;
import java.util.Collection;
import java.util.concurrent.BlockingQueue;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
/**
* This factory should be used to create the "optimal" {@link BlockingQueue}
* instance for the running JVM.
@ -25,6 +28,7 @@ import java.util.concurrent.BlockingQueue;
public final class QueueFactory {
private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(QueueFactory.class);
private QueueFactory() {
// only use static methods!
@ -38,11 +42,20 @@ public final class QueueFactory {
* @return queue the {@link BlockingQueue} implementation
*/
public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
if (useUnsafe) {
return new LinkedTransferQueue<T>();
} else {
return new LegacyLinkedTransferQueue<T>();
try {
if (useUnsafe) {
return new LinkedTransferQueue<T>();
}
} catch (Throwable t) {
// For whatever reason an exception was thrown while loading the LinkedTransferQueue
//
// This mostly happens because of a custom classloader or security policy that did not allow us to access the
// com.sun.Unmisc class. So just log it and fallback to the old LegacyLinkedTransferQueue that works in all cases
LOGGER.debug("Unable to instance LinkedTransferQueue, fallback to LegacyLinkedTransferQueue", t);
}
return new LegacyLinkedTransferQueue<T>();
}
/**
@ -53,10 +66,19 @@ public final class QueueFactory {
* @return queue the {@link BlockingQueue} implementation
*/
public static <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
if (useUnsafe) {
return new LinkedTransferQueue<T>(collection);
} else {
return new LegacyLinkedTransferQueue<T>(collection);
try {
if (useUnsafe) {
return new LinkedTransferQueue<T>(collection);
}
} catch (Throwable t) {
// For whatever reason an exception was thrown while loading the LinkedTransferQueue
//
// This mostly happens because of a custom classloader or security policy that did not allow us to access the
// com.sun.Unmisc class. So just log it and fallback to the old LegacyLinkedTransferQueue that works in all cases
LOGGER.debug("Unable to instance LinkedTransferQueue, fallback to LegacyLinkedTransferQueue", t);
}
return new LegacyLinkedTransferQueue<T>(collection);
}
}