java Intellij Idea插件开发中的ktor客户端出现问题?

zqdjd7g9  于 2023-03-11  发布在  Java
关注(0)|答案(1)|浏览(242)

我正在为IntelliJ Idea开发一个插件我想使用Ktor客户端来使用websocket功能

val client = HttpClient(CIO) {
    install(WebSockets)
}

当我发送WebSocket请求时

client.webSocket(...)

将引发此异常

java.lang.LinkageError: loader constraint violation: when resolving method 'org.slf4j.ILoggerFactory org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()' the class loader com.intellij.ide.plugins.cl.PluginClassLoader @a02df19 of the current class, org/slf4j/LoggerFactory, and the class loader com.intellij.util.lang.PathClassLoader @2ac1fdc4 for the method's defining class, org/slf4j/impl/StaticLoggerBinder, have different Class objects for the type org/slf4j/ILoggerFactory used in the signature (org.slf4j.LoggerFactory is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader @a02df19, parent loader 'bootstrap'; org.slf4j.impl.StaticLoggerBinder is in unnamed module of loader com.intellij.util.lang.PathClassLoader @2ac1fdc4)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:423)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:362)
    at io.ktor.util.logging.KtorSimpleLoggerJvmKt.KtorSimpleLogger(KtorSimpleLoggerJvm.kt:10)
    at io.ktor.client.plugins.websocket.WebSocketsKt.<clinit>(WebSockets.kt:22)
    at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invokeSuspend(WebSockets.kt:162)
    at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invoke(WebSockets.kt)
    at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invoke(WebSockets.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:88)
    at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:126)
    at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
    at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invokeSuspend(HttpRequestLifecycle.kt:35)
    at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:98)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
    at io.ktor.client.HttpClient.execute$ktor_client_core(HttpClient.kt:191)
    at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:108)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket(builders.kt:243)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket(builders.kt:119)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket(builders.kt:137)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket$default(builders.kt:132)
    at ir.amirab.debugboard.ideaplugin.Backend$initiateWebsocket$1.invokeSuspend(Backend.kt:70)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
java.lang.NoClassDefFoundError: Could not initialize class io.ktor.client.plugins.websocket.WebSocketsKt
    at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invokeSuspend(WebSockets.kt:162)
    at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invoke(WebSockets.kt)
    at io.ktor.client.plugins.websocket.WebSockets$Plugin$install$1.invoke(WebSockets.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:88)
    at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:126)
    at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.client.plugins.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
    at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invokeSuspend(HttpRequestLifecycle.kt:35)
    at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.client.plugins.HttpRequestLifecycle$Plugin$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:120)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:78)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute$ktor_utils(SuspendFunctionGun.kt:98)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:77)
    at io.ktor.client.HttpClient.execute$ktor_client_core(HttpClient.kt:191)
    at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:108)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket(builders.kt:243)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket(builders.kt:119)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket(builders.kt:137)
    at io.ktor.client.plugins.websocket.BuildersKt.webSocket$default(builders.kt:132)
    at ir.amirab.debugboard.ideaplugin.Backend$initiateWebsocket$1.invokeSuspend(Backend.kt:70)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

我也测试了ktor服务器。它也给予了我同样的异常!我的问题是

  • 在idea插件中使用ktor是否有效?
  • 如果可以的话,我该怎么做才能解决这个问题?

我用了这些版本

kotlinVersion = "1.8.0"
ideaPluginVersion = "1.13.1"
ktorVersion = "2.2.2"
更新:

当我添加ktor依赖项时

org.slf4j.LoggerFactory.getILoggerFactory()

即使我根本不使用ktor,这段代码也会抛出那个异常!

sc4hvdpw

sc4hvdpw1#

在我看来,在idea插件中使用ktor客户端是可能的。
我能够运行插件内的ktor客户端WebSocket没有任何问题,它读取和记录传入的消息这里是我的代码:

val ktorClient = HttpClient(CIO) {
    install(WebSockets)
}

class PluginAction : DumbAwareAction() {

    override fun actionPerformed(event: AnActionEvent) {

        CoroutineScope(SupervisorJob() + Dispatchers.IO).launch {

            ktorClient.webSocket(
                method = HttpMethod.Get,
                host = "localhost",
                path = "/path",
            ) {
                while (true) {
                    val incoming = (incoming.receive() as Frame.Text).readText()
                    thisLogger().info("incoming: $incoming")
                }
            }
        }
        ktorClient.close()
    }
}

build.gradle.kts:

val ktorVersion = "2.2.2"

dependencies {
    implementation("io.ktor:ktor-client-core:$ktorVersion")
    implementation("io.ktor:ktor-client-cio:$ktorVersion")
    implementation("io.ktor:ktor-client-websockets:$ktorVersion")
}

根据日志io.ktor.server.engine.ApplicationEngineEnvironmentBuilder判断,您尝试使用由于缺少设置而崩溃的ktor服务器模块。
基于你的问题,你需要的正是客户。
你也错过了引擎,我用СIO
val ktorClient = HttpClient(CIO) {...}
另见:
Setup ktor websocket client
Client webSockets code example

相关问题