mirror of
https://github.com/gmbrax/MediaManager-core.git
synced 2026-11-09 14:00:17 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aa0887fa9 | ||
|
|
281618a725 |
+103
@@ -0,0 +1,103 @@
|
||||
# Refactoring History—MediaManager Core
|
||||
|
||||
This document records the refactorings applied during development.
|
||||
|
||||
## Refactoring #1: Connection Pool Implementation
|
||||
|
||||
**Date**: 2025-11-13
|
||||
**Commits**: ```22476abb27d95e88ae0439d97ef97467b6a4f985```
|
||||
|
||||
### Problem Identified
|
||||
- Database connections were created multiple times
|
||||
- No centralized control
|
||||
- Possible resource leaks
|
||||
|
||||
### Technique Applied
|
||||
- **Pattern**: Singleton Pattern
|
||||
- **Refactoring**: Extract Class + Introduce Singleton
|
||||
|
||||
### Solution
|
||||
Created `DatabaseManager` class that:
|
||||
- Maintains single instance (Singleton)
|
||||
- Manages connection pool
|
||||
- Provides centralized `getConnection()` method
|
||||
|
||||
### Result
|
||||
- ✅ Single reusable connection
|
||||
- ✅ Code centralized in one class
|
||||
- ✅ Thread-safe
|
||||
- ✅ Easier to test
|
||||
|
||||
### Extracted Component
|
||||
`DatabaseManager` - available in `mediamanager-components`
|
||||
|
||||
---
|
||||
|
||||
## Refactoring #2: Thread Pool for IPC
|
||||
|
||||
**Date**: 2025-11-14
|
||||
**Commits**: ```f1071dca03da33f7355a6927497509b5aedaa4a2```
|
||||
|
||||
### Problem Identified
|
||||
- New thread created for each IPC client
|
||||
- Doesn't scale well
|
||||
- Thread creation overhead
|
||||
|
||||
### Technique Applied
|
||||
- **Pattern**: Thread Pool Pattern
|
||||
- **Refactoring**: Replace Thread Creation with Thread Pool
|
||||
|
||||
### Solution
|
||||
Implemented `ExecutorService` with:
|
||||
- Fixed thread pool
|
||||
- Thread reuse
|
||||
- Ordered shutdown
|
||||
|
||||
### Result
|
||||
- ✅ Better performance
|
||||
- ✅ Controlled resource usage
|
||||
- ✅ Scalability
|
||||
|
||||
### Extracted Component
|
||||
`IPCServer` - available in `mediamanager-components`
|
||||
|
||||
---
|
||||
|
||||
## Refactoring #3: Graceful Shutdown
|
||||
|
||||
**Date**: 2025-11-13
|
||||
**Commits**: ```65e14930076db71a312feae373d6c5daee3c4d29```,```b7ff9d1e16c85bf986a770676827bee390f0d53c```
|
||||
|
||||
### Problem Identified
|
||||
- Resources weren't released on exit
|
||||
- Database connections remained open
|
||||
- Thread pool didn't terminate correctly
|
||||
|
||||
### Technique Applied
|
||||
- **Pattern**: Shutdown Hook Pattern
|
||||
- **Refactoring**: Introduce Shutdown Protocol
|
||||
|
||||
### Solution
|
||||
Implemented shutdown hooks that:
|
||||
- Close database connections
|
||||
- Terminate thread pools
|
||||
- Release sockets
|
||||
- In specific order with timeouts
|
||||
|
||||
### Result
|
||||
- ✅ Ordered cleanup
|
||||
- ✅ No resource leaks
|
||||
- ✅ Shutdown logging
|
||||
- ✅ Timeout to prevent hanging
|
||||
|
||||
### Extracted Component
|
||||
Logic incorporated in `DatabaseManager` and `IPCServer`
|
||||
|
||||
---
|
||||
|
||||
## Applied Principles
|
||||
|
||||
- **DRY** (Don't Repeat Yourself): Elimination of duplicated code
|
||||
- **SOLID**: Especially Single Responsibility Principle
|
||||
- **Design Patterns**: Singleton, Thread Pool, Shutdown Hook
|
||||
- **Clean Code**: Clear names, small methods, well-defined responsibilities
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.mediamanager;
|
||||
|
||||
import com.mediamanager.config.DatabaseManager;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -10,6 +11,7 @@ import java.util.Properties;
|
||||
public class MediaManagerApplication {
|
||||
private static final Logger logger = LogManager.getLogger(MediaManagerApplication.class);
|
||||
private static Properties config;
|
||||
private static DatabaseManager databaseManager;
|
||||
|
||||
public static void main(String[] args) {
|
||||
logger.info("Starting MediaManager Core Application...");
|
||||
@@ -18,7 +20,9 @@ public class MediaManagerApplication {
|
||||
// Load configuration
|
||||
loadConfiguration();
|
||||
|
||||
// TODO: Initialize database connection
|
||||
// Initialize database connection
|
||||
databaseManager = DatabaseManager.getInstance(config);
|
||||
|
||||
// TODO: Initialize IPC server with named pipes
|
||||
// TODO: Start application services
|
||||
|
||||
@@ -28,7 +32,9 @@ public class MediaManagerApplication {
|
||||
// Keep application running
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
logger.info("Shutting down MediaManager Core...");
|
||||
// TODO: Cleanup resources
|
||||
if (databaseManager != null) {
|
||||
databaseManager.shutdown();
|
||||
}
|
||||
}));
|
||||
|
||||
} catch (Exception e) {
|
||||
@@ -52,4 +58,8 @@ public class MediaManagerApplication {
|
||||
public static Properties getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public static DatabaseManager getDatabaseManager() {
|
||||
return databaseManager;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.mediamanager.config;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DatabaseManager {
|
||||
private static final Logger logger = LogManager.getLogger(DatabaseManager.class);
|
||||
private static DatabaseManager instance;
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
private DatabaseManager(Properties config) {
|
||||
initializeEntityManagerFactory(config);
|
||||
}
|
||||
|
||||
public static synchronized DatabaseManager getInstance(Properties config) {
|
||||
if (instance == null) {
|
||||
instance = new DatabaseManager(config);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void initializeEntityManagerFactory(Properties config) {
|
||||
try {
|
||||
logger.info("Initializing database connection...");
|
||||
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
properties.put("jakarta.persistence.jdbc.url", config.getProperty("db.url"));
|
||||
properties.put("jakarta.persistence.jdbc.user", config.getProperty("db.username"));
|
||||
properties.put("jakarta.persistence.jdbc.password", config.getProperty("db.password"));
|
||||
properties.put("jakarta.persistence.jdbc.driver", config.getProperty("db.driver"));
|
||||
|
||||
properties.put("hibernate.hikari.maximumPoolSize", config.getProperty("db.pool.maximum-pool-size"));
|
||||
properties.put("hibernate.hikari.minimumIdle", config.getProperty("db.pool.minimum-idle"));
|
||||
properties.put("hibernate.hikari.connectionTimeout", config.getProperty("db.pool.connection-timeout"));
|
||||
properties.put("hibernate.hikari.idleTimeout", config.getProperty("db.pool.idle-timeout"));
|
||||
properties.put("hibernate.hikari.maxLifetime", config.getProperty("db.pool.max-lifetime"));
|
||||
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("MediaManagerPU", properties);
|
||||
logger.info("Database connection initialized successfully");
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to initialize database connection", e);
|
||||
throw new RuntimeException("Failed to initialize database connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
public EntityManager createEntityManager() {
|
||||
if (entityManagerFactory == null) {
|
||||
throw new IllegalStateException("EntityManagerFactory is not initialized");
|
||||
}
|
||||
return entityManagerFactory.createEntityManager();
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
if (entityManagerFactory != null && entityManagerFactory.isOpen()) {
|
||||
logger.info("Closing database connection...");
|
||||
entityManagerFactory.close();
|
||||
logger.info("Database connection closed successfully");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInitialized() {
|
||||
return entityManagerFactory != null && entityManagerFactory.isOpen();
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@
|
||||
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
|
||||
|
||||
<!-- Add entity classes here as they are created -->
|
||||
<!-- <class>com.mediamanager.model.Media</class> -->
|
||||
<class>com.mediamanager.model.Media</class>
|
||||
|
||||
<properties>
|
||||
<!-- Database connection properties will be set programmatically -->
|
||||
|
||||
Reference in New Issue
Block a user