Search in sources :

Example 1 with Context

use of com.kloia.eventapis.common.Context in project eventapis by kloiasoft.

the class EventMessageConverter method extractAndConvertValue.

@Override
public Object extractAndConvertValue(ConsumerRecord<?, ?> record, Type type) {
    Object value = record.value();
    if (value instanceof PublishedEventWrapper) {
        PublishedEventWrapper eventWrapper = (PublishedEventWrapper) value;
        Context context = eventWrapper.getContext();
        context.setCommandContext(record.topic());
        operationContext.switchContext(context);
        userContext.extractUserContext(eventWrapper.getUserContext());
        try {
            return objectMapper.readValue(eventWrapper.getEvent(), TypeFactory.rawClass(type));
        } catch (IOException e) {
            operationRepository.failOperation(operationContext.getCommandContext(), event -> event.setEventState(EventState.TXN_FAILED));
            throw new SerializationException(e);
        }
    } else
        return super.extractAndConvertValue(record, type);
}
Also used : OperationContext(com.kloia.eventapis.common.OperationContext) IUserContext(com.kloia.eventapis.api.IUserContext) Context(com.kloia.eventapis.common.Context) PublishedEventWrapper(com.kloia.eventapis.kafka.PublishedEventWrapper) MessagingMessageConverter(org.springframework.kafka.support.converter.MessagingMessageConverter) OperationContext(com.kloia.eventapis.common.OperationContext) SerializationException(org.apache.kafka.common.errors.SerializationException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) EventState(com.kloia.eventapis.pojos.EventState) IOperationRepository(com.kloia.eventapis.kafka.IOperationRepository) IUserContext(com.kloia.eventapis.api.IUserContext) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Type(java.lang.reflect.Type) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Context(com.kloia.eventapis.common.Context) PublishedEventWrapper(com.kloia.eventapis.kafka.PublishedEventWrapper) SerializationException(org.apache.kafka.common.errors.SerializationException) IOException(java.io.IOException)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 IUserContext (com.kloia.eventapis.api.IUserContext)1 Context (com.kloia.eventapis.common.Context)1 OperationContext (com.kloia.eventapis.common.OperationContext)1 IOperationRepository (com.kloia.eventapis.kafka.IOperationRepository)1 PublishedEventWrapper (com.kloia.eventapis.kafka.PublishedEventWrapper)1 EventState (com.kloia.eventapis.pojos.EventState)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 SerializationException (org.apache.kafka.common.errors.SerializationException)1 MessagingMessageConverter (org.springframework.kafka.support.converter.MessagingMessageConverter)1