Reputation: 1452
i am trying to use JMSAppenders in Logback. I tried out a simple app to do that and have successfully sent logs into queues in ActiveMQ.
package test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoggerTest {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger("test");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error", new Exception(""));
}
}
Everything runs fine but this application wont terminate even after all the statements in main have executed.
After some poking around I noticed that after the main thread ends there were two non daemon threads active ActiveMQ Transport and DestroyJavaVM I believe this DestoryJavaVM thread came into being after main thread exited.
I think this has something to do with sessions/connections not closing. If I terminate the broker, the app also exits.
There is a stop()
method in JMSQueueAppender that closes sessions but it seems its never called, apparently stop
method is not called for other appenders as well -- i tried it on RollingFileAppender.
I tried the same setup with HornetQ and it works fine but not with activeMQ
I am using slf4j 1.6.4, logback 1.0.0 and activemq 5.5.0
Heres the logback configuration
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="Queue" class="ch.qos.logback.classic.net.JMSQueueAppender">
<InitialContextFactoryName>
org.apache.activemq.jndi.ActiveMQInitialContextFactory
</InitialContextFactoryName>
<ProviderURL>tcp://localhost:61616</ProviderURL>
<QueueConnectionFactoryBindingName>
ConnectionFactory
</QueueConnectionFactoryBindingName>
<QueueBindingName>dynamicQueues/MyQueue</QueueBindingName>
</appender>
<root level="debug">
<appender-ref ref="Queue" />
<appender-ref ref="STDOUT" />
</root>
<logger name="org.apache.activemq" additivity="false" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
and pom
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<groupId>LoggerTest</groupId>
<artifactId>LoggerTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
<!-- <scope>runtime</scope> -->
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.5.0</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
Can anybody point out whats wrong with this setup. Thanks in advance
Upvotes: 4
Views: 2479
Reputation: 429
Version 1.1.10 onwards, logback takes care of stopping the current logback-classic context (and all the appenders) when the web-app is stopped or reloaded. Try updating your logback version and check once.
Here's the updated doc: https://logback.qos.ch/manual/configuration.html#webShutdownHook
Upvotes: 0
Reputation: 7111
The ActiveMQ transport stays alive until it gets shut down explicitly, which LogBack obviously does not automatically.
One can trigger the shut down via:
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
// Check for logback implementation of slf4j
if (loggerFactory instanceof LoggerContext) {
LoggerContext context = (LoggerContext) loggerFactory;
context.stop();
}
Taken from here: Do I need to flush events when shutting down using logback?
Verified with ActiveMQ 5.7.0, LogBack 1.1.2
Upvotes: 1