Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
rxjava中的socketwatchdog_Java_Sockets_Tcp_Reactive Programming_Rx Java - Fatal编程技术网

rxjava中的socketwatchdog

rxjava中的socketwatchdog,java,sockets,tcp,reactive-programming,rx-java,Java,Sockets,Tcp,Reactive Programming,Rx Java,我目前正在努力使用rx实现tcp看门狗/重试系统,非常感谢您的帮助 有了一个可观察的,我想有一个可观察的结果,定期检查我们是否仍然可以写入套接字。很简单,我可以做这样的事情: class SocketSubscribeFunc implements Observable.OnSubscribeFunc<Socket> { private final String hostname; private final int port; private Socket socket

我目前正在努力使用rx实现tcp看门狗/重试系统,非常感谢您的帮助

有了一个可观察的,我想有一个可观察的结果,定期检查我们是否仍然可以写入套接字。很简单,我可以做这样的事情:

class SocketSubscribeFunc implements Observable.OnSubscribeFunc<Socket> {
  private final String hostname;
  private final int port;
  private Socket socket;

  SocketSubscribeFunc(String hostname, int port) {
    this.hostname = hostname;
    this.port = port;
  }

  public Subscription onSubscribe(final Observer<? super Socket> observer) {
    try {
      log.debug("Trying to connect...");
      socket = new Socket(hostname, port);
      observer.onNext(socket);
    } catch (IOException e) {
      observer.onError(e);
    }
    return new Subscription() {
      public void unsubscribe() {
        try {
          socket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    };
  }
}

Observable<Socket> socketObservable = Observable.create(new SocketSubscribeFunc(hostname,port));
Observable<Boolean> watchdog = Observable.combineLatest(socketObservable, Observable.interval(1, TimeUnit.SECONDS), new Func2<Socket, Long, Boolean>() {

  public Boolean call(final Socket socket, final Long aLong) {
    try {
      socket.getOutputStream().write("ping\n".getBytes());
      return true;
    } catch (IOException e) {
     return false;
    }
  }
});
类SocketSubscribeFunc实现Observable.OnSubscribeFunc{
私有最终字符串主机名;
私人最终国际港口;
专用插座;
SocketSubscribeFunc(字符串主机名,int端口){
this.hostname=主机名;
this.port=端口;
}

公开认购(最终观察者首先,我要避免
可观察。大多数情况下,创建
,因为它通常是不需要的,并且引入了不必要的复杂性。在这种情况下,Rx有一个名为
的操作符,使用
,它允许您创建一个在可观察者生命周期内存在的资源对象。它会自动捕获运行时错误,并且提供了一个dispose操作,因此这对于本用例中的套接字来说是完美的

Observable.using(
    // Resource (socket) factory
    () -> {
      try {
        return new Socket(hostname, port);
      } catch (IOException e) {
        // Rx will propagate this as an onError event.
        throw new RuntimeException(e);
      }
    },
    // Observable factory
    (socket) -> {
      return Observable.interval(1, TimeUnit.SECONDS)
          .map((unusedTick) {
            try {
              socket.getOutputStream().write("ping\n".getBytes());
              return true;
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
          })
          // Retry the inner job up to 3 times before propagating.
          .retry(3);
    },
    // Dispose action for socket.
    // In real life the close probably needs a try/catch.
    (socket) -> socket.close())
    // Retry the outer job up to 3 times.
    .retry(3)
    // If we propagate all errors, emit a 'false', signaling service is not available.
    .onErrorResumeNext(Observable.just(false));
请注意,如果内部作业传播(3次失败后),这将重试外部作业。要修复此问题,您应该在重试时使用谓词以及retryWhen签出文档。您可以引发特殊的RuntimeException,并且仅在外部作业不是由内部作业传播的类型时重试外部作业

使用
文档: