Fallback to LegacyLinkedTransferQueue if using LinkedTransferQueue fails. See #268
This commit is contained in:
parent
02dc9ea8c1
commit
b3b5fb1de6
@ -18,6 +18,9 @@ package io.netty.util.internal;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.BlockingQueue;
|
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}
|
* This factory should be used to create the "optimal" {@link BlockingQueue}
|
||||||
* instance for the running JVM.
|
* instance for the running JVM.
|
||||||
@ -25,6 +28,7 @@ import java.util.concurrent.BlockingQueue;
|
|||||||
public final class QueueFactory {
|
public final class QueueFactory {
|
||||||
|
|
||||||
private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
|
private static final boolean useUnsafe = DetectionUtil.hasUnsafe();
|
||||||
|
private static final InternalLogger LOGGER = InternalLoggerFactory.getInstance(QueueFactory.class);
|
||||||
|
|
||||||
private QueueFactory() {
|
private QueueFactory() {
|
||||||
// only use static methods!
|
// only use static methods!
|
||||||
@ -38,11 +42,20 @@ public final class QueueFactory {
|
|||||||
* @return queue the {@link BlockingQueue} implementation
|
* @return queue the {@link BlockingQueue} implementation
|
||||||
*/
|
*/
|
||||||
public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
|
public static <T> BlockingQueue<T> createQueue(Class<T> itemClass) {
|
||||||
if (useUnsafe) {
|
try {
|
||||||
return new LinkedTransferQueue<T>();
|
if (useUnsafe) {
|
||||||
} else {
|
return new LinkedTransferQueue<T>();
|
||||||
return new LegacyLinkedTransferQueue<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
|
* @return queue the {@link BlockingQueue} implementation
|
||||||
*/
|
*/
|
||||||
public static <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
|
public static <T> BlockingQueue<T> createQueue(Collection<? extends T> collection, Class<T> itemClass) {
|
||||||
if (useUnsafe) {
|
try {
|
||||||
return new LinkedTransferQueue<T>(collection);
|
if (useUnsafe) {
|
||||||
} else {
|
return new LinkedTransferQueue<T>(collection);
|
||||||
return new LegacyLinkedTransferQueue<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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user