Reporter.scala 1.94 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Media Converter
 * Extracts media files from Fedora repository
 * Copyright (C) 2020  Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package ch.memobase

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord, RecordMetadata}
23
import org.apache.kafka.common.header.Header
24
import org.apache.logging.log4j.scala.Logging
25

26 27
import java.util.Properties
import java.util.concurrent.{Future => JavaFuture}
28
import scala.collection.JavaConverters._
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
29 30 31 32 33 34 35 36


/**
 * Wrapper class for sending reports via Kafka topic
 *
 * @param props          Properties for wrapped KafkaProducer
 * @param reportingTopic Name of reporting topic
 */
37
class Reporter(props: Properties, reportingTopic: String) extends Logging {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
38 39 40

  private val producer = new KafkaProducer[String, String](props)

41 42 43
  def send(report: models.ReportingObject, recordSetHeader: Option[Header], institutionHeader: Option[Header]): JavaFuture[RecordMetadata] = {
    val producerRecord = new ProducerRecord[String, String](reportingTopic, 0, report.id, report.toString,
      List(recordSetHeader, institutionHeader).flatten.asJava)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
44
    logger.info(s"Sending report: $report")
45 46 47
    producer.send(producerRecord)
  }

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
48 49 50 51 52 53 54
  def close(): Unit = producer.close()

}

object Reporter {
  def apply(props: Properties, reportingTopic: String): Reporter = new Reporter(props, reportingTopic)
}