/pubsub4s

A wrapper of Google PubSub for Scala

Primary LanguageScalaMIT LicenseMIT

PubSub4SBanner


Google PubSub4S


CircleCI

A wrapper of Google PubSub for Scala

Usage


Library

libraryDependencies += "knoldus" % "pubsub4s" % "0.1.1-SNAPSHOT"

Create Publisher

val topicName = TopicName.of("<PROJECT-ID>", "<TOPIC-NAME>")

val credentialFile = "[JSON-FILE]"

val googleCredentials = GoogleCredentials.fromStream(new FileInputStream(new File(credentialFile)))

val credentialsProvider = FixedCredentialsProvider.create(googleCredentials)

val publisher = PublisherBuilder.make(topicName).withCredentials(credentialsProvider).build

Creating PubSubMessage to Publish

val pubSubMessage = PubSubMessageBuilder.make.withMessageId("message-id").withMessage("my-message").build

val messageId = publisher.publish(pubSubMessage)

Creating Subscriber

val projectSubscriptionName = ProjectSubscriptionName.of("<PROJECT-ID>", "<SUBSCRIPTION-ID>")
 
val receiver: Receiver  = (msg: PubSubMessage, consumer: AckReplyConsumer) => {
    println(s"Message Received: ${msg.getMessageId} - ${msg.getDataString}")
    consumer.ack()
}
 
val subscriber: Subscriber = SubscriberBuilder.make(projectSubscriptionName, receiver).build

subscriber.startAsync()

subscriber.stopAsync()

subscriber.awaitTerminated()

© 2022 Knoldus Inc.