add ChannelUtil

This commit is contained in:
Looly 2022-05-23 11:36:58 +08:00
parent de75ff8e63
commit 0cbbc69c4f
2 changed files with 74 additions and 34 deletions

View File

@ -0,0 +1,62 @@
package cn.hutool.socket;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
/**
* Channel相关封装
*
* @author looly
* @since 5.8.2
*/
public class ChannelUtil {
/**
* 创建{@link AsynchronousChannelGroup}
*
* @param poolSize 线程池大小
* @return {@link AsynchronousChannelGroup}
*/
public static AsynchronousChannelGroup createFixedGroup(final int poolSize) {
try {
return AsynchronousChannelGroup.withFixedThreadPool(//
poolSize, // 默认线程池大小
ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()//
);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
/**
* 连接到指定地址
*
* @param group {@link AsynchronousChannelGroup}
* @param address 地址信息包括地址和端口
* @return {@link AsynchronousSocketChannel}
*/
public static AsynchronousSocketChannel connect(final AsynchronousChannelGroup group, final InetSocketAddress address) {
final AsynchronousSocketChannel channel;
try {
channel = AsynchronousSocketChannel.open(group);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
try {
channel.connect(address).get();
} catch (final InterruptedException | ExecutionException e) {
IoUtil.close(channel);
throw new SocketRuntimeException(e);
}
return channel;
}
}

View File

@ -1,19 +1,14 @@
package cn.hutool.socket.aio; package cn.hutool.socket.aio;
import cn.hutool.core.io.IORuntimeException; import cn.hutool.socket.ChannelUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.socket.SocketConfig; import cn.hutool.socket.SocketConfig;
import cn.hutool.socket.SocketRuntimeException;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketOption; import java.net.SocketOption;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
/** /**
* Aio Socket客户端 * Aio Socket客户端
@ -21,7 +16,7 @@ import java.util.concurrent.ExecutionException;
* @author looly * @author looly
* @since 4.5.0 * @since 4.5.0
*/ */
public class AioClient implements Closeable{ public class AioClient implements Closeable {
private final AioSession session; private final AioSession session;
@ -112,6 +107,7 @@ public class AioClient implements Closeable{
} }
// ------------------------------------------------------------------------------------- Private method start // ------------------------------------------------------------------------------------- Private method start
/** /**
* 初始化 * 初始化
* *
@ -120,25 +116,7 @@ public class AioClient implements Closeable{
* @return this * @return this
*/ */
private static AsynchronousSocketChannel createChannel(final InetSocketAddress address, final int poolSize) { private static AsynchronousSocketChannel createChannel(final InetSocketAddress address, final int poolSize) {
return ChannelUtil.connect(ChannelUtil.createFixedGroup(poolSize), address);
final AsynchronousSocketChannel channel;
try {
final AsynchronousChannelGroup group = AsynchronousChannelGroup.withFixedThreadPool(//
poolSize, // 默认线程池大小
ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()//
);
channel = AsynchronousSocketChannel.open(group);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
try {
channel.connect(address).get();
} catch (final InterruptedException | ExecutionException e) {
IoUtil.close(channel);
throw new SocketRuntimeException(e);
}
return channel;
} }
// ------------------------------------------------------------------------------------- Private method end // ------------------------------------------------------------------------------------- Private method end
} }