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

csip.KafkaPublisher Maven / Gradle / Ivy

Go to download

The Cloud Services Integration Platform is a SoA implementation to offer a Model-as-a-Service framework, Application Programming Interface, deployment infrastructure, and service implementations for environmental modeling.

There is a newer version: 2.6.30
Show newest version
/*
 * $Id: KafkaPublisher.java 461f710ffe44 2019-04-29 od $
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2019, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

/**
 * Kafka Publisher.
 *
 * @author od
 */
class KafkaPublisher implements Publisher {

  static final String STRING_SER = "org.apache.kafka.common.serialization.StringSerializer";

  Producer producer;
  ProducerCallback callback;
  String topic;

  Logger LOG;


  KafkaPublisher(Logger LOG, String topic, String bootstrap_servers, String acks,
      int retries, int max_block_ms) {

    if (topic == null) {
      throw new IllegalArgumentException("Not publishing to kafka because of 'null' topic.");
    }

    if (bootstrap_servers == null) {
      throw new IllegalArgumentException("Not publishing to kafka because of 'null' bootstrap_servers.");
    }

    Properties p = new Properties();
    p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrap_servers);
    p.put(ProducerConfig.ACKS_CONFIG, acks);
    p.put(ProducerConfig.RETRIES_CONFIG, retries);
    p.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, max_block_ms);
    p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, STRING_SER);
    p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, STRING_SER);

    this.LOG = LOG;
    this.topic = topic;

    producer = new KafkaProducer<>(p);
    callback = new ProducerCallback();
  }


  @Override
  public void publish(String key, String value) {
    if (LOG.isLoggable(Level.INFO)) {
      LOG.info("Publishing to topic '" + topic + "': " + key + "->" + value);
    }
    try {
      producer.send(new ProducerRecord<>(topic, key, value), callback).get();
    } catch (Exception ex) {
      LOG.log(Level.SEVERE, "Publish error:", ex);
    }
  }


  @Override
  public void close() throws Exception {
    if (producer != null) {
      producer.close();
    }
  }

  private class ProducerCallback implements Callback {

    @Override
    public void onCompletion(RecordMetadata recordMetadata, Exception e) {
      if (e != null) {
        LOG.severe("Error publishing: " + e.getMessage());
      }
    }
  }


  public static void main(String[] args) throws Exception {
    Publisher p = new KafkaPublisher(Logger.getAnonymousLogger(), "od123", "localhost:9092", "0", 1, 1000);
    p.publish("test", "hey");
    p.publish("test1", "hey");
    p.publish("test2", "hey");
    p.publish("test3", "hey");
    p.publish("test4", "hey");
    p.close();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy