Module bus.socket

Class AioClient

java.lang.Object
org.miaixz.bus.socket.accord.AioClient

public class AioClient extends Object
AIO实现的客户端服务
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • AioClient

      public AioClient(String host, int port, Message<T> message, Handler<T> handler)
      当前构造方法设置了启动Aio客户端的必要参数,基本实现开箱即用。
      Parameters:
      host - 远程服务器地址
      port - 远程服务器端口号
      message - 协议编解码
      handler - 消息处理器
  • Method Details

    • start

      public <A> void start(A attachment, CompletionHandler<Session,? super A> handler) throws IOException
      采用异步的方式启动客户端
      Type Parameters:
      A - 附件对象类型
      Parameters:
      attachment - 可传入回调方法中的附件对象
      handler - 异步回调
      Throws:
      IOException
    • start

      public <A> void start(AsynchronousChannelGroup asynchronousChannelGroup, A attachment, CompletionHandler<Session,? super A> handler) throws IOException
      采用异步的方式启动客户端
      Type Parameters:
      A - 附件对象类型
      Parameters:
      asynchronousChannelGroup - 通信线程资源组
      attachment - 可传入回调方法中的附件对象
      handler - 异步回调
      Throws:
      IOException
    • start

      public Session start(AsynchronousChannelGroup asynchronousChannelGroup) throws IOException
      启动客户端。 在与服务端建立连接期间,该方法处于阻塞状态。直至连接建立成功,或者发生异常。 该start方法支持外部指定AsynchronousChannelGroup,实现多个客户端共享一组线程池资源,有效提升资源利用率。
      Parameters:
      asynchronousChannelGroup - IO事件处理线程组
      Returns:
      建立连接后的会话对象
      Throws:
      IOException - IOException
      See Also:
    • getSession

      public TcpSession getSession()
    • start

      public Session start() throws IOException
      启动客户端。 本方法会构建线程数为2的asynchronousChannelGroup,并通过调用start(AsynchronousChannelGroup)启动服务。
      Returns:
      建立连接后的会话对象
      Throws:
      IOException - IOException
      See Also:
    • shutdown

      public void shutdown()
      停止客户端服务. 调用该方法会触发Session的close方法,并且如果当前客户端若是通过执行start()方法构建的,同时会触发asynchronousChannelGroup的shutdown动作。
    • shutdownNow

      public void shutdownNow()
      立即关闭客户端
    • setReadBufferSize

      public AioClient setReadBufferSize(int size)
      设置读缓存区大小
      Parameters:
      size - 单位:byte
      Returns:
      当前AioClient对象
    • setOption

      public <V> AioClient setOption(SocketOption<V> socketOption, V value)
      设置Socket的TCP参数配置

      AIO客户端的有效可选范围为: 1. StandardSocketOptions.SO_SNDBUF 2. StandardSocketOptions.SO_RCVBUF 3. StandardSocketOptions.SO_KEEPALIVE 4. StandardSocketOptions.SO_REUSEADDR 5. StandardSocketOptions.TCP_NODELAY

      Type Parameters:
      V - 泛型
      Parameters:
      socketOption - 配置项
      value - 配置值
      Returns:
      当前客户端实例
    • bindLocal

      public AioClient bindLocal(String local, int port)
      绑定本机地址、端口用于连接远程服务
      Parameters:
      local - 若传null则由系统自动获取
      port - 若传0则由系统指定
      Returns:
      当前客户端实例
    • setBufferPagePool

      public AioClient setBufferPagePool(BufferPagePool bufferPool)
      设置内存池。 通过该方法设置的内存池,在AioClient执行shutdown时不会触发内存池的释放。 该方法适用于多个AioServer、AioClient共享内存池的场景。 在启用内存池的情况下会有更好的性能表现
      Parameters:
      bufferPool - 内存池对象
      Returns:
      当前客户端实例
    • setBufferPagePool

      public AioClient setBufferPagePool(BufferPagePool readBufferPool, BufferPagePool writeBufferPool)
    • setWriteBuffer

      public AioClient setWriteBuffer(int bufferSize, int bufferCapacity)
      设置输出缓冲区容量
      Parameters:
      bufferSize - 单个内存块大小
      bufferCapacity - 内存块数量上限
      Returns:
      当前客户端实例
    • connectTimeout

      public AioClient connectTimeout(int timeout)
      客户端连接超时时间,单位:毫秒
      Parameters:
      timeout - 超时时间
      Returns:
      当前客户端实例
    • disableLowMemory

      public AioClient disableLowMemory()
      禁用低代码模式
      Returns: