use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.
the class CassandraEventRecorder method recordEntityEvent.
/* private EntityEvent convertToEntityEvent(Row entityEventData) throws EventStoreException {
EventKey eventKey = new EventKey(entityEventData.getString(ENTITY_ID), entityEventData.getInt(VERSION));
String opId = entityEventData.getString(OP_ID);
String eventData = entityEventData.getString("eventData");
return new EntityEvent(eventKey, opId, entityEventData.getTimestamp(OP_DATE), entityEventData.getString("eventType"), entityEventData.getString("status"), eventData);
}*/
// private ConcurrencyResolver concurrencyResolver = new DefaultConcurrencyResolver();
// private Function<E, ConcurrencyResolver> concurrencyResolverFactory;
@Override
public <T extends Exception> EventKey recordEntityEvent(RecordedEvent event, long opDate, Optional<EventKey> previousEventKey, Function<EntityEvent, ConcurrencyResolver<T>> concurrencyResolverFactory) throws EventStoreException, T {
ConcurrencyResolver<T> concurrencyResolver = null;
String eventData = null;
try {
eventData = objectMapper.writerWithView(Views.RecordedOnly.class).writeValueAsString(event);
} catch (IllegalArgumentException | JsonProcessingException e) {
throw new EventStoreException(e.getMessage(), e);
}
EventKey eventKey;
if (previousEventKey.isPresent()) {
eventKey = new EventKey(previousEventKey.get().getEntityId(), previousEventKey.get().getVersion() + 1);
} else
eventKey = new EventKey(idCreationStrategy.nextId(), 0);
EntityEvent entityEvent = new EntityEvent(eventKey, operationContext.getContextOpId(), new Date(opDate), event.getEventName(), EventState.CREATED, userContext.getAuditInfo(), eventData);
while (true) {
Insert insert = createInsertQuery(entityEvent);
log.debug("Recording Event: " + insert.toString());
ResultSet resultSet = cassandraSession.execute(insert);
log.debug("Recorded Event: " + resultSet.toString());
if (resultSet.wasApplied()) {
return entityEvent.getEventKey();
} else {
Row one = resultSet.one();
log.warn("!wasApplied: " + one.getBool("[applied]"));
if (concurrencyResolver == null)
concurrencyResolver = concurrencyResolverFactory.apply(entityEvent);
// go on or finish
concurrencyResolver.tryMore();
Select select = QueryBuilder.select().max(VERSION).from(tableName);
select.where(QueryBuilder.eq(ENTITY_ID, entityEvent.getEventKey().getEntityId()));
ResultSet execute = cassandraSession.execute(select);
int lastVersion = execute.one().getInt(0);
entityEvent.setEventKey(concurrencyResolver.calculateNext(entityEvent.getEventKey(), lastVersion));
}
}
}
use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.
the class CassandraViewQuery method queryEntityInternal.
private E queryEntityInternal(String entityId, Select select) throws EventStoreException {
E initialInstance;
E result = null;
try {
initialInstance = entityType.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
log.error(e.getMessage(), e);
throw new EventStoreException(e);
}
List<Row> entityEventDatas = cassandraSession.execute(select, PagingIterable::all);
for (Row entityEventData : entityEventDatas) {
EntityEvent entityEvent = convertToEntityEvent(entityEventData);
if (entityEvent.getStatus() == EventState.CREATED) {
EntityFunctionSpec<E, ?> functionSpec = functionMap.get(entityEvent.getEventType());
if (functionSpec != null) {
EntityEventWrapper eventWrapper = new EntityEventWrapper<>(functionSpec.getQueryType(), objectMapper, entityEvent);
EntityFunction<E, ?> entityFunction = functionSpec.getEntityFunction();
result = (E) entityFunction.apply(result == null ? initialInstance : result, eventWrapper);
} else
log.trace("Function Spec is not available for " + entityEvent.getEventType() + " EntityId:" + entityId + " Table:" + tableName);
}
if (result != null) {
result.setId(entityId);
result.setVersion(entityEvent.getEventKey().getVersion());
}
}
return (result == null || result.getId() == null) ? null : result;
}
use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.
the class StockReleasedEventHandler method execute.
@KafkaListener(topics = "StockReleasedEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(StockReleasedEvent dto) throws EventStoreException, ConcurrentEventException {
Orders order = orderQuery.queryEntity(dto.getOrderId());
if (order.getState() == OrderState.RELEASING_STOCK) {
OrderCancelledEvent orderCancelledEvent = new OrderCancelledEvent();
log.info("Payment is processing : " + dto);
EventKey eventKey = eventRepository.recordAndPublish(order, orderCancelledEvent);
paymentClient.returnPaymentCommand(order.getPaymentId(), new ReturnPaymentCommandDto(order.getId()));
return eventKey;
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.
the class StockReservedEventHandler method execute.
@Override
@KafkaListener(topics = "StockReservedEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(StockReservedEvent dto) throws EventStoreException, ConcurrentEventException {
Orders order = orderQuery.queryEntity(dto.getOrderId());
if (order.getState() == OrderState.RESERVING_STOCK) {
PaymentProcessEvent paymentProcessEvent = new PaymentProcessEvent(order.getId(), dto.getSender().getVersion(), new PaymentInformation(order.getPaymentAddress(), order.getAmount(), order.getCardInformation()));
log.info("Payment is processing : " + dto);
return eventRepository.recordAndPublish(order, paymentProcessEvent);
} else
throw new EventStoreException("Order state is not valid for this Operation: " + dto);
}
use of com.kloia.eventapis.exception.EventStoreException in project eventapis by kloiasoft.
the class CompositeRepositoryImplTest method shouldRecordAndPublishWithPreviousEventAndPublishedEventAndConcurrencyResolverFactory.
@Test
public void shouldRecordAndPublishWithPreviousEventAndPublishedEventAndConcurrencyResolverFactory() throws JsonProcessingException, EventStoreException, ConcurrentEventException {
mockCommon(intermediateEvent);
Entity previousEntity = mock(Entity.class);
EventKey previousEntityEventKey = new EventKey();
when(previousEntity.getEventKey()).thenReturn(previousEntityEventKey);
ConcurrencyResolver concurrencyResolver = mock(ConcurrencyResolver.class);
Function<EntityEvent, ConcurrencyResolver<ConcurrentEventException>> factory = entityEvent -> concurrencyResolver;
EventKey actual = compositeRepository.recordAndPublish(previousEntity, intermediateEvent, factory);
assertCommon(intermediateEvent);
assertThat(actual, equalTo(eventKey));
assertThat(previousEventKeyCaptor.getValue().isPresent(), equalTo(true));
assertThat(previousEventKeyCaptor.getValue().get(), equalTo(previousEntityEventKey));
assertThat(concurrencyResolverFactoryCaptor.getValue(), equalTo(factory));
}
Aggregations