diff --git a/logback-core/pom.xml b/logback-core/pom.xml index 53d0ab55a6..e710dab38e 100755 --- a/logback-core/pom.xml +++ b/logback-core/pom.xml @@ -3,156 +3,190 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - 4.0.0 - - - ch.qos.logback - logback-parent - 1.5.0-SNAPSHOT - - - logback-core - jar - Logback Core Module - logback-core module - - - ch.qos.logback.core - - - - - - org.codehaus.janino - janino - compile - true - - - org.codehaus.janino - commons-compiler - compile - true - - - org.fusesource.jansi - jansi - true - - - - jakarta.mail - jakarta.mail-api - compile - true - - - - org.eclipse.angus - angus-mail - test - - - - jakarta.servlet - jakarta.servlet-api - compile - true - - - - - org.mockito - mockito-core - test - - - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - - - - --add-opens ch.qos.logback.core/ch.qos.logback.core.testUtil=java.naming - --add-reads ch.qos.logback.core=ALL-UNNAMED - - classes - 8 - - 1 - true - plain - false - - true - - **/All*Test.java - **/PackageTest.java - - **/ConsoleAppenderTest.java - - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - - ${project.build.outputDirectory}/META-INF/MANIFEST.MF - - - - - - bundle-test-jar - package - - test-jar - - - - - - - org.apache.felix - maven-bundle-plugin - - - - bundle-manifest - process-classes - - manifest - - - - - - ch.qos.logback.core* - - ch.qos.logback.core*;version="${range;[==,+);${version_cleanup;${project.version}}}", - jakarta.*;resolution:=optional, - org.xml.*;resolution:=optional, - org.fusesource.jansi;resolution:=optional, - org.codehaus.janino;resolution:=optional, - org.codehaus.commons.compiler;resolution:=optional, - * - - - - - - + 4.0.0 + + + ch.qos.logback + logback-parent + 1.5.0-SNAPSHOT + + + logback-core + jar + Logback Core Module + logback-core module + + + ch.qos.logback.core + + + + + + org.codehaus.janino + janino + compile + true + + + org.codehaus.janino + commons-compiler + compile + true + + + org.fusesource.jansi + jansi + true + + + + jakarta.mail + jakarta.mail-api + compile + true + + + + org.eclipse.angus + angus-mail + test + + + + jakarta.servlet + jakarta.servlet-api + compile + true + + + + + org.mockito + mockito-core + test + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + + + default-compile + + compile + + + ${jdk.version} + ${jdk.version} + + + + + java21-compile + compile + + compile + + + 21 + + ${project.basedir}/src/main/java21 + + true + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + + + --add-opens ch.qos.logback.core/ch.qos.logback.core.testUtil=java.naming + --add-reads ch.qos.logback.core=ALL-UNNAMED + + classes + 8 + + 1 + true + plain + false + + true + + **/All*Test.java + **/PackageTest.java + + **/ConsoleAppenderTest.java + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + ${project.build.outputDirectory}/META-INF/MANIFEST.MF + + + + + + bundle-test-jar + package + + test-jar + + + + + + + org.apache.felix + maven-bundle-plugin + + + + bundle-manifest + process-classes + + manifest + + + + + + true + ch.qos.logback.core* + + ch.qos.logback.core*;version="${range;[==,+);${version_cleanup;${project.version}}}", + jakarta.*;resolution:=optional, + org.xml.*;resolution:=optional, + org.fusesource.jansi;resolution:=optional, + org.codehaus.janino;resolution:=optional, + org.codehaus.commons.compiler;resolution:=optional, + * + + + + + + diff --git a/logback-core/src/main/java/ch/qos/logback/core/util/ExecutorServiceUtil.java b/logback-core/src/main/java/ch/qos/logback/core/util/ExecutorServiceUtil.java index f7e1e03a83..97ac296ecd 100755 --- a/logback-core/src/main/java/ch/qos/logback/core/util/ExecutorServiceUtil.java +++ b/logback-core/src/main/java/ch/qos/logback/core/util/ExecutorServiceUtil.java @@ -1,15 +1,13 @@ /** - * Logback: the reliable, generic, fast and flexible logging framework. - * Copyright (C) 1999-2015, QOS.ch. All rights reserved. + * Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2015, QOS.ch. All rights + * reserved. * - * This program and the accompanying materials are dual-licensed under - * either the terms of the Eclipse Public License v1.0 as published by - * the Eclipse Foundation + * This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License + * v1.0 as published by the Eclipse Foundation * - * or (per the licensee's choosing) + * or (per the licensee's choosing) * - * under the terms of the GNU Lesser General Public License version 2.1 - * as published by the Free Software Foundation. + * under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. */ package ch.qos.logback.core.util; @@ -29,22 +27,16 @@ /** * Static utility methods for manipulating an {@link ExecutorService}. - * + * * @author Carl Harris * @author Mikhail Mazursky */ public class ExecutorServiceUtil { - static private final String NEW_VIRTUAL_TPT_METHOD_NAME = "newVirtualThreadPerTaskExecutor"; - - public static final String OF_VIRTUAL_METHOD_NAME = "ofVirtual"; - public static final String FACTORY_METHOD_NAME = "factory"; - private static final ThreadFactory THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE = new ThreadFactory() { private final AtomicInteger threadNumber = new AtomicInteger(1); - private final ThreadFactory defaultFactory = makeThreadFactory(); /** @@ -53,19 +45,7 @@ public class ExecutorServiceUtil { * @return */ private ThreadFactory makeThreadFactory() { - if(EnvUtil.isJDK21OrHigher()) { - try { - Method ofVirtualMethod = Thread.class.getMethod(OF_VIRTUAL_METHOD_NAME); - Object threadBuilderOfVirtual = ofVirtualMethod.invoke(null); - Method factoryMethod = threadBuilderOfVirtual.getClass().getMethod(FACTORY_METHOD_NAME); - return (ThreadFactory) factoryMethod.invoke(threadBuilderOfVirtual); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - return Executors.defaultThreadFactory(); - } - - } else { - return Executors.defaultThreadFactory(); - } + return Executors.defaultThreadFactory(); } @Override @@ -85,13 +65,12 @@ static public ScheduledExecutorService newScheduledExecutorService() { } /** - * @deprecated replaced by {@link #newThreadPoolExecutor()} + * @deprecated replaced by {@link #newThreadPoolExecutor()} */ static public ExecutorService newExecutorService() { - return newThreadPoolExecutor(); + return newThreadPoolExecutor(); } - /** * Creates an ThreadPoolExecutor suitable for use by logback components. * @@ -100,17 +79,18 @@ static public ExecutorService newExecutorService() { */ static public ThreadPoolExecutor newThreadPoolExecutor() { return new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, CoreConstants.MAX_POOL_SIZE, 0L, - TimeUnit.MILLISECONDS, new SynchronousQueue(), THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE); + TimeUnit.MILLISECONDS, new SynchronousQueue(), + THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE); } /** * Shuts down an executor service. *

- * + * * @param executorService the executor service to shut down */ static public void shutdown(ExecutorService executorService) { - if(executorService != null) { + if (executorService != null) { executorService.shutdownNow(); } } @@ -122,16 +102,6 @@ static public void shutdown(ExecutorService executorService) { * @since 1.3.12/1.4.12 */ static public ExecutorService newAlternateThreadPoolExecutor() { - - if(EnvUtil.isJDK21OrHigher()) { - try { - Method newVirtualTPTMethod = Executors.class.getMethod(NEW_VIRTUAL_TPT_METHOD_NAME); - return (ExecutorService) newVirtualTPTMethod.invoke(null); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - return newThreadPoolExecutor(); - } - } else { - return newThreadPoolExecutor(); - } + return newThreadPoolExecutor(); } } diff --git a/logback-core/src/main/java21/ch/qos/logback/core/util/ExecutorServiceUtil.java b/logback-core/src/main/java21/ch/qos/logback/core/util/ExecutorServiceUtil.java new file mode 100644 index 0000000000..221f23447d --- /dev/null +++ b/logback-core/src/main/java21/ch/qos/logback/core/util/ExecutorServiceUtil.java @@ -0,0 +1,109 @@ +/** + * Logback: the reliable, generic, fast and flexible logging framework. + * Copyright (C) 1999-2015, QOS.ch. All rights reserved. + * + * This program and the accompanying materials are dual-licensed under + * either the terms of the Eclipse Public License v1.0 as published by + * the Eclipse Foundation + * + * or (per the licensee's choosing) + * + * under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation. + */ +package ch.qos.logback.core.util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import ch.qos.logback.core.CoreConstants; + +/** + * Static utility methods for manipulating an {@link ExecutorService}. + * + * @author Carl Harris + * @author Mikhail Mazursky + */ +public class ExecutorServiceUtil { + private static final ThreadFactory THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE = new ThreadFactory() { + + private final AtomicInteger threadNumber = new AtomicInteger(1); + + private final ThreadFactory defaultFactory = makeThreadFactory(); + + /** + * A thread factory which may be a virtual thread factory the JDK supports it. + * + * @return + */ + private ThreadFactory makeThreadFactory() { + ThreadFactory tf = Thread.ofVirtual().factory(); + return tf; + } + + @Override + public Thread newThread(Runnable r) { + Thread thread = defaultFactory.newThread(r); + if (!thread.isDaemon()) { + thread.setDaemon(true); + } + thread.setName("logback-" + threadNumber.getAndIncrement()); + return thread; + } + }; + + static public ScheduledExecutorService newScheduledExecutorService() { + return new ScheduledThreadPoolExecutor(CoreConstants.SCHEDULED_EXECUTOR_POOL_SIZE, + THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE); + } + + /** + * @deprecated replaced by {@link #newThreadPoolExecutor()} + */ + static public ExecutorService newExecutorService() { + return newThreadPoolExecutor(); + } + + /** + * Creates an ThreadPoolExecutor suitable for use by logback components. + * + * @return ThreadPoolExecutor + * @since 1.4.7 + */ + static public ThreadPoolExecutor newThreadPoolExecutor() { + return new ThreadPoolExecutor(CoreConstants.CORE_POOL_SIZE, CoreConstants.MAX_POOL_SIZE, 0L, + TimeUnit.MILLISECONDS, new SynchronousQueue(), + THREAD_FACTORY_FOR_SCHEDULED_EXECUTION_SERVICE); + } + + /** + * Shuts down an executor service. + *

+ * + * @param executorService the executor service to shut down + */ + static public void shutdown(ExecutorService executorService) { + if (executorService != null) { + executorService.shutdownNow(); + } + } + + /** + * An alternate implementation of {@linl #newThreadPoolExecutor} which returns a virtual thread per task executor + * when available. + * + * @since 1.3.12/1.4.12 + */ + static public ExecutorService newAlternateThreadPoolExecutor() { + return Executors.newVirtualThreadPerTaskExecutor(); + } +}