All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.edgar615.util.eventbus.KafkaEventProducer Maven / Gradle / Ivy

package com.github.edgar615.util.eventbus;

import com.github.edgar615.util.event.Event;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Edgar on 2017/4/19.
 *
 * @author Edgar  Date 2017/4/19
 */
public class KafkaEventProducer extends EventProducerImpl {
  private static final Logger LOGGER = LoggerFactory.getLogger(KafkaEventProducer.class);

  private Producer producer;

  public KafkaEventProducer(KafkaProducerOptions options) {
    super(options);
    producer = new KafkaProducer<>(options.toProps());
  }


  @Override
  public EventFuture sendEvent(Event event) {
    EventFuture future = EventFuture.future(event);
    ProducerRecord record =
            new ProducerRecord<>(event.head().to(), event);
    producer.send(record, (metadata, exception) -> {
      if (exception == null) {
        LOGGER.info("======> [{}] [OK] [{},{},{}] [{}] [{}] [{}]",
                    event.head().id(),
                    metadata.topic(),
                    metadata.partition(),
                    metadata.offset(),
                    event.head().action(),
                    Helper.toHeadString(event),
                    Helper.toActionString(event));
        future.complete(null);
      } else {
        LOGGER.error("======> [{}] [FAILED] [{}] [{}] [{}] [{}]",
                     event.head().id(),
                     event.head().to(),
                     event.head().action(),
                     Helper.toHeadString(event),
                     Helper.toActionString(event),
                     future.cause());
        future.fail(exception);
      }
    });
    return future;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy