当打开通道失败时,会抛出一个IOException异常。
1.2 绑定地址和端口
通过调用AsynchronousServerSocketChannel.bind(SocketAddress)方法来绑定监听地址和端口:
//构建一个InetSocketAddress实例以指定监听的地址和端口,如果需要指定ip,则调用InetSocketAddress(ip,port)构造方法创建即可
serverSocketChannel.bind(new InetSocketAddress(port));
1.3 监听和接收客户端连接请求
监听客户端连接请求,主要通过调用AsynchronousServerSocketChannel.accept()方法完成。accept()有两个重载方法:
public abstract void accept(A,CompletionHandler);public abstract Future accept();
这两个重载方法的行为方式完全相同一种基于Future,一种基于回调,事实上,AIO的很多异步API都封装了诸如此类的重载方法:提供CompletionHandle回调参数或者返回一个Future类型变量。用过Feture接口的都知道,可以调用Feture.get()方法阻塞等待调用结果。无论是哪种方式来获取连接,最终的处理对象都是Socket,和ServerSocketChannel不同的是,这里的socket是封装在AsynchronousSocketChannel中的。
基于Future实现:
public voidAsynchronousServerSocketChannel() {try{
AsynchronousServerSocketChannel channel=AsynchronousServerSocketChannel.open();
channel.bind(new InetSocketAddress(8888));while (true) {
Future conn =channel.accept();//阻塞等待直到future有结果
AsynchronousSocketChannel asyncSocketChannel =conn.get();//异步处理连接
asyncHandle(asyncSocketChannel);
}
}catch (IOException | InterruptedException |ExecutionException e) {
e.printStackTrace();
}
}
基于回调:
public voidAsynchronousServerSocketChannelCallback() {try{
AsynchronousServerSocketChannel channel=AsynchronousServerSocketChannel.open();
channel.bind(new InetSocketAddress(8888));
channel.accept(null, new CompletionHandler() {
@Overridepublic voidcompleted(AsynchronousSocketChannel result, Void attachment) {
// 接收到新的客户端连接时调用,result就是和客户端的连接对话,此时可以通过result和客户端进行通信
System.out.println("accept completed");//异步处理连接
asyncHandle(result);//继续监听accept
channel.accept(null, this);
}
@Overridepublic voidfailed(Throwable exc, Void attachment) {
// accept失败时回调
System.out.println("accept failed");
}
});//让主线程保持存活
while (true) {
System.in.read();
}
}catch(IOException e) {
e.printStackTrace();
}
}
需要注意的是,AsynchronousServerSocketChannel是线程安全的,但在任何时候同一时间内只能允许有一个accept操作。因此,必须得等待前一个accept操作完成之后才能启动下一个accept:
serverSocketChannel
.accept(serverSocketChannel,new CompletionHandler
AsynchronousServerSocketChannel>() {
@Overridepublic void completed(finalAsynchronousSocketChannel result,finalAsynchronousServerSocketChannel attachment) {//接收到新的客户端连接,此时本次accept已经完成//继续监听下一个客户端连接到来
serverSocketChannel.accept(serverSocketChannel,this);//result即和该客户端的连接会话//此时可以通过result与客户端进行交互
}
...
});
此外,还可以通过以下方法获取和设置AsynchronousServerSocketChannel的socket选项:
//设置socket选项
serverSocketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE,true);//获取socket选项设置
boolean keepAlive = serverSocketChannel.getOption(StandardSocketOptions.SO_KEEPALIVE);
其中StandardSocketOptions类封装了常用的socket设置选项。
获取本地地址:
InetSocketAddress address = (InetSocketAddress) serverSocketChannel.getLocalAddress();
1.4 AsynchronousChannelGroup异步通道组
try{
ExecutorService pool=Executors.newCachedThreadPool();
AsynchronousChannelGroup group= AsynchronousChannelGroup.withCachedThreadPool(pool, 10);
AsynchronousServerSocketChannel serverSocketChannel=AsynchronousServerSocketChannel.open(group);
}catch(IOException e) {
e.printStackTrace();
}
AsynchronousServerSocketChannel提供了设置通道分组(AsynchronousChannelGroup)的功能,以实现组内通道资源共享。可以调用open(AsynchronousChannelGroup)重载方法创建指定分组的通道,默认情况下,具有 open() 方法的通道属于一个全局通道组,可利用如下系统变量对其进行配置:
java.nio.channels.DefaultThreadPoolthreadFactory,其不采用默认设置,而是定义一个 java.util.concurrent.ThreadFactory