我有一个生成器 g,一旦我从控制台运行它,它就开始将数据写入控制台输出(stdout),然后休眠 x 秒并继续,形成了一串数据。
我想让我的程序运行 g 并将其输出绑定到 Java Vertx 应用程序作为流输入。
我希望所有的读取都是异步完成,我该如何实现?
这是我正在做的事情:
public class InputHandler extends AbstractVerticle {
final String command = "path";
@Override
public void start() throws Exception {
Runtime r = Runtime.getRuntime();
Process p; // Process tracks one external native process
BufferedReader is; // reader for output of process
String line;
p = r.exec(command);
System.out.println("In Main after exec");
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = is.readLine()) != null)
try {
System.out.println(line);
}catch (Exception ex){
System.out.println(ex);
}
}
}
并且这是被抛出的异常:
io.vertx.core.VertxException: Thread blocked
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:255)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at io.vertx.example.InputHandler.start(InputHandler.java:27)
at io.vertx.core.AbstractVerticle.start(AbstractVerticle.java:106)
at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$8(DeploymentManager.java:483)
at io.vertx.core.impl.DeploymentManager$$Lambda$8/884603232.handle(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339)
at io.vertx.core.impl.ContextImpl$$Lambda$9/154173878.run(Unknown Source)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)