From 5698f1ecd412a974dc92710c9c420f750025ba56 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 23 May 2022 11:32:50 +0800 Subject: [PATCH] add ChannelUtil --- CHANGELOG.md | 1 + .../java/cn/hutool/socket/ChannelUtil.java | 62 +++++++++++++++++++ .../java/cn/hutool/socket/aio/AioClient.java | 27 +------- .../cn/hutool/socket/aio/AioClientTest.java | 16 +++-- 4 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 hutool-socket/src/main/java/cn/hutool/socket/ChannelUtil.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad2352c1..640c82e12 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * 【core 】 BeanUtil拷贝对象增加空检查(issue#I58CJ3@Gitee) * 【db 】 Column#size改为long * 【core 】 ClassUtil增加isInterface等方法(pr#623@Gitee) +* 【socket 】 增加ChannelUtil * ### 🐞Bug修复 * 【extra 】 修复SshjSftp初始化未能代入端口配置问题(issue#2333@Github) diff --git a/hutool-socket/src/main/java/cn/hutool/socket/ChannelUtil.java b/hutool-socket/src/main/java/cn/hutool/socket/ChannelUtil.java new file mode 100644 index 000000000..cf105a629 --- /dev/null +++ b/hutool-socket/src/main/java/cn/hutool/socket/ChannelUtil.java @@ -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(int poolSize) { + + try { + return AsynchronousChannelGroup.withFixedThreadPool(// + poolSize, // 默认线程池大小 + ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()// + ); + } catch (IOException e) { + throw new IORuntimeException(e); + } + } + + /** + * 连接到指定地址 + * + * @param group {@link AsynchronousChannelGroup} + * @param address 地址信息,包括地址和端口 + * @return {@link AsynchronousSocketChannel} + */ + public static AsynchronousSocketChannel connect(AsynchronousChannelGroup group, InetSocketAddress address) { + AsynchronousSocketChannel channel; + try { + 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; + } +} diff --git a/hutool-socket/src/main/java/cn/hutool/socket/aio/AioClient.java b/hutool-socket/src/main/java/cn/hutool/socket/aio/AioClient.java index 2fb37713d..9e15f30e5 100644 --- a/hutool-socket/src/main/java/cn/hutool/socket/aio/AioClient.java +++ b/hutool-socket/src/main/java/cn/hutool/socket/aio/AioClient.java @@ -1,19 +1,14 @@ package cn.hutool.socket.aio; -import cn.hutool.core.io.IORuntimeException; -import cn.hutool.core.io.IoUtil; -import cn.hutool.core.thread.ThreadFactoryBuilder; +import cn.hutool.socket.ChannelUtil; import cn.hutool.socket.SocketConfig; -import cn.hutool.socket.SocketRuntimeException; import java.io.Closeable; import java.io.IOException; import java.net.InetSocketAddress; import java.net.SocketOption; import java.nio.ByteBuffer; -import java.nio.channels.AsynchronousChannelGroup; import java.nio.channels.AsynchronousSocketChannel; -import java.util.concurrent.ExecutionException; /** * Aio Socket客户端 @@ -120,25 +115,7 @@ public class AioClient implements Closeable{ * @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; + return ChannelUtil.connect(ChannelUtil.createFixedGroup(poolSize), address); } // ------------------------------------------------------------------------------------- Private method end } diff --git a/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java b/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java index f0694a080..1a53476d8 100755 --- a/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java +++ b/hutool-socket/src/test/java/cn/hutool/socket/aio/AioClientTest.java @@ -1,15 +1,23 @@ package cn.hutool.socket.aio; import cn.hutool.core.lang.Console; +import cn.hutool.core.thread.ThreadFactoryBuilder; import cn.hutool.core.util.StrUtil; +import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousChannelGroup; public class AioClientTest { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { + final AsynchronousChannelGroup GROUP = AsynchronousChannelGroup.withFixedThreadPool(// + Runtime.getRuntime().availableProcessors(), // 默认线程池大小 + ThreadFactoryBuilder.create().setNamePrefix("Huool-socket-").build()// + ); + AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() { - + @Override public void doAction(AioSession session, ByteBuffer data) { if(data.hasRemaining()) { @@ -19,10 +27,10 @@ public class AioClientTest { Console.log("OK"); } }); - + client.write(ByteBuffer.wrap("Hello".getBytes())); client.read(); - + client.close(); } }