Skip to content

Commit 01ed4cc

Browse files
committed
Remove JMX support
1 parent 4772a2b commit 01ed4cc

25 files changed

+41
-2292
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncThreadContextTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
2727
import java.util.concurrent.TimeUnit;
28+
import java.util.function.LongSupplier;
2829
import org.apache.logging.log4j.ThreadContext;
2930
import org.apache.logging.log4j.core.Logger;
3031
import org.apache.logging.log4j.core.LoggerContext;
3132
import org.apache.logging.log4j.core.ThreadContextTestAccess;
3233
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
3334
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
34-
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
3535
import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
3636
import org.apache.logging.log4j.core.selector.ContextSelector;
3737
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
@@ -157,19 +157,22 @@ private static void runTest(
157157

158158
final Logger log = context.getLogger("com.foo.Bar");
159159
final String loggerContextName = context.getClass().getSimpleName();
160-
RingBufferAdmin ring;
160+
final LongSupplier remainingCapacity;
161161
if (context instanceof AsyncLoggerContext) {
162-
ring = ((AsyncLoggerContext) context).createRingBufferAdmin();
162+
remainingCapacity =
163+
((AsyncLoggerContext) context).getAsyncLoggerDisruptor().getRingBuffer()::remainingCapacity;
163164
} else {
164-
ring = ((AsyncLoggerConfig) log.get()).createRingBufferAdmin("");
165+
remainingCapacity =
166+
((AsyncLoggerConfigDisruptor) ((AsyncLoggerConfig) log.get()).getAsyncLoggerConfigDelegate())
167+
.getRingBuffer()::remainingCapacity;
165168
}
166169

167170
for (int i = 0; i < LINE_COUNT; i++) {
168171
// buffer may be full
169172
if (i >= 128) {
170173
waitAtMost(500, TimeUnit.MILLISECONDS)
171174
.pollDelay(10, TimeUnit.MILLISECONDS)
172-
.until(() -> ring.getRemainingCapacity() > 0);
175+
.until(() -> remainingCapacity.getAsLong() > 0);
173176
}
174177
if ((i & 1) == 1) {
175178
ThreadContext.put("count", String.valueOf(i));

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/QueueFullAbstractTest.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
import static java.util.concurrent.TimeUnit.SECONDS;
2020
import static org.assertj.core.api.Assertions.assertThat;
2121

22-
import com.lmax.disruptor.dsl.Disruptor;
22+
import com.lmax.disruptor.RingBuffer;
2323
import java.lang.reflect.Field;
2424
import java.util.ArrayList;
2525
import java.util.Collection;
2626
import java.util.Collections;
2727
import java.util.List;
28+
import java.util.Optional;
2829
import java.util.Stack;
2930
import java.util.concurrent.CountDownLatch;
3031
import java.util.stream.Collectors;
@@ -36,7 +37,6 @@
3637
import org.apache.logging.log4j.core.appender.AsyncAppender;
3738
import org.apache.logging.log4j.core.config.Configuration;
3839
import org.apache.logging.log4j.core.config.LoggerConfig;
39-
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
4040
import org.apache.logging.log4j.core.util.Constants;
4141
import org.apache.logging.log4j.status.StatusData;
4242
import org.apache.logging.log4j.status.StatusLogger;
@@ -234,29 +234,19 @@ static List<String> getMessages(final List<LogEvent> logEvents) {
234234
}
235235

236236
static long asyncRemainingCapacity(final Logger logger) {
237-
if (logger instanceof AsyncLogger) {
238-
try {
239-
final Field f = field(AsyncLogger.class, "loggerDisruptor");
240-
return ((AsyncLoggerDisruptor) f.get(logger))
241-
.getDisruptor()
242-
.getRingBuffer()
243-
.remainingCapacity();
244-
} catch (final Exception ex) {
245-
throw new RuntimeException(ex);
246-
}
237+
if (logger instanceof AsyncLogger asyncLogger) {
238+
return Optional.ofNullable(asyncLogger.getAsyncLoggerDisruptor())
239+
.map(AsyncLoggerDisruptor::getRingBuffer)
240+
.map(RingBuffer::remainingCapacity)
241+
.orElse(0L);
247242
} else {
248243
final LoggerConfig loggerConfig = ((org.apache.logging.log4j.core.Logger) logger).get();
249-
if (loggerConfig instanceof AsyncLoggerConfig) {
250-
try {
251-
final Object delegate =
252-
field(AsyncLoggerConfig.class, "delegate").get(loggerConfig);
253-
return ((Disruptor) field(AsyncLoggerConfigDisruptor.class, "disruptor")
254-
.get(delegate))
255-
.getRingBuffer()
256-
.remainingCapacity();
257-
} catch (final Exception ex) {
258-
throw new RuntimeException(ex);
259-
}
244+
if (loggerConfig instanceof AsyncLoggerConfig asyncLoggerConfig) {
245+
return Optional.ofNullable(
246+
(AsyncLoggerConfigDisruptor) asyncLoggerConfig.getAsyncLoggerConfigDelegate())
247+
.map(AsyncLoggerConfigDisruptor::getRingBuffer)
248+
.map(RingBuffer::remainingCapacity)
249+
.orElse(0L);
260250
} else {
261251
final Appender async = loggerConfig.getAppenders().get("async");
262252
if (async instanceof AsyncAppender) {
@@ -287,24 +277,25 @@ protected static void assertAsyncAppender(final LoggerContext ctx) {
287277

288278
protected static void assertAsyncLogger(final LoggerContext ctx, final int expectedBufferSize) {
289279
assertThat(ctx).isInstanceOf(AsyncLoggerContext.class);
290-
final RingBufferAdmin ringBufferAdmin = ((AsyncLoggerContext) ctx).createRingBufferAdmin();
291-
assertThat(ringBufferAdmin.getRemainingCapacity()).isEqualTo(expectedBufferSize);
280+
assertThat(((AsyncLoggerContext) ctx)
281+
.getAsyncLoggerDisruptor()
282+
.getRingBuffer()
283+
.getBufferSize())
284+
.isEqualTo(expectedBufferSize);
292285

293286
final Configuration config = ctx.getConfiguration();
294287
assertThat(config).isNotNull();
295288
assertThat(config.getRootLogger()).isNotInstanceOf(AsyncLoggerConfig.class);
296289
}
297290

298-
protected static void assertAsyncLoggerConfig(final LoggerContext ctx, final int expectedBufferSize)
299-
throws ReflectiveOperationException {
291+
protected static void assertAsyncLoggerConfig(final LoggerContext ctx, final int expectedBufferSize) {
300292
assertThat(ctx).isNotInstanceOf(AsyncLoggerContext.class);
301293

302294
final Configuration config = ctx.getConfiguration();
303295
assertThat(config).isNotNull();
304296
assertThat(config.getRootLogger()).isInstanceOf(AsyncLoggerConfig.class);
305297
final AsyncLoggerConfigDisruptor disruptor = (AsyncLoggerConfigDisruptor) config.getAsyncLoggerConfigDelegate();
306-
final Field sizeField = field(AsyncLoggerConfigDisruptor.class, "ringBufferSize");
307-
assertThat(sizeField.get(disruptor)).isEqualTo(expectedBufferSize);
298+
assertThat(disruptor.getRingBuffer().getBufferSize()).isEqualTo(expectedBufferSize);
308299
}
309300

310301
protected static void assertFormatMessagesInBackground() {

log4j-core-test/src/test/java/org/apache/logging/log4j/core/jmx/ServerTest.java

Lines changed: 0 additions & 118 deletions
This file was deleted.

log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.apache.logging.log4j.core.config.NullConfiguration;
4343
import org.apache.logging.log4j.core.config.Reconfigurable;
4444
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
45-
import org.apache.logging.log4j.core.jmx.Server;
4645
import org.apache.logging.log4j.core.util.Cancellable;
4746
import org.apache.logging.log4j.core.util.ExecutorServices;
4847
import org.apache.logging.log4j.core.util.NetUtils;
@@ -436,13 +435,7 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
436435
}
437436

438437
this.setStopping();
439-
String name = getName();
440-
try {
441-
Server.unregisterLoggerContext(name); // LOG4J2-406, LOG4J2-500
442-
} catch (final LinkageError | Exception e) {
443-
// LOG4J2-1506 Hello Android, GAE
444-
LOGGER.error("Unable to unregister MBeans", e);
445-
}
438+
446439
if (shutdownCallback != null) {
447440
shutdownCallback.cancel();
448441
shutdownCallback = null;
@@ -736,13 +729,6 @@ public Configuration setConfiguration(final Configuration config) {
736729
notifyConfigurationStopped(prev);
737730
}
738731

739-
try {
740-
Server.reregisterMBeansAfterReconfigure();
741-
} catch (final LinkageError | Exception e) {
742-
// LOG4J2-716: Android has no java.lang.management
743-
LOGGER.error("Could not reconfigure JMX", e);
744-
}
745-
746732
return prev;
747733
} finally {
748734
configLock.unlock();

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfig.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.logging.log4j.core.config.LoggerConfig;
2828
import org.apache.logging.log4j.core.config.Property;
2929
import org.apache.logging.log4j.core.impl.LogEventFactory;
30-
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
3130
import org.apache.logging.log4j.plugins.Configurable;
3231
import org.apache.logging.log4j.plugins.Plugin;
3332
import org.apache.logging.log4j.plugins.PluginFactory;
@@ -210,17 +209,6 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
210209
return true;
211210
}
212211

213-
/**
214-
* Creates and returns a new {@code RingBufferAdmin} that instruments the
215-
* ringbuffer of this {@code AsyncLoggerConfig}.
216-
*
217-
* @param contextName name of the {@code LoggerContext}
218-
* @return a new {@code RingBufferAdmin} that instruments the ringbuffer
219-
*/
220-
public RingBufferAdmin createRingBufferAdmin(final String contextName) {
221-
return delegate.createRingBufferAdmin(contextName, getName());
222-
}
223-
224212
// Note: for asynchronous loggers, includeLocation default is FALSE
225213
protected static boolean includeLocation(final String includeLocationConfigValue) {
226214
return Boolean.parseBoolean(includeLocationConfigValue);

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDelegate.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,13 @@
1919
import org.apache.logging.log4j.Level;
2020
import org.apache.logging.log4j.core.LogEvent;
2121
import org.apache.logging.log4j.core.impl.LogEventFactory;
22-
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
2322

2423
/**
2524
* Encapsulates the mechanism used to log asynchronously. There is one delegate per configuration, which is shared by
2625
* all AsyncLoggerConfig objects in the configuration.
2726
*/
2827
public interface AsyncLoggerConfigDelegate {
2928

30-
/**
31-
* Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of this
32-
* {@code AsyncLoggerConfig}.
33-
*
34-
* @param contextName name of the {@code LoggerContext}
35-
* @param loggerConfigName name of the logger config
36-
* @return the RingBufferAdmin that instruments the ringbuffer
37-
*/
38-
RingBufferAdmin createRingBufferAdmin(final String contextName, final String loggerConfigName);
39-
4029
/**
4130
* Returns the {@code EventRoute} for the event with the specified level.
4231
*

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigDisruptor.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.apache.logging.log4j.core.impl.LogEventFactory;
4040
import org.apache.logging.log4j.core.impl.MutableLogEvent;
4141
import org.apache.logging.log4j.core.impl.ReusableLogEventFactory;
42-
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
4342
import org.apache.logging.log4j.core.util.Log4jThread;
4443
import org.apache.logging.log4j.core.util.Log4jThreadFactory;
4544
import org.apache.logging.log4j.core.util.Throwables;
@@ -426,14 +425,8 @@ private LogEvent ensureImmutable(final LogEvent event) {
426425
return result;
427426
}
428427

429-
/*
430-
* (non-Javadoc)
431-
*
432-
* @see org.apache.logging.log4j.core.async.AsyncLoggerConfigDelegate#createRingBufferAdmin(java.lang.String,
433-
* java.lang.String)
434-
*/
435-
@Override
436-
public RingBufferAdmin createRingBufferAdmin(final String contextName, final String loggerConfigName) {
437-
return RingBufferAdmin.forAsyncLoggerConfig(disruptor.getRingBuffer(), contextName, loggerConfigName);
428+
// package-protected for tests
429+
RingBuffer<Log4jEventWrapper> getRingBuffer() {
430+
return disruptor.getRingBuffer();
438431
}
439432
}

log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLoggerContext.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.logging.log4j.core.LoggerContext;
2323
import org.apache.logging.log4j.core.config.Configuration;
2424
import org.apache.logging.log4j.core.config.DefaultConfiguration;
25-
import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
2625
import org.apache.logging.log4j.message.MessageFactory;
2726
import org.apache.logging.log4j.plugins.di.ConfigurableInstanceFactory;
2827
import org.apache.logging.log4j.status.StatusLogger;
@@ -131,13 +130,8 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
131130
return true;
132131
}
133132

134-
/**
135-
* Creates and returns a new {@code RingBufferAdmin} that instruments the ringbuffer of the {@code AsyncLogger}
136-
* objects in this {@code LoggerContext}.
137-
*
138-
* @return a new {@code RingBufferAdmin} that instruments the ringbuffer
139-
*/
140-
public RingBufferAdmin createRingBufferAdmin() {
141-
return loggerDisruptor.createRingBufferAdmin(getName());
133+
// package-protected for tests
134+
AsyncLoggerDisruptor getAsyncLoggerDisruptor() {
135+
return loggerDisruptor;
142136
}
143137
}

0 commit comments

Comments
 (0)