Vert.x-具有DataInputStreams的GraphQL订阅

时间:2019-10-15 16:55:28

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

我有通过DataInputStream连接到的第三方代码。第三方代码在生成信息时会不断吐出信息。遇到感兴趣的问题时,我想将其传递给GraphQL订阅

在这种情况下,我不确定如何将第三方代码连接到服务器端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);
    }
  });
}

1 个答案:

答案 0 :(得分:0)

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