use of com.kloia.eventapis.common.EventKey in project eventapis by kloiasoft.
the class CompositeRepositoryImpl method recordAndPublishInternal.
private <P extends PublishedEvent, T extends Exception> EventKey recordAndPublishInternal(P publishedEvent, Optional<EventKey> previousEventKey, Function<EntityEvent, ConcurrencyResolver<T>> concurrencyResolverFactory) throws EventStoreException, T {
long opDate = System.currentTimeMillis();
EventKey eventKey = eventRecorder.recordEntityEvent(publishedEvent, opDate, previousEventKey, concurrencyResolverFactory);
publishedEvent.setSender(eventKey);
String event;
try {
event = objectMapper.writerWithView(Views.PublishedOnly.class).writeValueAsString(publishedEvent);
} catch (JsonProcessingException e) {
throw new EventStoreException(e.getMessage(), e);
}
operationRepository.publishEvent(publishedEvent.getClass().getSimpleName(), event, opDate);
checkOperationFinalStates(publishedEvent);
return publishedEvent.getSender();
}
use of com.kloia.eventapis.common.EventKey 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.common.EventKey in project eventapis by kloiasoft.
the class CassandraViewQuery method convertToEntityEvent.
static EntityEvent convertToEntityEvent(Row entityEventData) {
EventKey eventKey = new EventKey(entityEventData.getString(CassandraEventRecorder.ENTITY_ID), entityEventData.getInt(CassandraEventRecorder.VERSION));
String opId = entityEventData.getString(CassandraEventRecorder.OP_ID);
String eventData = entityEventData.getString(CassandraEventRecorder.EVENT_DATA);
return new EntityEvent(eventKey, opId, entityEventData.getTimestamp(CassandraEventRecorder.OP_DATE), entityEventData.getString(CassandraEventRecorder.EVENT_TYPE), EventState.valueOf(entityEventData.getString(CassandraEventRecorder.STATUS)), entityEventData.getString(CassandraEventRecorder.AUDIT_INFO), eventData);
}
use of com.kloia.eventapis.common.EventKey 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.common.EventKey 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