|
| 1 | +// Copyright (c) 2024 Broadcom. All Rights Reserved. |
| 2 | +// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// |
| 4 | +// This software, the RabbitMQ Stream Java client library, is dual-licensed under the |
| 5 | +// Mozilla Public License 2.0 ("MPL"), and the Apache License version 2 ("ASL"). |
| 6 | +// For the MPL, please see LICENSE-MPL-RabbitMQ. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | +package com.rabbitmq.stream.impl; |
| 16 | + |
| 17 | +import java.lang.reflect.InvocationTargetException; |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.concurrent.ExecutorService; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | +import java.util.concurrent.ThreadFactory; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.function.Predicate; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | + |
| 29 | +final class ThreadUtils { |
| 30 | + |
| 31 | + private static final Logger LOGGER = LoggerFactory.getLogger(ThreadUtils.class); |
| 32 | + |
| 33 | + private static final ThreadFactory THREAD_FACTORY; |
| 34 | + private static final Function<String, ExecutorService> EXECUTOR_SERVICE_FACTORY; |
| 35 | + private static final Predicate<Thread> IS_VIRTUAL; |
| 36 | + |
| 37 | + static { |
| 38 | + if (isJava21OrMore()) { |
| 39 | + LOGGER.debug("Running Java 21 or more, using virtual threads"); |
| 40 | + Class<?> builderClass = |
| 41 | + Arrays.stream(Thread.class.getDeclaredClasses()) |
| 42 | + .filter(c -> "Builder".equals(c.getSimpleName())) |
| 43 | + .findFirst() |
| 44 | + .get(); |
| 45 | + // Reflection code is the same as: |
| 46 | + // Thread.ofVirtual().factory(); |
| 47 | + try { |
| 48 | + Object builder = Thread.class.getDeclaredMethod("ofVirtual").invoke(null); |
| 49 | + THREAD_FACTORY = (ThreadFactory) builderClass.getDeclaredMethod("factory").invoke(builder); |
| 50 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { |
| 51 | + throw new RuntimeException(e); |
| 52 | + } |
| 53 | + EXECUTOR_SERVICE_FACTORY = |
| 54 | + prefix -> { |
| 55 | + try { |
| 56 | + // Reflection code is the same as the 2 following lines: |
| 57 | + // ThreadFactory factory = Thread.ofVirtual().name(prefix, 0).factory(); |
| 58 | + // Executors.newThreadPerTaskExecutor(factory); |
| 59 | + Object builder = Thread.class.getDeclaredMethod("ofVirtual").invoke(null); |
| 60 | + if (prefix != null) { |
| 61 | + builder = |
| 62 | + builderClass |
| 63 | + .getDeclaredMethod("name", String.class, Long.TYPE) |
| 64 | + .invoke(builder, prefix, 0L); |
| 65 | + } |
| 66 | + ThreadFactory factory = |
| 67 | + (ThreadFactory) builderClass.getDeclaredMethod("factory").invoke(builder); |
| 68 | + return (ExecutorService) |
| 69 | + Executors.class |
| 70 | + .getDeclaredMethod("newThreadPerTaskExecutor", ThreadFactory.class) |
| 71 | + .invoke(null, factory); |
| 72 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + }; |
| 76 | + IS_VIRTUAL = |
| 77 | + thread -> { |
| 78 | + Method method = null; |
| 79 | + try { |
| 80 | + method = Thread.class.getDeclaredMethod("isVirtual"); |
| 81 | + return (boolean) method.invoke(thread); |
| 82 | + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { |
| 83 | + LOGGER.info("Error while checking if a thread is virtual: {}", e.getMessage()); |
| 84 | + return false; |
| 85 | + } |
| 86 | + }; |
| 87 | + } else { |
| 88 | + THREAD_FACTORY = Executors.defaultThreadFactory(); |
| 89 | + EXECUTOR_SERVICE_FACTORY = prefix -> Executors.newCachedThreadPool(threadFactory(prefix)); |
| 90 | + IS_VIRTUAL = ignored -> false; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private ThreadUtils() {} |
| 95 | + |
| 96 | + static ThreadFactory threadFactory(String prefix) { |
| 97 | + if (prefix == null) { |
| 98 | + return Executors.defaultThreadFactory(); |
| 99 | + } else { |
| 100 | + return new NamedThreadFactory(prefix); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + static ThreadFactory internalThreadFactory(String prefix) { |
| 105 | + return new NamedThreadFactory(THREAD_FACTORY, prefix); |
| 106 | + } |
| 107 | + |
| 108 | + static boolean isVirtual(Thread thread) { |
| 109 | + return IS_VIRTUAL.test(thread); |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean isJava21OrMore() { |
| 113 | + return Runtime.version().compareTo(Runtime.Version.parse("21")) >= 0; |
| 114 | + } |
| 115 | + |
| 116 | + private static class NamedThreadFactory implements ThreadFactory { |
| 117 | + |
| 118 | + private final ThreadFactory backingThreadFactory; |
| 119 | + |
| 120 | + private final String prefix; |
| 121 | + |
| 122 | + private final AtomicLong count = new AtomicLong(0); |
| 123 | + |
| 124 | + private NamedThreadFactory(String prefix) { |
| 125 | + this(Executors.defaultThreadFactory(), prefix); |
| 126 | + } |
| 127 | + |
| 128 | + private NamedThreadFactory(ThreadFactory backingThreadFactory, String prefix) { |
| 129 | + this.backingThreadFactory = backingThreadFactory; |
| 130 | + this.prefix = prefix; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Thread newThread(Runnable r) { |
| 135 | + Thread thread = this.backingThreadFactory.newThread(r); |
| 136 | + thread.setName(prefix + count.getAndIncrement()); |
| 137 | + return thread; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments