diff --git a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java index 565de754918..8c175e96688 100644 --- a/common/src/main/java/org/apache/rocketmq/common/UtilAll.java +++ b/common/src/main/java/org/apache/rocketmq/common/UtilAll.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.Inet4Address; import java.net.Inet6Address; @@ -42,11 +43,15 @@ import java.util.zip.CRC32; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterInputStream; +import org.apache.commons.lang3.JavaVersion; +import org.apache.commons.lang3.SystemUtils; import org.apache.commons.validator.routines.InetAddressValidator; import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLoggerFactory; import org.apache.rocketmq.remoting.common.RemotingHelper; +import sun.misc.Unsafe; +import sun.nio.ch.DirectBuffer; public class UtilAll { private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME); @@ -670,7 +675,19 @@ public static void cleanBuffer(final ByteBuffer buffer) { if (buffer == null || !buffer.isDirect() || buffer.capacity() == 0) { return; } - invoke(invoke(viewed(buffer), "cleaner"), "clean"); + if (SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_9)) { + try { + Field field = Unsafe.class.getDeclaredField("theUnsafe"); + field.setAccessible(true); + Unsafe unsafe = (Unsafe) field.get(null); + Method cleaner = method(unsafe, "invokeCleaner", new Class[] {ByteBuffer.class}); + cleaner.invoke(unsafe, viewed(buffer)); + } catch (Exception e) { + throw new IllegalStateException(e); + } + } else { + invoke(invoke(viewed(buffer), "cleaner"), "clean"); + } } public static Object invoke(final Object target, final String methodName, final Class... args) { @@ -697,17 +714,10 @@ public static Method method(Object target, String methodName, Class[] args) t } private static ByteBuffer viewed(ByteBuffer buffer) { - String methodName = "viewedBuffer"; - - Method[] methods = buffer.getClass().getMethods(); - for (Method method : methods) { - if (method.getName().equals("attachment")) { - methodName = "attachment"; - break; - } + if (!buffer.isDirect()) { + throw new IllegalArgumentException("buffer is non-direct"); } - - ByteBuffer viewedBuffer = (ByteBuffer) invoke(buffer, methodName); + ByteBuffer viewedBuffer = (ByteBuffer) ((DirectBuffer) buffer).attachment(); if (viewedBuffer == null) { return buffer; } else { diff --git a/pom.xml b/pom.xml index 958c9015a3f..cdae906a1ec 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,7 @@ 1.2.69_noneautotype 3.20.0-GA 4.2.2 - 3.4 + 3.12.0 2.7 31.0.1-jre 0.3.1-alpha