KProducer.scala 1.64 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
2
/*
 * Import Process Delete
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
3
 * Copyright (C) 2021 Memoriav
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * 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/>.
 */

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
19
20
21
22
23
24
25
26
27
28
29
30
package ch.memobase

import ch.memobase.models.DeleteMessage
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import org.apache.logging.log4j.scala.Logging


abstract class KProducer {
  self: AppSettings with Logging =>

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

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
31
  def sendDelete(id: DeleteMessage, dryRun: Boolean): Unit = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
32
    logger.debug(s"Sending delete command for ${id.recordId}")
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
33
    val producerRecord = new ProducerRecord[String, String](outputTopic, id.recordId, null)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
34
35
36
37
    producerRecord.headers()
      .add("recordSetId", id.recordSetId.getBytes())
      .add("institutionId", id.institutionId.getBytes())
      .add("sessionId", id.sessionId.getBytes())
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
38
      .add("dryRun", List((if (dryRun) 1 else 0).toByte).toArray)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
39
40
41
42
43
44
45
46
    producer.send(producerRecord)
  }

  def closeProducer(): Unit = {
    logger.debug("Closing Kafka producer instance")
    producer.close()
  }
}