How to use google pubsub library with scala

Multi tool use
Multi tool use


How to use google pubsub library with scala



I'm writing a Google pubsub client using the Java API, the client is written in Scala. The problem of this code is that it's not idiomatic in scala with the use of null and the while true loop


null


while true


val receiver = new MessageReceiver() {
// React to each received message
// If there are any
override def receiveMessage(message: PubsubMessage, consumer: AckReplyConsumer): Unit = { // handle incoming message, then ack/nack the received message
System.out.println("Id : " + message.getMessageId)
System.out.println("Data : " + message.getData.toStringUtf8)
throw new RuntimeException("This is just an exception")
consumer.ack()
}
}

var subscriber: ApiService = null
try { // Create a subscriber for "my-subscription-id" bound to the message receiver
var subscriber = Subscriber.newBuilder(subscriptionName, receiver).build
subscriber.startAsync
// ...
} finally {
// stop receiving messages
if (subscriber != null) subscriber.stopAsync()
}
while (true) {
Thread.sleep(1000)
}



How do I tranform this code in order to use scala's Future or cats IO


IO





To make it more Scala-like, start by not using mutable variables. It is bad practice to use mutable variables in Scala. Also, have you tried to do this yourself? What have you attempted so far?
– James Whiteley
Jul 2 at 14:20




1 Answer
1



Have you considered using Lightbend's Alpakka Google Cloud Pub/Sub connector?
https://developer.lightbend.com/docs/alpakka/current/google-cloud-pub-sub.html



It works very well and is idiomatic






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

KrrO9fOx4SCA,JhgTA
gWj,QUxpMJ17AWFKZHc,R,kUTwuRB,m RRuvoq 70pfM

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications