【问题标题】:Vert.x - GraphQL Subscriptions with DataInputStreamsVert.x - 带有 DataInputStreams 的 GraphQL 订阅
【发布时间】:2020-02-12 09:58:00
【问题描述】:

我有通过 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);
    }
  });
}

【问题讨论】:

标签: java graphql vert.x graphql-java graphql-subscriptions


【解决方案1】:

好的,我使用 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);

【讨论】:

    猜你喜欢
    • 2020-10-18
    • 2020-08-17
    • 2019-01-18
    • 2019-05-23
    • 2019-01-27
    • 2019-01-27
    • 2017-09-19
    • 2019-12-02
    • 2021-10-09
    相关资源
    最近更新 更多