Search in sources :

Example 1 with EventuateLocalPublishingException

use of io.eventuate.local.common.exception.EventuateLocalPublishingException in project eventuate-local by eventuate-local.

the class DbLogBasedCdcKafkaPublisher method handleEvent.

@Override
public void handleEvent(EVENT publishedEvent) throws EventuateLocalPublishingException {
    Objects.requireNonNull(publishedEvent);
    logger.trace("Got record " + publishedEvent.toString());
    String aggregateTopic = publishingStrategy.topicFor(publishedEvent);
    String json = publishingStrategy.toJson(publishedEvent);
    Exception lastException = null;
    for (int i = 0; i < 5; i++) {
        try {
            if (duplicatePublishingDetector.shouldBePublished(publishedEvent.getBinlogFileOffset(), aggregateTopic)) {
                producer.send(aggregateTopic, publishingStrategy.partitionKeyFor(publishedEvent), json).get(10, TimeUnit.SECONDS);
                publishingStrategy.getCreateTime(publishedEvent).ifPresent(time -> histogramEventAge.ifPresent(x -> x.set(System.currentTimeMillis() - time)));
                meterEventsPublished.ifPresent(Counter::increment);
                databaseOffsetKafkaStore.save(publishedEvent.getBinlogFileOffset());
            } else {
                logger.debug("Duplicate event {}", publishedEvent);
                meterEventsDuplicates.ifPresent(Counter::increment);
            }
            return;
        } catch (Exception e) {
            logger.warn("error publishing to " + aggregateTopic, e);
            meterEventsRetries.ifPresent(Counter::increment);
            lastException = e;
            try {
                Thread.sleep((int) Math.pow(2, i) * 1000);
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            }
        }
    }
    throw new EventuateLocalPublishingException("error publishing to " + aggregateTopic, lastException);
}
Also used : EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException) Counter(io.micrometer.core.instrument.Counter) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) BinLogEvent(io.eventuate.local.common.BinLogEvent) Logger(org.slf4j.Logger) CdcKafkaPublisher(io.eventuate.local.common.CdcKafkaPublisher) PublishingStrategy(io.eventuate.local.common.PublishingStrategy) LoggerFactory(org.slf4j.LoggerFactory) EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException) Counter(io.micrometer.core.instrument.Counter) EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException)

Example 2 with EventuateLocalPublishingException

use of io.eventuate.local.common.exception.EventuateLocalPublishingException in project eventuate-local by eventuate-local.

the class PollingCdcKafkaPublisher method handleEvent.

@Override
public void handleEvent(EVENT event) throws EventuateLocalPublishingException {
    logger.trace("Got record " + event.toString());
    String aggregateTopic = publishingStrategy.topicFor(event);
    String json = publishingStrategy.toJson(event);
    Exception lastException = null;
    for (int i = 0; i < 5; i++) {
        try {
            producer.send(aggregateTopic, publishingStrategy.partitionKeyFor(event), json).get(10, TimeUnit.SECONDS);
            publishingStrategy.getCreateTime(event).ifPresent(time -> histogramEventAge.ifPresent(x -> x.set(System.currentTimeMillis() - time)));
            meterEventsPublished.ifPresent(Counter::increment);
            return;
        } catch (Exception e) {
            logger.warn("error publishing to " + aggregateTopic, e);
            meterEventsRetries.ifPresent(Counter::increment);
            lastException = e;
            try {
                Thread.sleep((int) Math.pow(2, i) * 1000);
            } catch (InterruptedException ie) {
                throw new RuntimeException(ie);
            }
        }
    }
    throw new EventuateLocalPublishingException("error publishing to " + aggregateTopic, lastException);
}
Also used : EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException) Counter(io.micrometer.core.instrument.Counter) TimeUnit(java.util.concurrent.TimeUnit) Logger(org.slf4j.Logger) CdcKafkaPublisher(io.eventuate.local.common.CdcKafkaPublisher) PublishingStrategy(io.eventuate.local.common.PublishingStrategy) LoggerFactory(org.slf4j.LoggerFactory) EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException) Counter(io.micrometer.core.instrument.Counter) EventuateLocalPublishingException(io.eventuate.local.common.exception.EventuateLocalPublishingException)

Aggregations

CdcKafkaPublisher (io.eventuate.local.common.CdcKafkaPublisher)2 PublishingStrategy (io.eventuate.local.common.PublishingStrategy)2 EventuateLocalPublishingException (io.eventuate.local.common.exception.EventuateLocalPublishingException)2 Counter (io.micrometer.core.instrument.Counter)2 TimeUnit (java.util.concurrent.TimeUnit)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 BinLogEvent (io.eventuate.local.common.BinLogEvent)1 Objects (java.util.Objects)1