use of com.kloia.eventapis.pojos.CommandRecord in project eventapis by kloiasoft.
the class CommandExecutionInterceptor method recordCommand.
private CommandRecord recordCommand(JoinPoint jp, CommandHandler commandHandler, Command command) throws ConcurrentEventException, EventStoreException {
EventRepository eventRepository;
CommandDto commandDto = null;
CommandRecord commandRecord = new CommandRecord();
commandRecord.setEventName(commandHandler.getClass().getSimpleName());
for (int i = 0; i < jp.getArgs().length; i++) {
Object arg = jp.getArgs()[i];
commandRecord.getParameters().put(i, arg);
}
// }
try {
Field declaredField = commandHandler.getClass().getDeclaredField(command.eventRepository());
if (!declaredField.isAccessible())
declaredField.setAccessible(true);
eventRepository = (EventRepository) declaredField.get(commandHandler);
} catch (IllegalAccessException | NoSuchFieldException e) {
log.error("Error while accessing EventRecorder(" + command.eventRepository() + ") of Command:" + commandHandler.getClass().getSimpleName() + " message: " + e.getMessage(), e);
return null;
}
if (eventRepository != null) {
eventRepository.getEventRecorder().recordEntityEvent(commandRecord, System.currentTimeMillis(), Optional.empty(), entityEvent -> new DefaultConcurrencyResolver());
} else
log.error("Error while accessing EventRecorder(" + command.eventRepository() + " is null ) of Command:" + commandHandler.getClass().getSimpleName());
return commandRecord;
}
use of com.kloia.eventapis.pojos.CommandRecord in project eventapis by kloiasoft.
the class RollbackCommandSpec method rollback.
default void rollback(CommandRecord record) {
// Get from context
ObjectMapper objectMapper = new ObjectMapper();
for (Method method : this.getClass().getDeclaredMethods()) {
if (method.getName().equals("rollback")) {
if (method.getParameterCount() == 1 && method.getParameterTypes()[0] == CommandRecord.class)
continue;
try {
Object[] args = new Object[method.getParameterCount()];
for (Map.Entry<Integer, ?> entry : record.getParameters().entrySet()) {
Class<?> type = method.getParameterTypes()[entry.getKey()];
args[entry.getKey()] = objectMapper.treeToValue((TreeNode) entry.getValue(), type);
}
method.invoke(this, args);
} catch (InvocationTargetException | IllegalAccessException | IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
}
use of com.kloia.eventapis.pojos.CommandRecord in project eventapis by kloiasoft.
the class CommandExecutionInterceptor method before.
@Before("this(com.kloia.eventapis.api.CommandHandler) && @annotation(command)")
public void before(JoinPoint jp, Command command) throws Throwable {
Object target = jp.getTarget();
if (!(target instanceof CommandHandler))
throw new IllegalArgumentException("Point is not Instance of CommandHandler");
CommandHandler commandHandler = (CommandHandler) target;
long commandTimeout = command.commandTimeout();
// Ability to generate new Context
operationContext.startNewContext(commandTimeout);
operationContext.setCommandContext(target.getClass().getSimpleName());
CommandRecord commandDto = recordCommand(jp, commandHandler, command);
log.debug("before method:" + (commandDto == null ? "" : commandDto.toString()));
}
Aggregations