[framework] TxCore → common-framework (TxContext·MessageCodec) (ACM-FW-001)
This commit is contained in:
parent
3fd7456027
commit
43cbbab5cc
1 changed files with 65 additions and 0 deletions
|
|
@ -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> T execute(TxCore.TxContext ctx, TxAction<T> 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> {
|
||||||
|
T execute(TxCore.TxContext context);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in a new issue