gpt4 book ai didi

java - Vert.x - 使用 DataInputStreams 的 GraphQL 订阅

转载 作者:行者123 更新时间:2023-12-01 23:06:23 24 4
gpt4 key购买 nike

我有通过 DataInputStream 连接到的第 3 方代码。第三方代码在生成信息时不断地输出信息。当遇到感兴趣的东西时,我想将其传递给 GraphQL 订阅

在这种情况下,我不确定如何将第 3 方代码连接到服务器端 GraphQL 订阅代码。任何建议,将不胜感激。

一些概念代码如下:

public void liveStream(DataInputStream in) {
// Sit and constantly watch input stream and report when messages come in
while(true) {
SomeMessage message = readFromInputStream(in);
System.out.println("Received Message Type:" + message.getType());

// Convert SomeMessage into the appropriate class based on its type
if (message.getType() == "foo") {
Foo foo = convertMessageToFoo(message);
} else if (message.getType() == "bar") {
Bar bar = convertMessageToBar(message);
} else if (howeverManyMoreOfThese) {
// Keep converting to different objects
}
}
}

// The client code will eventually trigger this method when
// the GraphQL Subscription query is sent over
VertxDataFetcher<Publisher<SomeClassTBD>> myTestDataFetcher() {
return new VertxDataFetcher<> (env, future) -> {
try {
future.complete(myTest());
} catch(Exception e) {
future.fail(e);
}
});
}

最佳答案

好的,我使用 executorService 将我的 liveStream 代码包装在 ObservableOnSubscribe 中,并且正在取回所有数据。我想我现在可以直接将其传递到前端,或者创建单独的发布者来处理特定的对象类型,并使 graphql 订阅指向各自的发布者。

ExecutorService executor = Executors.newSingleThreadExecutor;

ObservableOnSubscribe<SomeClassTBD> handler = emitter ->
executor.submit(() -> {
try {
//liveStream code here
emitter.onComplete();
}
catch(Exception e) {
emitter.onError(e);
}
finally {
// Cleanup here
}
});
Observable<SomeClassTBD> = Observable.create(handler);

关于java - Vert.x - 使用 DataInputStreams 的 GraphQL 订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com