Reporter.scala 1.8 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
24
import org.apache.kafka.common.header.Header

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


/**
 * Wrapper class for sending reports via Kafka topic
 *
 * @param props          Properties for wrapped KafkaProducer
 * @param reportingTopic Name of reporting topic
 */
class Reporter(props: Properties, reportingTopic: String) {

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

40
41
  def send(report: models.ReportingObject, recordSetHeader: Header, institutionHeader: Header): JavaFuture[RecordMetadata] = {
    val producerRecord = new ProducerRecord[String, String](reportingTopic, 0, report.id, report.toString, List(recordSetHeader, institutionHeader).asJava)
42
43
44
    producer.send(producerRecord)
  }

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
45
46
47
48
49
50
51
  def close(): Unit = producer.close()

}

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