Search in sources :

Example 1 with PublishedEventWrapper

use of io.splitet.core.kafka.PublishedEventWrapper in project eventapis by kloiasoft.

the class EventListenConfiguration method messageListenerContainer.

@Bean(name = "messageListenerContainer")
public ConcurrentMessageListenerContainer<String, PublishedEventWrapper> messageListenerContainer() {
    Map<String, Object> consumerProperties = eventApisConfiguration.getEventBus().buildConsumerProperties();
    consumerProperties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
    consumerProperties.put(ConsumerConfig.METADATA_MAX_AGE_CONFIG, 3000);
    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));
    containerProperties.setAckMode(ContainerProperties.AckMode.BATCH);
    ConcurrentMessageListenerContainer<String, PublishedEventWrapper> messageListenerContainer = new ConcurrentMessageListenerContainer<>(consumerFactory, containerProperties);
    messageListenerContainer.setConcurrency(eventApisConfiguration.getEventBus().getConsumer().getEventConcurrency());
    messageListenerContainer.setBeanName("emon-events");
    return messageListenerContainer;
}
Also used : PublishedEventWrapper(io.splitet.core.kafka.PublishedEventWrapper) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) MultipleEventMessageListener(io.splitet.core.api.emon.service.MultipleEventMessageListener) ContainerProperties(org.springframework.kafka.listener.ContainerProperties) ConcurrentMessageListenerContainer(org.springframework.kafka.listener.ConcurrentMessageListenerContainer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) Bean(org.springframework.context.annotation.Bean)

Example 2 with PublishedEventWrapper

use of io.splitet.core.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 : Context(io.splitet.core.common.Context) IUserContext(io.splitet.core.api.IUserContext) OperationContext(io.splitet.core.common.OperationContext) MessagingMessageConverter(org.springframework.kafka.support.converter.MessagingMessageConverter) PublishedEventWrapper(io.splitet.core.kafka.PublishedEventWrapper) SerializationException(org.apache.kafka.common.errors.SerializationException) Context(io.splitet.core.common.Context) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) EventState(io.splitet.core.pojos.EventState) IOperationRepository(io.splitet.core.kafka.IOperationRepository) IUserContext(io.splitet.core.api.IUserContext) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) Type(java.lang.reflect.Type) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) OperationContext(io.splitet.core.common.OperationContext) PublishedEventWrapper(io.splitet.core.kafka.PublishedEventWrapper) SerializationException(org.apache.kafka.common.errors.SerializationException) IOException(java.io.IOException)

Example 3 with PublishedEventWrapper

use of io.splitet.core.kafka.PublishedEventWrapper in project eventapis by kloiasoft.

the class EventApisFactory method kafkaConsumerFactory.

@Bean
public ConsumerFactory<String, PublishedEventWrapper> kafkaConsumerFactory() {
    KafkaProperties properties = eventApisConfiguration.getEventBus().clone();
    properties.getConsumer().setEnableAutoCommit(false);
    return new DefaultKafkaConsumerFactory<>(properties.buildConsumerProperties(), new StringDeserializer(), new JsonDeserializer<>(PublishedEventWrapper.class, objectMapper));
}
Also used : PublishedEventWrapper(io.splitet.core.kafka.PublishedEventWrapper) KafkaProperties(io.splitet.core.kafka.KafkaProperties) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) DefaultKafkaConsumerFactory(org.springframework.kafka.core.DefaultKafkaConsumerFactory) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with PublishedEventWrapper

use of io.splitet.core.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(ContainerProperties.AckMode.RECORD);
    return factory;
}
Also used : PublishedEventWrapper(io.splitet.core.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)

Aggregations

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