Refactor thread factories. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9afe7ea8 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9afe7ea8 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9afe7ea8
Branch: refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure Commit: 9afe7ea8544942a6ddea68fbdce4428473d07a06 Parents: 932824a Author: Gary Gregory <ggreg...@apache.org> Authored: Sat Aug 20 11:47:08 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Sat Aug 20 11:47:08 2016 -0700 ---------------------------------------------------------------------- .../core/async/AsyncLoggerConfigDisruptor.java | 3 +- .../log4j/core/async/AsyncLoggerDisruptor.java | 3 +- .../log4j/core/async/DaemonThreadFactory.java | 51 ------- .../core/config/ConfigurationScheduler.java | 15 +- .../apache/logging/log4j/core/jmx/Server.java | 7 +- .../logging/log4j/core/util/Log4jThread.java | 138 +++++++++---------- .../log4j/core/util/Log4jThreadFactory.java | 90 ++++++++++++ .../flume/appender/FlumePersistentManager.java | 30 +--- 8 files changed, 177 insertions(+), 160 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java index d0c9eea..f3565a1 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java @@ -28,6 +28,7 @@ import org.apache.logging.log4j.core.impl.LogEventFactory; import org.apache.logging.log4j.core.impl.MutableLogEvent; import org.apache.logging.log4j.core.impl.ReusableLogEventFactory; import org.apache.logging.log4j.core.jmx.RingBufferAdmin; +import org.apache.logging.log4j.core.util.Log4jThreadFactory; import org.apache.logging.log4j.message.ReusableMessage; import org.apache.logging.log4j.status.StatusLogger; @@ -176,7 +177,7 @@ public class AsyncLoggerConfigDisruptor implements AsyncLoggerConfigDelegate { } }; - private static final ThreadFactory THREAD_FACTORY = new DaemonThreadFactory("AsyncLoggerConfig-"); + private static final ThreadFactory THREAD_FACTORY = Log4jThreadFactory.createDaemonThreadFactory("AsyncLoggerConfig"); private int ringBufferSize; private AsyncQueueFullPolicy asyncQueueFullPolicy; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java index 1875ad2..259e22d 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerDisruptor.java @@ -22,6 +22,7 @@ import java.util.concurrent.Executors; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.jmx.RingBufferAdmin; +import org.apache.logging.log4j.core.util.Log4jThreadFactory; import org.apache.logging.log4j.status.StatusLogger; import com.lmax.disruptor.ExceptionHandler; @@ -81,7 +82,7 @@ class AsyncLoggerDisruptor { LOGGER.trace("[{}] AsyncLoggerDisruptor creating new disruptor for this context.", contextName); ringBufferSize = DisruptorUtil.calculateRingBufferSize("AsyncLogger.RingBufferSize"); final WaitStrategy waitStrategy = DisruptorUtil.createWaitStrategy("AsyncLogger.WaitStrategy"); - executor = Executors.newSingleThreadExecutor(new DaemonThreadFactory("AsyncLogger[" + contextName + "]-")); + executor = Executors.newSingleThreadExecutor(Log4jThreadFactory.createDaemonThreadFactory("AsyncLogger[" + contextName + "]")); backgroundThreadId = DisruptorUtil.getExecutorThreadId(executor); asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create(); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DaemonThreadFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DaemonThreadFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DaemonThreadFactory.java deleted file mode 100644 index 3267c61..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/DaemonThreadFactory.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.core.async; - -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicInteger; - -import org.apache.logging.log4j.core.util.Log4jThread; - -/** - * ThreadFactory that creates daemon threads for the async loggers. - */ -public class DaemonThreadFactory implements ThreadFactory { - - private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1); - private final ThreadGroup group; - private final String threadNamePrefix; - - public DaemonThreadFactory(final String threadNamePrefix) { - this.threadNamePrefix = threadNamePrefix; - final SecurityManager securityManager = System.getSecurityManager(); - group = securityManager != null ? securityManager.getThreadGroup() : Thread.currentThread().getThreadGroup(); - } - - @Override - public Thread newThread(final Runnable runnable) { - final Thread thread = new Log4jThread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0); - if (!thread.isDaemon()) { - thread.setDaemon(true); - } - if (thread.getPriority() != Thread.NORM_PRIORITY) { - thread.setPriority(Thread.NORM_PRIORITY); - } - return thread; - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java index 6a3c1ec..744ed24 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationScheduler.java @@ -16,12 +16,6 @@ */ package org.apache.logging.log4j.core.config; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.core.AbstractLifeCycle; -import org.apache.logging.log4j.core.async.DaemonThreadFactory; -import org.apache.logging.log4j.core.util.CronExpression; -import org.apache.logging.log4j.status.StatusLogger; - import java.util.Date; import java.util.concurrent.Callable; import java.util.concurrent.ScheduledExecutorService; @@ -29,6 +23,12 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.AbstractLifeCycle; +import org.apache.logging.log4j.core.util.CronExpression; +import org.apache.logging.log4j.core.util.Log4jThreadFactory; +import org.apache.logging.log4j.status.StatusLogger; + /** * */ @@ -48,7 +48,8 @@ public class ConfigurationScheduler extends AbstractLifeCycle { if (scheduledItems > 5) { scheduledItems = 5; } - executorService = new ScheduledThreadPoolExecutor(scheduledItems, new DaemonThreadFactory("Log4j2Scheduled-")); + executorService = new ScheduledThreadPoolExecutor(scheduledItems, + Log4jThreadFactory.createDaemonThreadFactory("Log4j2Scheduled")); } else { LOGGER.debug("No scheduled items"); } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java index fd14e11..10f343a 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java @@ -36,11 +36,11 @@ import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.appender.AsyncAppender; import org.apache.logging.log4j.core.async.AsyncLoggerConfig; import org.apache.logging.log4j.core.async.AsyncLoggerContext; -import org.apache.logging.log4j.core.async.DaemonThreadFactory; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.impl.Log4jContextFactory; import org.apache.logging.log4j.core.selector.ContextSelector; import org.apache.logging.log4j.core.util.Constants; +import org.apache.logging.log4j.core.util.Log4jThreadFactory; import org.apache.logging.log4j.spi.LoggerContextFactory; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.PropertiesUtil; @@ -59,7 +59,7 @@ public final class Server { public static final String DOMAIN = "org.apache.logging.log4j2"; private static final String PROPERTY_DISABLE_JMX = "log4j2.disable.jmx"; private static final String PROPERTY_ASYNC_NOTIF = "log4j2.jmx.notify.async"; - private static final String THREAD_NAME_PREFIX = "log4j2.jmx.notif-"; + private static final String THREAD_NAME_PREFIX = "log4j2.jmx.notif"; private static final StatusLogger LOGGER = StatusLogger.getLogger(); static final Executor executor = isJmxDisabled() ? null : createExecutor(); @@ -76,7 +76,8 @@ public final class Server { private static ExecutorService createExecutor() { final boolean defaultAsync = !Constants.IS_WEB_APP; final boolean async = PropertiesUtil.getProperties().getBooleanProperty(PROPERTY_ASYNC_NOTIF, defaultAsync); - return async ? Executors.newFixedThreadPool(1, new DaemonThreadFactory(THREAD_NAME_PREFIX)) : null; + return async ? Executors.newFixedThreadPool(1, Log4jThreadFactory.createDaemonThreadFactory(THREAD_NAME_PREFIX)) + : null; } /** http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThread.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThread.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThread.java index 1467a54..b92f181 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThread.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThread.java @@ -1,69 +1,69 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ - -package org.apache.logging.log4j.core.util; - -/** - * Prefixes thread names with {@code "Log4j2-"}. - */ -public class Log4jThread extends Thread { - - private static final String PREFIX = "Log4j2-"; - - private static int threadInitNumber; - - private static synchronized int nextThreadNum() { - return threadInitNumber++; - } - - private static String toThreadName(final Object name) { - return PREFIX + name; - } - - public Log4jThread() { - super(toThreadName(nextThreadNum())); - } - - public Log4jThread(final Runnable target) { - super(target, toThreadName(nextThreadNum())); - } - - public Log4jThread(final Runnable target, final String name) { - super(target, toThreadName(name)); - } - - public Log4jThread(final String name) { - super(toThreadName(name)); - } - - public Log4jThread(final ThreadGroup group, final Runnable target) { - super(group, target, toThreadName(nextThreadNum())); - } - - public Log4jThread(final ThreadGroup group, final Runnable target, final String name) { - super(group, target, toThreadName(name)); - } - - public Log4jThread(final ThreadGroup group, final Runnable target, final String name, final long stackSize) { - super(group, target, toThreadName(name), stackSize); - } - - public Log4jThread(final ThreadGroup group, final String name) { - super(group, toThreadName(name)); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package org.apache.logging.log4j.core.util; + +/** + * Prefixes thread names with {@code "Log4j2-"}. + */ +public class Log4jThread extends Thread { + + static final String PREFIX = "Log4j2-"; + + private static int threadInitNumber; + + private static synchronized int nextThreadNum() { + return threadInitNumber++; + } + + private static String toThreadName(final Object name) { + return PREFIX + name; + } + + public Log4jThread() { + super(toThreadName(nextThreadNum())); + } + + public Log4jThread(final Runnable target) { + super(target, toThreadName(nextThreadNum())); + } + + public Log4jThread(final Runnable target, final String name) { + super(target, toThreadName(name)); + } + + public Log4jThread(final String name) { + super(toThreadName(name)); + } + + public Log4jThread(final ThreadGroup group, final Runnable target) { + super(group, target, toThreadName(nextThreadNum())); + } + + public Log4jThread(final ThreadGroup group, final Runnable target, final String name) { + super(group, target, toThreadName(name)); + } + + public Log4jThread(final ThreadGroup group, final Runnable target, final String name, final long stackSize) { + super(group, target, toThreadName(name), stackSize); + } + + public Log4jThread(final ThreadGroup group, final String name) { + super(group, toThreadName(name)); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThreadFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThreadFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThreadFactory.java new file mode 100644 index 0000000..ed07056 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Log4jThreadFactory.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ + +package org.apache.logging.log4j.core.util; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.logging.log4j.util.Strings; + +/** + * Creates {@link Log4jThreads}. + * + * @since 2.7 + */ +public class Log4jThreadFactory implements ThreadFactory { + + private static final String PREFIX = Log4jThread.PREFIX + "TF-"; + + /** + * Creates a new daemon thread factory. + * + * @param threadNamePrefix + * The thread name prefix. + * @return a new daemon thread factory. + */ + public static Log4jThreadFactory createDaemonThreadFactory(final String threadNamePrefix) { + return new Log4jThreadFactory(threadNamePrefix, true, Thread.NORM_PRIORITY); + } + + private static final AtomicInteger FACTORY_NUMBER = new AtomicInteger(1); + private static final AtomicInteger THREAD_NUMBER = new AtomicInteger(1); + private final boolean daemon; + private final ThreadGroup group; + private final int priority; + private final String threadNamePrefix; + + /** + * Constructs a thread factory with default settings. + */ + public Log4jThreadFactory() { + this("thread", false, Thread.NORM_PRIORITY); + } + + /** + * Constructs an initialized thread factory. + * + * @param threadNamePrefix + * The thread name prefix. + * @param daemon + * Whether to create daemon threads. + * @param priority + * The thread priority. + */ + public Log4jThreadFactory(final String threadNamePrefix, final boolean daemon, final int priority) { + this.threadNamePrefix = PREFIX + FACTORY_NUMBER.getAndIncrement() + "-" + threadNamePrefix + "-"; + this.daemon = daemon; + this.priority = priority; + final SecurityManager securityManager = System.getSecurityManager(); + this.group = securityManager != null ? securityManager.getThreadGroup() + : Thread.currentThread().getThreadGroup(); + } + + @Override + public Thread newThread(final Runnable runnable) { + final Thread thread = new Log4jThread(group, runnable, threadNamePrefix + THREAD_NUMBER.getAndIncrement(), 0); + if (thread.isDaemon() != daemon) { + thread.setDaemon(daemon); + } + if (thread.getPriority() != priority) { + thread.setPriority(priority); + } + return thread; + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9afe7ea8/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java ---------------------------------------------------------------------- diff --git a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java index daf6fdd..a76065a 100644 --- a/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java +++ b/log4j-flume-ng/src/main/java/org/apache/logging/log4j/flume/appender/FlumePersistentManager.java @@ -46,6 +46,7 @@ import org.apache.logging.log4j.core.config.plugins.util.PluginManager; import org.apache.logging.log4j.core.config.plugins.util.PluginType; import org.apache.logging.log4j.core.util.FileUtils; import org.apache.logging.log4j.core.util.Log4jThread; +import org.apache.logging.log4j.core.util.Log4jThreadFactory; import org.apache.logging.log4j.core.util.SecretKeyProvider; import org.apache.logging.log4j.util.Strings; @@ -124,7 +125,7 @@ public class FlumePersistentManager extends FlumeAvroManager { lockTimeoutRetryCount); this.worker.start(); this.secretKey = secretKey; - this.threadPool = Executors.newCachedThreadPool(new DaemonThreadFactory()); + this.threadPool = Executors.newCachedThreadPool(Log4jThreadFactory.createDaemonThreadFactory("Flume")); this.lockTimeoutRetryCount = lockTimeoutRetryCount; } @@ -824,33 +825,6 @@ public class FlumePersistentManager extends FlumeAvroManager { } /** - * Factory that creates Daemon threads that can be properly shut down. - */ - private static class DaemonThreadFactory implements ThreadFactory { - private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1); - private final ThreadGroup group; - private final AtomicInteger threadNumber = new AtomicInteger(1); - private final String namePrefix; - - public DaemonThreadFactory() { - final SecurityManager securityManager = System.getSecurityManager(); - group = securityManager != null ? securityManager.getThreadGroup() : - Thread.currentThread().getThreadGroup(); - namePrefix = "DaemonPool-" + POOL_NUMBER.getAndIncrement() + "-thread-"; - } - - @Override - public Thread newThread(final Runnable r) { - final Thread thread = new Log4jThread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); - thread.setDaemon(true); - if (thread.getPriority() != Thread.NORM_PRIORITY) { - thread.setPriority(Thread.NORM_PRIORITY); - } - return thread; - } - } - - /** * An internal class. */ private static class Gate {