Reporter.scala 2.24 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
23
24
25
/*
 * 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 java.util.Properties
import java.util.concurrent.{Future => JavaFuture}

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord, RecordMetadata}
26
27
28
import org.apache.kafka.common.header.Header

import scala.collection.JavaConverters._
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
29
30
31
32
33
34
35
36
37
38
39
40


/**
 * 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)

41
  def send(report: models.ReportingObject): JavaFuture[RecordMetadata] = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
42
43
    // val headers = createHeaders(report.institutionRecordSet._2.getOrElse(""), report.institutionRecordSet._1.getOrElse(""))
    val producerRecord = new ProducerRecord[String, String](reportingTopic, 0, report.id, report.toString)
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    producer.send(producerRecord)
  }

  private def createHeaders(recordSetId: String, institutionId: String): java.lang.Iterable[Header] = {
    List(
      new Header {
        override def key(): String = "recordSetId"

        override def value(): Array[Byte] = recordSetId.getBytes
      },
      new Header {
        override def key(): String = "institutionId"

        override def value(): Array[Byte] = institutionId.getBytes
      }
    ).asJava
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
60
61
62
63
64
65
66
67
68
  }

  def close(): Unit = producer.close()

}

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