为什么JMSAppender使用同步发送?我们可以改变吗?

时间:2016-12-14 09:57:59

标签: java jms log4j2

我正在尝试使用JMSAppender log4j2将日志发送到ActivMQ。

我做了以下conf

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="5">
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{HH:mm:ss.SSS} %highlight{[%-5p] %c{1.} - %m} %style{(%t)}{Cyan}%n" />
        </Console>
        <RollingFile name="fish" fileName="E:/fish/log/fish.server/fish.log"
            filePattern="/log/fish.server/fish-%-d{yyyy-MM-dd-HH}.log">
            <PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>

         <JMS name="jmsQueue" destinationBindingName="logQueue"
            factoryName="org.apache.activemq.jndi.ActiveMQInitialContextFactory"
            factoryBindingName="ConnectionFactory" providerURL="tcp://xxxxxx:61616">
            <PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n" />
        </JMS>

    </Appenders>
    <Loggers>

        <Logger name="jmsLogger" level="warn">
            <AppenderRef ref="jmsQueue" />
        </Logger>

        <Root level="warn">
            <!-- <AppenderRef ref="STDOUT" /> -->
            <AppenderRef ref="fish" />
        </Root>
    </Loggers>
</Configuration>

之后,我有了32个streamExecutor线程来执行一些人员,然后使用jmsLogger.warn(xxxx)。

我为我的应用做了一个线程转储,我注意到以下阻塞人员:

"Log4j2-AsyncLogger[AsyncContext@14dad5dc]1" - Thread t@27
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <324492ea> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.ArrayBlockingQueue.take(ArrayBlockingQueue.java:403)
    at org.apache.activemq.transport.FutureResponse.getResult(FutureResponse.java:40)
    at org.apache.activemq.transport.ResponseCorrelator.request(ResponseCorrelator.java:87)
    at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1366)
    at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1305)
    at org.apache.activemq.ActiveMQSession.send(ActiveMQSession.java:1795)
    - locked <1af6596a> (a java.lang.Object)
    at org.apache.activemq.ActiveMQMessageProducer.send(ActiveMQMessageProducer.java:277)
    at org.apache.activemq.ActiveMQMessageProducer.send(ActiveMQMessageProducer.java:212)
    at org.apache.activemq.ActiveMQMessageProducerSupport.send(ActiveMQMessageProducerSupport.java:241)
    at org.apache.logging.log4j.core.appender.mom.JmsAppender.append(JmsAppender.java:65)
    at org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender(AppenderControl.java:155)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender0(AppenderControl.java:128)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppenderPreventRecursion(AppenderControl.java:119)
    at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:84)
    at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:390)
    at org.apache.logging.log4j.core.config.LoggerConfig.processLogEvent(LoggerConfig.java:375)
    at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:359)
    at org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStrategy.log(AwaitCompletionReliabilityStrategy.java:79)
    at org.apache.logging.log4j.core.async.AsyncLogger.actualAsyncLog(AsyncLogger.java:310)
    at org.apache.logging.log4j.core.async.RingBufferLogEvent.execute(RingBufferLogEvent.java:149)
    at org.apache.logging.log4j.core.async.RingBufferLogEventHandler.onEvent(RingBufferLogEventHandler.java:45)
    at org.apache.logging.log4j.core.async.RingBufferLogEventHandler.onEvent(RingBufferLogEventHandler.java:29)
    at com.lmax.disruptor.BatchEventProcessor.run(BatchEventProcessor.java:129)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

所以问题是,为什么发送模式是同步的?org.apache.activemq.ActiveMQConnection.syncSendPacket。这会导致一些阻塞问题并降低我的性能。

很多:)

1 个答案:

答案 0 :(得分:1)

已解决,我只是加入tcp:// xxxxxx:61616?jms.useAsyc = true