public final class AioQuickClient extends Object
public class IntegerClient {
public static void main(String[] args) throws Exception {
IntegerClientProcessor processor = new IntegerClientProcessor();
AioQuickClient aioQuickClient = new AioQuickClient("localhost", 8888, new IntegerProtocol(), processor);
aioQuickClient.start();
processor.getSession().write(1);
processor.getSession().close(false);//待数据发送完毕后关闭
aioQuickClient.shutdown();
}
}
| 构造器和说明 |
|---|
AioQuickClient(String host,
int port,
Protocol<T> protocol,
MessageProcessor<T> messageProcessor)
当前构造方法设置了启动Aio客户端的必要参数,基本实现开箱即用。
|
| 限定符和类型 | 方法和说明 |
|---|---|
AioQuickClient |
bindLocal(String local,
int port)
绑定本机地址、端口用于连接远程服务
|
AioQuickClient |
connectTimeout(int timeout)
客户端连接超时时间,单位:毫秒
|
org.smartboot.socket.transport.TcpAioSession |
getSession() |
AioQuickClient |
setBufferFactory(BufferFactory bufferFactory)
设置内存池的构造工厂。
|
AioQuickClient |
setBufferPagePool(BufferPagePool bufferPool)
设置内存池。
|
<V> AioQuickClient |
setOption(SocketOption<V> socketOption,
V value)
设置Socket的TCP参数配置
AIO客户端的有效可选范围为:
1. |
AioQuickClient |
setReadBufferFactory(VirtualBufferFactory readBufferFactory) |
AioQuickClient |
setReadBufferSize(int size)
设置读缓存区大小
|
AioQuickClient |
setWriteBuffer(int bufferSize,
int bufferCapacity)
设置输出缓冲区容量
|
void |
shutdown()
停止客户端服务.
|
void |
shutdownNow()
立即关闭客户端
|
AioSession |
start()
启动客户端。
|
<A> void |
start(A attachment,
CompletionHandler<AioSession,? super A> handler)
采用异步的方式启动客户端
|
AioSession |
start(AsynchronousChannelGroup asynchronousChannelGroup)
启动客户端。
|
<A> void |
start(AsynchronousChannelGroup asynchronousChannelGroup,
A attachment,
CompletionHandler<AioSession,? super A> handler)
采用异步的方式启动客户端
|
public AioQuickClient(String host, int port, Protocol<T> protocol, MessageProcessor<T> messageProcessor)
host - 远程服务器地址port - 远程服务器端口号protocol - 协议编解码messageProcessor - 消息处理器public <A> void start(A attachment,
CompletionHandler<AioSession,? super A> handler)
throws IOException
A - 附件对象类型attachment - 可传入回调方法中的附件对象handler - 异步回调IOExceptionpublic <A> void start(AsynchronousChannelGroup asynchronousChannelGroup, A attachment, CompletionHandler<AioSession,? super A> handler) throws IOException
A - 附件对象类型asynchronousChannelGroup - 通信线程资源组attachment - 可传入回调方法中的附件对象handler - 异步回调IOExceptionpublic AioSession start(AsynchronousChannelGroup asynchronousChannelGroup) throws IOException
在与服务端建立连接期间,该方法处于阻塞状态。直至连接建立成功,或者发生异常。
该start方法支持外部指定AsynchronousChannelGroup,实现多个客户端共享一组线程池资源,有效提升资源利用率。
asynchronousChannelGroup - IO事件处理线程组IOException - IOExceptionAsynchronousSocketChannel.connect(SocketAddress)public org.smartboot.socket.transport.TcpAioSession getSession()
public AioSession start() throws IOException
本方法会构建线程数为2的asynchronousChannelGroup,并通过调用start(AsynchronousChannelGroup)启动服务。
IOException - IOExceptionstart(AsynchronousChannelGroup)public void shutdown()
调用该方法会触发AioSession的close方法,并且如果当前客户端若是通过执行start()方法构建的,同时会触发asynchronousChannelGroup的shutdown动作。
public void shutdownNow()
public AioQuickClient setReadBufferSize(int size)
size - 单位:bytepublic <V> AioQuickClient setOption(SocketOption<V> socketOption, V value)
AIO客户端的有效可选范围为:
1. StandardSocketOptions.SO_SNDBUF
2. StandardSocketOptions.SO_RCVBUF
3. StandardSocketOptions.SO_KEEPALIVE
4. StandardSocketOptions.SO_REUSEADDR
5. StandardSocketOptions.TCP_NODELAY
V - 泛型socketOption - 配置项value - 配置值public AioQuickClient bindLocal(String local, int port)
local - 若传null则由系统自动获取port - 若传0则由系统指定public AioQuickClient setBufferPagePool(BufferPagePool bufferPool)
bufferPool - 内存池对象public AioQuickClient setBufferFactory(BufferFactory bufferFactory)
bufferFactory - 内存池工厂public AioQuickClient setWriteBuffer(int bufferSize, int bufferCapacity)
bufferSize - 单个内存块大小bufferCapacity - 内存块数量上限public AioQuickClient connectTimeout(int timeout)
timeout - 超时时间public final AioQuickClient setReadBufferFactory(VirtualBufferFactory readBufferFactory)
Copyright © 2023. All rights reserved.