From 43cbbab5cc6bf21919c2acbc10490a12d81bf6f9 Mon Sep 17 00:00:00 2001 From: forge-bot Date: Sat, 18 Jul 2026 10:06:08 +0000 Subject: [PATCH] =?UTF-8?q?[framework]=20TxCore=20=E2=86=92=20common-frame?= =?UTF-8?q?work=20(TxContext=C2=B7MessageCodec)=20(ACM-FW-001)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../acquirecore/framework/TxTemplate.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxTemplate.java diff --git a/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxTemplate.java b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxTemplate.java new file mode 100644 index 0000000..08fb43f --- /dev/null +++ b/boot/common-framework/src/main/java/com/klaro/acquirecore/framework/TxTemplate.java @@ -0,0 +1,65 @@ +package com.klaro.acquirecore.framework; + +import org.springframework.stereotype.Component; + +/** + * Transaction template providing tx_begin/commit/rollback semantics. + * Equivalent to C tx_begin(), tx_commit(), tx_rollback() functions. + */ +@Component +public class TxTemplate { + + private final TxServiceRegistry registry; + + public TxTemplate(TxServiceRegistry registry) { + this.registry = registry; + } + + /** + * Begin a new transaction context - equivalent to C tx_begin(). + */ + public TxCore.TxContext begin() { + return new TxCore.TxContext(); + } + + /** + * Begin with custom flags. + */ + public TxCore.TxContext begin(int flags) { + return new TxCore.TxContext(flags); + } + + /** + * Execute action within transaction context. + * Automatically handles rollback on exception. + */ + public T execute(TxCore.TxContext ctx, TxAction action) { + try { + return action.execute(ctx); + } catch (Exception e) { + ctx.resetBuffer(); + throw e; + } + } + + /** + * Dispatch request to named service - equivalent to C tx_dispatch(). + */ + public TxCore.TxResponse dispatch(String serviceName, TxCore.TxRequest request) { + return registry.dispatch(serviceName, request); + } + + /** + * Dispatch with new context. + */ + public TxCore.TxResponse dispatch(String serviceName, String operation, Object payload) { + TxCore.TxContext ctx = begin(); + TxCore.TxRequest request = new TxCore.TxRequest(serviceName, Map.of(operation, payload), ctx); + return dispatch(serviceName, request); + } + + @FunctionalInterface + public interface TxAction { + T execute(TxCore.TxContext context); + } +}