use of io.seata.saga.engine.AsyncCallback in project seata by seata.
the class StateMachineDBTests method simpleChoiceTestStateMachineAsyncConcurrently.
@Test
@Disabled("https://github.com/seata/seata/issues/2414#issuecomment-639546811")
public void simpleChoiceTestStateMachineAsyncConcurrently() throws Exception {
final CountDownLatch countDownLatch = new CountDownLatch(100);
final List<Exception> exceptions = new ArrayList<>();
final AsyncCallback asyncCallback = new AsyncCallback() {
@Override
public void onFinished(ProcessContext context, StateMachineInstance stateMachineInstance) {
countDownLatch.countDown();
}
@Override
public void onError(ProcessContext context, StateMachineInstance stateMachineInstance, Exception exp) {
countDownLatch.countDown();
exceptions.add(exp);
}
};
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int j = 0; j < 10; j++) {
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("a", 1);
paramMap.put("barThrowException", "false");
String stateMachineName = "simpleCompensationStateMachine";
try {
stateMachineEngine.startAsync(stateMachineName, null, paramMap, asyncCallback);
} catch (Exception e) {
countDownLatch.countDown();
exceptions.add(e);
}
}
}
});
t.start();
}
countDownLatch.await(10000, TimeUnit.MILLISECONDS);
if (exceptions.size() > 0) {
Assertions.fail(exceptions.get(0));
}
long cost = System.currentTimeMillis() - start;
System.out.println("====== cost :" + cost);
}
use of io.seata.saga.engine.AsyncCallback in project seata by seata.
the class EngineUtils method endStateMachine.
/**
* end StateMachine
*
* @param context
*/
public static void endStateMachine(ProcessContext context) {
if (context.hasVariable(DomainConstants.VAR_NAME_IS_LOOP_STATE)) {
if (context.hasVariable(DomainConstants.LOOP_SEMAPHORE)) {
Semaphore semaphore = (Semaphore) context.getVariable(DomainConstants.LOOP_SEMAPHORE);
semaphore.release();
}
return;
}
StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
stateMachineInstance.setGmtEnd(new Date());
Exception exp = (Exception) context.getVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
if (exp != null) {
stateMachineInstance.setException(exp);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Exception Occurred: " + exp);
}
}
StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
stateMachineConfig.getStatusDecisionStrategy().decideOnEndState(context, stateMachineInstance, exp);
stateMachineInstance.getEndParams().putAll((Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT));
StateInstruction instruction = context.getInstruction(StateInstruction.class);
instruction.setEnd(true);
stateMachineInstance.setRunning(false);
stateMachineInstance.setGmtEnd(new Date());
if (stateMachineInstance.getStateMachine().isPersist() && stateMachineConfig.getStateLogStore() != null) {
stateMachineConfig.getStateLogStore().recordStateMachineFinished(stateMachineInstance, context);
}
AsyncCallback callback = (AsyncCallback) context.getVariable(DomainConstants.VAR_NAME_ASYNC_CALLBACK);
if (callback != null) {
if (exp != null) {
callback.onError(context, stateMachineInstance, exp);
} else {
callback.onFinished(context, stateMachineInstance);
}
}
}
use of io.seata.saga.engine.AsyncCallback in project seata by seata.
the class EngineUtils method failStateMachine.
/**
* fail StateMachine
*
* @param context
* @param exp
*/
public static void failStateMachine(ProcessContext context, Exception exp) {
if (context.hasVariable(DomainConstants.VAR_NAME_IS_LOOP_STATE)) {
return;
}
StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
stateMachineConfig.getStatusDecisionStrategy().decideOnTaskStateFail(context, stateMachineInstance, exp);
stateMachineInstance.getEndParams().putAll((Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT));
StateInstruction instruction = context.getInstruction(StateInstruction.class);
instruction.setEnd(true);
stateMachineInstance.setRunning(false);
stateMachineInstance.setGmtEnd(new Date());
stateMachineInstance.setException(exp);
if (stateMachineInstance.getStateMachine().isPersist() && stateMachineConfig.getStateLogStore() != null) {
stateMachineConfig.getStateLogStore().recordStateMachineFinished(stateMachineInstance, context);
}
AsyncCallback callback = (AsyncCallback) context.getVariable(DomainConstants.VAR_NAME_ASYNC_CALLBACK);
if (callback != null) {
callback.onError(context, stateMachineInstance, exp);
}
}
Aggregations