Improve logging, shutdown behavior, and build configuration:
- Enabled `immediateFlush` for log appenders and disabled `shutdownHook` in Log4j2 configuration. - Enhanced shutdown hook with graceful Log4j2 termination and extended delay for log flushing. - Improved database connection closure logging in `DatabaseManager`. - Integrated Maven Shade Plugin to create executable JARs with all dependencies.
This commit is contained in:
parent
b0c45bf657
commit
b7ff9d1e16
22
pom.xml
22
pom.xml
|
|
@ -96,6 +96,28 @@
|
|||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.2.5</version>
|
||||
</plugin>
|
||||
|
||||
<!-- Maven Shade Plugin - Creates executable JAR with all dependencies -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>com.mediamanager.MediaManagerApplication</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
|
|||
|
|
@ -32,19 +32,28 @@ public class MediaManagerApplication {
|
|||
|
||||
// Keep application running
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
|
||||
logger.info("Shutting down MediaManager Core...");
|
||||
|
||||
|
||||
if (databaseManager != null) {
|
||||
databaseManager.close();
|
||||
}
|
||||
|
||||
// TODO: Cleanup resources
|
||||
|
||||
logger.info("MediaManager Core shutdown successfully");
|
||||
logger.info("Goodbye!");
|
||||
|
||||
|
||||
// Give Log4j2 time to write all pending messages before shutting down
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
// Now shutdown Log4j2
|
||||
org.apache.logging.log4j.LogManager.shutdown();
|
||||
}));
|
||||
logger.info("Application is running");
|
||||
logger.info("Press Ctrl+C to exit");
|
||||
|
|
|
|||
|
|
@ -88,11 +88,14 @@ public class DatabaseManager {
|
|||
public void close() {
|
||||
if (connection != null) {
|
||||
try {
|
||||
logger.info("Closing database connection...");
|
||||
connection.close();
|
||||
logger.info("Database connection closed successfully");
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed to close database connection", e);
|
||||
}
|
||||
logger.error("Error closing database connection: {}", e.getMessage());
|
||||
}
|
||||
} else {
|
||||
logger.debug("No database connection to close");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Configuration status="WARN" shutdownHook="disable">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<Console name="Console" target="SYSTEM_OUT" immediateFlush="true">
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
|
||||
<RollingFile name="FileAppender" fileName="logs/mediamanager.log"
|
||||
filePattern="logs/mediamanager-%d{yyyy-MM-dd}-%i.log">
|
||||
filePattern="logs/mediamanager-%d{yyyy-MM-dd}-%i.log"
|
||||
immediateFlush="true">
|
||||
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1"/>
|
||||
|
|
|
|||
Loading…
Reference in New Issue