Search in sources :

Example 1 with StockNotEnoughException

use of com.kloia.sample.dto.StockNotEnoughException in project eventapis by kloiasoft.

the class ReserveStockEventHandler method execute.

@Override
@KafkaListener(topics = "ReserveStockEvent", containerFactory = "eventsKafkaListenerContainerFactory")
public EventKey execute(ReserveStockEvent dto) throws Exception {
    Stock stock = stockQuery.queryEntity(dto.getStockId());
    if (stock.getRemainingStock() >= dto.getNumberOfItemsSold()) {
        StockReservedEvent stockReservedEvent = new StockReservedEvent();
        BeanUtils.copyProperties(dto, stockReservedEvent);
        stockReservedEvent.setOrderId(dto.getSender().getEntityId());
        try {
            return eventRepository.recordAndPublish(stock, stockReservedEvent, entityEvent -> new StockConcurrencyResolver(stockQuery, dto));
        } catch (StockNotEnoughException e) {
            return recordStockNotEnough(dto, stock);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return stock.getEventKey();
        }
    } else
        return recordStockNotEnough(dto, stock);
}
Also used : StockNotEnoughException(com.kloia.sample.dto.StockNotEnoughException) StockReservedEvent(com.kloia.sample.dto.event.StockReservedEvent) Stock(com.kloia.sample.model.Stock) StockNotEnoughException(com.kloia.sample.dto.StockNotEnoughException) EventStoreException(com.kloia.eventapis.exception.EventStoreException) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Aggregations

EventStoreException (com.kloia.eventapis.exception.EventStoreException)1 StockNotEnoughException (com.kloia.sample.dto.StockNotEnoughException)1 StockReservedEvent (com.kloia.sample.dto.event.StockReservedEvent)1 Stock (com.kloia.sample.model.Stock)1 KafkaListener (org.springframework.kafka.annotation.KafkaListener)1