forked from akka/alpakka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prototype of google cloud pub/sub connector using akka-grpc
- Loading branch information
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...le-cloud-pub-sub-grpc/src/main/scala/akka/stream/alpakka/googlecloud/pubsub/GrpcApi.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (C) 2016-2018 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.googlecloud.pubsub | ||
|
||
import akka.stream.Materializer | ||
import com.google.auth.Credentials | ||
import com.google.pubsub.v1.pubsub.{PublisherClient, SubscriberClient} | ||
import io.grpc.auth.MoreCallCredentials | ||
import io.grpc.{CallOptions, ManagedChannelBuilder} | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
private object GrpcApi extends GrpcApi { | ||
final val DefaultPubSubGoogleApisHost = "pubsub.googleapis.com" | ||
} | ||
|
||
private trait GrpcApi { | ||
import GrpcApi._ | ||
|
||
private val channel = ManagedChannelBuilder.forTarget(DefaultPubSubGoogleApisHost).build() | ||
|
||
def publisher(credentials: Credentials)(implicit mat: Materializer, ec: ExecutionContext) = | ||
new PublisherClient(channel, | ||
CallOptions.DEFAULT | ||
.withCallCredentials(MoreCallCredentials.from(credentials))) | ||
|
||
def subscriber(credentials: Credentials)(implicit mat: Materializer, ec: ExecutionContext) = | ||
new SubscriberClient(channel, | ||
CallOptions.DEFAULT | ||
.withCallCredentials(MoreCallCredentials.from(credentials))) | ||
} |
58 changes: 58 additions & 0 deletions
58
...ub-grpc/src/main/scala/akka/stream/alpakka/googlecloud/pubsub/scaladsl/GooglePubSub.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2016-2018 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.googlecloud.pubsub.scaladsl | ||
|
||
import akka.stream.Materializer | ||
import akka.stream.alpakka.googlecloud.pubsub._ | ||
import akka.stream.scaladsl.{Flow, Keep, Sink, Source} | ||
import akka.{Done, NotUsed} | ||
import com.google.auth.Credentials | ||
import com.google.pubsub.v1.pubsub._ | ||
|
||
import scala.collection.immutable | ||
import scala.concurrent.Future | ||
|
||
object GooglePubSub extends GooglePubSub { | ||
private[pubsub] override val grpcApi = GrpcApi | ||
} | ||
|
||
protected[pubsub] trait GooglePubSub { | ||
private[pubsub] def grpcApi: GrpcApi | ||
|
||
/** | ||
* Creates a flow to that publish messages to a topic and emits the message ids | ||
*/ | ||
def publish(creds: Credentials, parallelism: Int = 1)( | ||
implicit materializer: Materializer | ||
): Flow[PublishRequest, immutable.Seq[String], NotUsed] = { | ||
import materializer.executionContext | ||
|
||
Flow[PublishRequest] | ||
.mapAsyncUnordered(parallelism)(grpcApi.publisher(creds).publish) | ||
.map(_.messageIds.toVector) | ||
} | ||
|
||
def subscribe(subscription: String, creds: Credentials)( | ||
implicit materializer: Materializer | ||
): Source[ReceivedMessage, NotUsed] = { | ||
import materializer.executionContext | ||
|
||
val req = StreamingPullRequest(subscription = subscription) | ||
grpcApi | ||
.subscriber(creds) | ||
.streamingPull(Source.single(req)) | ||
.mapConcat(_.receivedMessages.toVector) | ||
.mapMaterializedValue(_ => NotUsed) | ||
} | ||
|
||
def acknowledge(creds: Credentials, | ||
parallelism: Int = 1)(implicit materializer: Materializer): Sink[AcknowledgeRequest, Future[Done]] = { | ||
import materializer.executionContext | ||
|
||
Flow[AcknowledgeRequest] | ||
.mapAsyncUnordered(parallelism)(grpcApi.subscriber(creds).acknowledge) | ||
.toMat(Sink.ignore)(Keep.right) | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...le-cloud-pub-sub-grpc/src/test/scala/akka/stream/alpakka/googlecloud/pubsub/TestApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2016-2018 Lightbend Inc. <http://www.lightbend.com> | ||
*/ | ||
|
||
package akka.stream.alpakka.googlecloud.pubsub | ||
|
||
import akka.actor.ActorSystem | ||
import akka.stream.{ActorMaterializer, Materializer} | ||
import akka.stream.alpakka.googlecloud.pubsub.scaladsl.GooglePubSub | ||
import akka.stream.scaladsl.Source | ||
import com.google.auth.oauth2.GoogleCredentials | ||
import com.google.protobuf.ByteString | ||
import com.google.pubsub.v1.pubsub.{PublishRequest, PubsubMessage} | ||
|
||
import scala.collection.immutable | ||
import scala.concurrent.Await | ||
import scala.concurrent.duration._ | ||
|
||
object TestApp { | ||
def main(args: Array[String]): Unit = { | ||
implicit val sys = ActorSystem() | ||
implicit val mat = ActorMaterializer() | ||
|
||
try { | ||
run() | ||
scala.io.StdIn.readLine() | ||
} finally { | ||
Await.ready(sys.terminate(), 5.seconds) | ||
} | ||
} | ||
|
||
def run()(implicit sys: ActorSystem, mat: Materializer): Unit = { | ||
val requests = Source( | ||
immutable.Seq( | ||
PublishRequest( | ||
"projects/kraziu-tvirtove/topics/testTopic", | ||
Seq( | ||
PubsubMessage(ByteString.copyFromUtf8("hi")), | ||
PubsubMessage(ByteString.copyFromUtf8("my name is")), | ||
PubsubMessage(ByteString.copyFromUtf8("who")) | ||
) | ||
) | ||
) | ||
) | ||
|
||
// Loads credentials from a json file, which is found | ||
// from GOOGLE_APPLICATION_CREDENTIALS env variable | ||
val creds = GoogleCredentials.getApplicationDefault | ||
|
||
requests.via(GooglePubSub.publish(creds)).runForeach(println) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Perhaps we should add this by default from the AkkaGrpcPlugin? (added akka#109)