Search in sources :

Example 1 with CommandRecord

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;
}
Also used : Field(java.lang.reflect.Field) DefaultConcurrencyResolver(com.kloia.eventapis.cassandra.DefaultConcurrencyResolver) EventRepository(com.kloia.eventapis.api.EventRepository) CommandDto(com.kloia.eventapis.api.CommandDto) CommandRecord(com.kloia.eventapis.pojos.CommandRecord) JoinPoint(org.aspectj.lang.JoinPoint)

Example 2 with 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);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TreeNode(com.fasterxml.jackson.core.TreeNode) CommandRecord(com.kloia.eventapis.pojos.CommandRecord) AbstractMap(java.util.AbstractMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with CommandRecord

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()));
}
Also used : CommandHandler(com.kloia.eventapis.api.CommandHandler) CommandRecord(com.kloia.eventapis.pojos.CommandRecord) Before(org.aspectj.lang.annotation.Before)

Aggregations

CommandRecord (com.kloia.eventapis.pojos.CommandRecord)3 TreeNode (com.fasterxml.jackson.core.TreeNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CommandDto (com.kloia.eventapis.api.CommandDto)1 CommandHandler (com.kloia.eventapis.api.CommandHandler)1 EventRepository (com.kloia.eventapis.api.EventRepository)1 DefaultConcurrencyResolver (com.kloia.eventapis.cassandra.DefaultConcurrencyResolver)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 AbstractMap (java.util.AbstractMap)1 Map (java.util.Map)1 JoinPoint (org.aspectj.lang.JoinPoint)1 Before (org.aspectj.lang.annotation.Before)1