Search in sources :

Example 1 with SocketRuntimeException

use of cn.hutool.socket.SocketRuntimeException in project hutool by looly.

the class AioClient method createChannel.

// ------------------------------------------------------------------------------------- Private method start
/**
 * 初始化
 *
 * @param address 地址和端口
 * @param poolSize 线程池大小
 * @return this
 */
private static AsynchronousSocketChannel createChannel(InetSocketAddress address, int poolSize) {
    AsynchronousSocketChannel channel;
    try {
        AsynchronousChannelGroup group = // 
        AsynchronousChannelGroup.withFixedThreadPool(// 默认线程池大小
        poolSize, // 
        ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build());
        channel = AsynchronousSocketChannel.open(group);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
    try {
        channel.connect(address).get();
    } catch (InterruptedException | ExecutionException e) {
        IoUtil.close(channel);
        throw new SocketRuntimeException(e);
    }
    return channel;
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) IORuntimeException(cn.hutool.core.io.IORuntimeException) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SocketRuntimeException(cn.hutool.socket.SocketRuntimeException)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)1 SocketRuntimeException (cn.hutool.socket.SocketRuntimeException)1 IOException (java.io.IOException)1 AsynchronousChannelGroup (java.nio.channels.AsynchronousChannelGroup)1 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)1 ExecutionException (java.util.concurrent.ExecutionException)1