Skip to content

Instantly share code, notes, and snippets.

@irajhedayati
Created July 28, 2022 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irajhedayati/caf491b6c191cc6e72351df945fe55d7 to your computer and use it in GitHub Desktop.
Save irajhedayati/caf491b6c191cc6e72351df945fe55d7 to your computer and use it in GitHub Desktop.
The main application to run Pub/Sub application which supports local emulator
object Main {
def main(args: Array[String]): Unit = {
Using.resource(getSubscriber(args)) { subscriber =>
subscriber.startAsync.awaitRunning()
subscriber.awaitTerminated()
}
}
def getSubscriber(args: Array[String]): SubscriberWrapper = {
val projectId = args(0)
val subscription = args(1)
val topic = args(2)
val localPubSubEndpoint = if (args.length == 4) Option(args(3)) else None
val publisher = PublisherFactory(projectId, topic, localPubSubEndpoint)
val processor = new Processor(publisher)
SubscriberWrapper(projectId, subscription, localPubSubEndpoint, processor)
}
class Processor(publisher: Publisher) extends MessageReceiver {
override def receiveMessage(message: PubsubMessage, consumer: AckReplyConsumer): Unit = {
println(message.getData.toStringUtf8)
publisher.publish(message)
consumer.ack()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment