[framework] TxCore → common-framework (ACM-FW-001)
This commit is contained in:
parent
f10a82ec16
commit
63f85f0f81
1 changed files with 69 additions and 0 deletions
|
|
@ -0,0 +1,69 @@
|
|||
package com.klaro.acquirecore.framework;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
/**
|
||||
* Service registry for transaction services. Migrated from txcore.c tx_service_registry.
|
||||
* Replaces TX_SERVICE/registry pattern with @Service dispatch.
|
||||
*/
|
||||
@Service
|
||||
public class TxServiceRegistry {
|
||||
|
||||
/** Functional interface for transaction service handlers. */
|
||||
@FunctionalInterface
|
||||
public interface TxServiceHandler {
|
||||
TxResult execute(TxContext context);
|
||||
}
|
||||
|
||||
private final Map<String, TxServiceHandler> services = new ConcurrentHashMap<>();
|
||||
private final Map<String, TxConfig> serviceConfigs = new ConcurrentHashMap<>();
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {}
|
||||
|
||||
public void registerService(String serviceName, TxServiceHandler handler) {
|
||||
services.put(serviceName, handler);
|
||||
}
|
||||
|
||||
public void registerService(String serviceName, TxServiceHandler handler, TxConfig config) {
|
||||
services.put(serviceName, handler);
|
||||
serviceConfigs.put(serviceName, config);
|
||||
}
|
||||
|
||||
public void unregisterService(String serviceName) {
|
||||
services.remove(serviceName);
|
||||
serviceConfigs.remove(serviceName);
|
||||
}
|
||||
|
||||
public TxServiceHandler getService(String serviceName) {
|
||||
return services.get(serviceName);
|
||||
}
|
||||
|
||||
public boolean hasService(String serviceName) {
|
||||
return services.containsKey(serviceName);
|
||||
}
|
||||
|
||||
public TxConfig getConfig(String serviceName) {
|
||||
return serviceConfigs.getOrDefault(serviceName, new TxConfig());
|
||||
}
|
||||
|
||||
public TxResult execute(String serviceName, TxContext context) {
|
||||
TxServiceHandler handler = services.get(serviceName);
|
||||
if (handler == null) {
|
||||
return TxResult.fail(context.getTxId(), "Service not found: " + serviceName);
|
||||
}
|
||||
return handler.execute(context);
|
||||
}
|
||||
|
||||
public void forEachService(BiConsumer<String, TxServiceHandler> action) {
|
||||
services.forEach(action);
|
||||
}
|
||||
|
||||
public int getServiceCount() {
|
||||
return services.size();
|
||||
}
|
||||
}
|
||||
Reference in a new issue