java - Vert.x - 带有 DataInputStreams 的 GraphQL 订阅
问题描述
我有通过 DataInputStream 连接到的第 3 方代码。第 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);
推荐阅读
- image-processing - 我们如何使用图像处理或计算机视觉获得两种相似颜色之间的边界线?
- python - 编写一个用于接收密码的程序,该程序告诉用户它的长度,以及它是否太大、太小或是否太强
- java - 为什么我的 if input.equals("Quit") 没有正确退出?
- laravel - Octobercms 部分未更新
- ruby-on-rails - ROR:我可以添加一些逻辑来选择数据并在 .rake 文件上进行循环吗?
- vba - 网页加载时间长 Selenium VBA
- c# - Entity Framework 6.44 的这段代码在哪里执行?
- node.js - 解析服务器中的 Node.js,空值?
- python - 如何从自定义 MariaDB 连接器检索模板中的数据?(姜戈)
- javascript - 当我尝试从 React 中的道具访问对象时,无法使用 map 函数访问它