google-app-engine - 使用Objectify捕获IllegalStateException

标签 google-app-engine kotlin google-cloud-platform objectify ktor

我使用Objectify 6.0.5,Ktor 1.2.6,com.google.appengine:appengine:1.9.60

我通过教程https://github.com/objectify/objectify/wiki/Setup设置了web.xml,bootstrapper

web.xml

    ...
    <listener>
        <listener-class>com.group.Bootstrapper</listener-class>
    </listener>

    <filter>
        <filter-name>ObjectifyFilter</filter-name>
        <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>ObjectifyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    ...

助推器
class Bootstrapper : ServletContextListener {
    override fun contextInitialized(sce: ServletContextEvent?) {
        println("         contextInitialized")
        ObjectifyService.init(
            ObjectifyFactory()
        )

        ObjectifyService.register(User::class.java)
    }

    override fun contextDestroyed(sce: ServletContextEvent?) {}
}

当我调用此方法时
    fun save(entity: T) {
        ofy().save().entity(entity)
    }

我发现错误
2020-01-05 17:55:09 ERROR Application:104 - Unhandled: GET - /test
java.lang.IllegalStateException: You must call ObjectifyService.init() before using Objectify
    at com.google.common.base.Preconditions.checkState(Preconditions.java:511) ~[guava-28.1-android.jar:?]
    at com.googlecode.objectify.ObjectifyService.factory(ObjectifyService.java:34) ~[objectify-6.0.5.jar:?]
    at com.googlecode.objectify.ObjectifyService.ofy(ObjectifyService.java:51) ~[objectify-6.0.5.jar:?]
    at com.group.dao.BaseDao.listAll(BaseDao.kt:15) ~[classes/:?]
    at com.group.ApplicationKt$module$2$2.invokeSuspend(Application.kt:50) ~[classes/:?]
    at com.group.ApplicationKt$module$2$2.invoke(Application.kt) ~[classes/:?]
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:268) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:141) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:161) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
    at io.ktor.routing.Routing.executeResult(Routing.kt:147) ~[ktor-server-core-1.2.6.jar:1.2.6]
...

我使用“gcloud beta模拟器数据存储启动”来运行数据存储。它肯定运行。可能是无法连接吗?

当我使用appengineRun运行应用程序时,是否还运行数据存储区?

最佳答案

问题是ktor使用协程。协程默认在线程池中运行,并且ObjectifyFilter仅对于接收请求的线程是 Activity 的。我使用此解决方法,直到找到更好的东西:

get(path) {
    withContext(Dispatchers.Unconfined) {
        val content = page.doGet()
        call.respond(content)
    }
}
Unconfined调度程序在启动协程的同一线程中运行协程,至少直到第一次挂起为止。

关于google-app-engine - 使用Objectify捕获IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59602749/

相关文章:

python - 是否有任何 URL 信息/元数据网络服务 API?

android - 在 ConstraintLayout 中使用 RecyclerView 时,所有 RecyclerView 项都不可见

android-fragments - 如何使用导航组件配置DrawerLayout

google-cloud-platform - 使用自定义 docker 镜像启动 Google Cloud AI Platform Notebooks 的要求

google-app-engine - Appengine - 从 db.Model 转换为 ndb.Model?

google-app-engine - 是否可以在 App Engine 上的请求待处理队列中找到请求?

java - Google App Engine - Memcache - 在这种情况下丢失数据的可能性有多大?

Kotlin:函数声明必须有一个名字

npm - 将 Google Cloud Functions 与 registry.npmjs.org 以外的注册表一起使用

ubuntu - 运行 Google Cloud SDK 时为 "sudo: gcloud: command not found"