Search in sources :

Example 1 with PublishedEventWrapper

use of com.kloia.eventapis.kafka.PublishedEventWrapper in project eventapis by kloiasoft.

the class EventApisFactory method eventsKafkaListenerContainerFactory.

@Bean({ "eventsKafkaListenerContainerFactory", "kafkaListenerContainerFactory" })
public ConcurrentKafkaListenerContainerFactory<String, PublishedEventWrapper> eventsKafkaListenerContainerFactory(EventMessageConverter eventMessageConverter, ConsumerFactory<String, PublishedEventWrapper> consumerFactory) {
    ConcurrentKafkaListenerContainerFactory<String, PublishedEventWrapper> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory);
    factory.setConcurrency(eventApisConfiguration.getEventBus().getConsumer().getEventConcurrency());
    factory.setMessageConverter(eventMessageConverter);
    factory.getContainerProperties().setPollTimeout(3000);
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setPoolSize(eventApisConfiguration.getEventBus().getConsumer().getEventSchedulerPoolSize());
    scheduler.setBeanName("EventsFactory-Scheduler");
    scheduler.initialize();
    factory.getContainerProperties().setScheduler(scheduler);
    factory.getContainerProperties().setAckMode(AbstractMessageListenerContainer.AckMode.RECORD);
    return factory;
}
Also used : PublishedEventWrapper(com.kloia.eventapis.kafka.PublishedEventWrapper) ConcurrentKafkaListenerContainerFactory(org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with PublishedEventWrapper

use of com.kloia.eventapis.kafka.PublishedEventWrapper 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)

Example 3 with PublishedEventWrapper

use of com.kloia.eventapis.kafka.PublishedEventWrapper in project eventapis by kloiasoft.

the class EventListenConfiguration method startEvents.

private void startEvents() {
    Map<String, Object> consumerProperties = eventApisConfiguration.getEventBus().buildConsumerProperties();
    DefaultKafkaConsumerFactory<String, PublishedEventWrapper> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerProperties, new StringDeserializer(), new JsonDeserializer<>(PublishedEventWrapper.class));
    ContainerProperties containerProperties = new ContainerProperties(Pattern.compile(eventTopicRegexStr));
    containerProperties.setMessageListener(new MultipleEventMessageListener(eventMessageListeners));
    messageListenerContainer = new ConcurrentMessageListenerContainer<>(consumerFactory, containerProperties);
    messageListenerContainer.setBeanName("emon-events");
    messageListenerContainer.start();
}
Also used : PublishedEventWrapper(com.kloia.eventapis.kafka.PublishedEventWrapper) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) MultipleEventMessageListener(com.kloia.eventapis.api.emon.service.MultipleEventMessageListener) ContainerProperties(org.springframework.kafka.listener.config.ContainerProperties) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory)

Aggregations

PublishedEventWrapper (com.kloia.eventapis.kafka.PublishedEventWrapper)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)1 IUserContext (com.kloia.eventapis.api.IUserContext)1 MultipleEventMessageListener (com.kloia.eventapis.api.emon.service.MultipleEventMessageListener)1 Context (com.kloia.eventapis.common.Context)1 OperationContext (com.kloia.eventapis.common.OperationContext)1 IOperationRepository (com.kloia.eventapis.kafka.IOperationRepository)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 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)1 Bean (org.springframework.context.annotation.Bean)1 ConcurrentKafkaListenerContainerFactory (org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory)1 DefaultKafkaConsumerFactory (org.springframework.kafka.core.DefaultKafkaConsumerFactory)1 ContainerProperties (org.springframework.kafka.listener.config.ContainerProperties)1