4

我正在使用 grpc 在 Java 服务器和节点客户端之间进行通信。当他们中的任何一个死亡/崩溃时,另一个死去。

这是节点客户端死机时在 Java 服务器上引发的异常 -

Nov 08, 2016 11:28:03 AM io.grpc.netty.NettyServerHandler onConnectionError
WARNING: Connection Error
java.io.IOException: An existing connection was forcibly closed by the remote host
    at sun.nio.ch.SocketDispatcher.read0(Native Method)
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:192)
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380)
    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:349)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:112)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:571)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:512)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:426)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:398)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:877)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
    at java.lang.Thread.run(Thread.java:745)

这是 java 死亡时节点上抛出的异常:

events.js:141
      throw er; // Unhandled 'error' event
      ^
Error: {"created":"@1478623914.082000000","description":"An existing connection was forcibly closed by the remote     host.\r\n","file":"..\src\core\lib\iomgr\tcp_windows.c","file_line":171,"grpc_status":14}    
    at ClientReadableStream._emitStatusIfDone (    C:\HarshalDev\Live_TDFX\TDSL0007-TDFXPlatfo    rmsDesignandBuild\tdfxlive\node_modules\grpc\src\node\src\client.js:189:19)
    at ClientReadableStream._receiveStatus (C:\    HarshalDev\Live_TDFX\TDSL0007-TDFXPlatformsDesignandBuild\tdfxlive\node_modules\grpc\src\node\src\client.js:169:8)
    at C:\HarshalDev\Live_TDFX\TDSL0007-TDFXPlatformsDesignandBuild\tdfxlive\node_modules\grpc\src\node\src\client.js:577:14
[nodemon] app crashed - waiting for file changes before starting...

问题 - 我如何处理这些异常?

我尝试添加 try/catch 并为线程添加未捕获的异常处理程序,但没有成功。

要初始化的 Java 代码 -

ServerBuilder<?> serverBuilder = ServerBuilder.forPort(getPort());
server = serverBuilder
        .addService(createBwayStreamingService())
        .addService(ServerInterceptors.intercept(createBwayOrderService(), authServerInterceptor))
        .addService(createBwayInstrumentService())
        .addService(createBwaySettlementService())
        .addService(createBwayDateTimeService())
        .addService(ServerInterceptors.intercept(createBwayConfService(), authServerInterceptor))
        .addService(ServerInterceptors.intercept(createBwayTradeService(), authServerInterceptor))
        .addService(ServerInterceptors.intercept(createBwayAuthService(), authServerInterceptor))
        .build();
Preconditions.checkNotNull(server);
server.start();
System.out.println("Server started, listening on " + getPort());
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
        System.out.println("Shutting down gRPC server");
        TocGateway.this.stop();
        System.out.println("Server shut down");
    }
});
server.awaitTermination();

节点客户端处理程序(服务之一,所有其他服务使用相同的模式) -

let protoDescriptorStreaming = grpc.load((process.env.FX_LIVE_PROTO_DIR || '../tocGateway/src/main/proto') + '/streaming.proto');
let streamingService = new protoDescriptorStreaming.tds.fxlive.bway.BwayStreamService(process.env.TOC_GATEWAY_ADDRESS || 'localhost:8087', grpc.credentials.createInsecure());
4

1 回答 1

2

Java 警告是无害的。您的应用程序应在异常发生后继续正常运行。降低日志级别以减少垃圾邮件会很好,但它也不应该影响应用程序的正确性。

于 2016-11-09T00:33:26.330 回答