Search in sources :

Example 6 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project eventapis by kloiasoft.

the class SpringKafkaOpListener method listenOperations.

@Transactional(rollbackFor = Exception.class)
@KafkaListener(id = "op-listener", topics = Operation.OPERATION_EVENTS, containerFactory = "operationsKafkaListenerContainerFactory")
void listenOperations(ConsumerRecord<String, Operation> record) {
    String key = record.key();
    Operation value = record.value();
    log.debug("Incoming Message: " + key + " " + value);
    userContext.extractUserContext(value.getUserContext());
    for (AggregateListener snapshotRecorder : aggregateListeners) {
        snapshotRecorder.listenOperations(record);
    }
}
Also used : Operation(com.kloia.eventapis.pojos.Operation) AggregateListener(com.kloia.eventapis.view.AggregateListener) KafkaListener(org.springframework.kafka.annotation.KafkaListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener 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)

Example 8 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project summer by foxsugar.

the class UserServiceMsgConsumer method listen2.

@KafkaListener(id = "centerTopic", topicPattern = "center_topic")
public void listen2(ConsumerRecord<String, String> record) {
    ThreadPool.getInstance().executor.execute(() -> {
        String key = record.key();
        String value = record.value();
        KafkaMsgKey msgKey = JsonUtil.readValue(key, KafkaMsgKey.class);
        CenterMsgService.dispatch(msgKey, value);
    });
}
Also used : KafkaMsgKey(com.code.server.constant.kafka.KafkaMsgKey) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 9 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener in project open-kilda by telstra.

the class KafkaHealthCheckMessageConsumer method receive.

/**
 * Receives messages from WorkFlowManager queue.
 *
 * @param record the message object instance
 */
@KafkaListener(id = "northbound-listener-health-check", topics = Topic.HEALTH_CHECK)
public void receive(final String record) {
    try {
        logger.trace("message received");
        Message message = MAPPER.readValue(record, Message.class);
        if (Destination.NORTHBOUND.equals(message.getDestination())) {
            logger.debug("message received: {}", record);
            InfoMessage info = (InfoMessage) message;
            HealthCheckInfoData healthCheck = (HealthCheckInfoData) info.getData();
            messages.put(healthCheck.getId(), healthCheck);
        } else {
            logger.trace("Skip message: {}", message);
        }
    } catch (IOException exception) {
        logger.error("Could not deserialize message: {}", record, exception);
    }
}
Also used : HealthCheckInfoData(org.openkilda.messaging.info.discovery.HealthCheckInfoData) InfoMessage(org.openkilda.messaging.info.InfoMessage) Message(org.openkilda.messaging.Message) InfoMessage(org.openkilda.messaging.info.InfoMessage) IOException(java.io.IOException) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 10 with KafkaListener

use of org.springframework.kafka.annotation.KafkaListener 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);
}
Also used : Orders(com.kloia.sample.model.Orders) EventStoreException(com.kloia.eventapis.exception.EventStoreException) OrderCancelledEvent(com.kloia.sample.dto.event.OrderCancelledEvent) ReturnPaymentCommandDto(com.kloia.sample.dto.command.ReturnPaymentCommandDto) EventKey(com.kloia.eventapis.common.EventKey) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Aggregations

KafkaListener (org.springframework.kafka.annotation.KafkaListener)13 KafkaMsgKey (com.code.server.constant.kafka.KafkaMsgKey)3 EventStoreException (com.kloia.eventapis.exception.EventStoreException)3 IOException (java.io.IOException)3 Message (org.openkilda.messaging.Message)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 StockReservedEvent (com.kloia.sample.dto.event.StockReservedEvent)2 Orders (com.kloia.sample.model.Orders)2 Stock (com.kloia.sample.model.Stock)2 InfoMessage (org.openkilda.messaging.info.InfoMessage)2 ClubServiceMsgDispatch (com.code.server.login.service.ClubServiceMsgDispatch)1 UserServiceMsgDispatch (com.code.server.login.service.UserServiceMsgDispatch)1 JsonObject (com.google.gson.JsonObject)1 EventKey (com.kloia.eventapis.common.EventKey)1 Operation (com.kloia.eventapis.pojos.Operation)1 AggregateListener (com.kloia.eventapis.view.AggregateListener)1 StockNotEnoughException (com.kloia.sample.dto.StockNotEnoughException)1 ReturnPaymentCommandDto (com.kloia.sample.dto.command.ReturnPaymentCommandDto)1 OrderCancelledEvent (com.kloia.sample.dto.event.OrderCancelledEvent)1 PaymentFailedEvent (com.kloia.sample.dto.event.PaymentFailedEvent)1