Search in sources :

Example 1 with EventConsumer

use of io.seata.saga.proctrl.eventing.EventConsumer in project seata by seata.

the class DirectEventBus method offer.

@Override
public boolean offer(ProcessContext context) throws FrameworkException {
    List<EventConsumer> eventHandlers = getEventConsumers(context.getClass());
    if (CollectionUtils.isEmpty(eventHandlers)) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("cannot find event handler by class: " + context.getClass());
        }
        return false;
    }
    boolean isFirstEvent = false;
    Stack<ProcessContext> currentStack = (Stack<ProcessContext>) context.getVariable(VAR_NAME_SYNC_EXE_STACK);
    if (currentStack == null) {
        synchronized (context) {
            currentStack = (Stack<ProcessContext>) context.getVariable(VAR_NAME_SYNC_EXE_STACK);
            if (currentStack == null) {
                currentStack = new Stack<>();
                context.setVariable(VAR_NAME_SYNC_EXE_STACK, currentStack);
                isFirstEvent = true;
            }
        }
    }
    currentStack.push(context);
    if (isFirstEvent) {
        try {
            while (currentStack.size() > 0) {
                ProcessContext currentContext = currentStack.pop();
                for (EventConsumer eventHandler : eventHandlers) {
                    eventHandler.process(currentContext);
                }
            }
        } finally {
            context.removeVariable(VAR_NAME_SYNC_EXE_STACK);
        }
    }
    return true;
}
Also used : EventConsumer(io.seata.saga.proctrl.eventing.EventConsumer) ProcessContext(io.seata.saga.proctrl.ProcessContext) Stack(java.util.Stack)

Aggregations

ProcessContext (io.seata.saga.proctrl.ProcessContext)1 EventConsumer (io.seata.saga.proctrl.eventing.EventConsumer)1 Stack (java.util.Stack)1