首先,我对Akka没有经验,所以我自己调试这个很糟糕。我尝试了here中的示例,并且发布消息有效(这意味着凭据有效),但是没有消息被发出。服务帐户被授予所有权限。
我的代码看起来像这样,基本上与示例中的完全相同:
def coordinator_students(request):
FSJ_user = get_FSJ_user(request.user.username)
student_list = Student.objects.all().order_by('ccid')
filtered_list = StudentFilter(request.GET, queryset=student_list)
student_paginator = Paginator(filtered_list.qs, 25)
template = loader.get_template("FSJ/coord_student_list.html")
context = get_standard_context(FSJ_user)
context["student_list"] = student_list
page = request.GET.get('page', 1)
try:
students = student_paginator.page(page)
except PageNotAnInteger:
students = student_paginator.page(1)
except EmptyPage:
students = student_paginator.page(student_paginator.num_pages)
context["filter"] = filtered_list
context["students"] = students
return HttpResponse(template.render(context, request))
我发现package com.example.google.pubsub
import java.io.FileInputStream
import java.security.spec.PKCS8EncodedKeySpec
import java.security.{KeyFactory, PrivateKey}
import java.util.Base64
import akka.actor.ActorSystem
import akka.stream.{ActorMaterializer, Attributes, Outlet, SourceShape}
import akka.stream.alpakka.googlecloud.pubsub.scaladsl.GooglePubSub
import akka.stream.alpakka.googlecloud.pubsub._
import akka.stream.scaladsl.{Sink, Source}
import akka.stream.stage.{GraphStage, GraphStageLogic}
import akka.{Done, NotUsed}
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import scala.concurrent.duration._
import scala.concurrent.Future
object SubscriberMain extends App {
println("#### SUBSCRIBER ####")
val privateKey: PrivateKey = {
import java.io.FileInputStream
val credential = GoogleCredential.fromStream(new FileInputStream("mycredentials.json"))
val privateKey = credential.getServiceAccountPrivateKey
privateKey
}
val clientEmail = "main-19@weirdproject.iam.gserviceaccount.com"
val projectId = "weirdproject"
val apiKey = "xxxx"
val subscription = "somesubscription"
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
val subscriptionSource: Source[ReceivedMessage, NotUsed] =
GooglePubSub.subscribe(projectId, apiKey, clientEmail, privateKey, subscription)
val ackSink: Sink[AcknowledgeRequest, Future[Done]] =
GooglePubSub.acknowledge(projectId, apiKey, clientEmail, privateKey, subscription)
subscriptionSource
.map { message =>
val data = message.message.data
println(s"received a message: $data")
message.ackId
}
.groupedWithin(1000, 1.minute)
.map(AcknowledgeRequest.apply)
.to(ackSink)
}
永远不会被执行,这似乎是没有提取任何消息的原因。
答案 0 :(得分:2)
您拥有的是流的定义,但您没有运行它。致电run()
:
subscriptionSource
.map { message =>
val data = message.message.data
println(s"received a message: $data")
message.ackId
}
.groupedWithin(1000, 1.minute)
.map(AcknowledgeRequest.apply)
.to(ackSink)
.run() // <---
或者,使用runWith()
,这是一种方便的方法,可以返回Sink
的具体化值:
val result: Future[Done] =
subscriptionSource
.map { message =>
val data = message.message.data
println(s"received a message: $data")
message.ackId
}
.groupedWithin(1000, 1.minute)
.map(AcknowledgeRequest.apply)
.runWith(ackSink)
有关定义和运行流的更多信息,请here。