mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add ChannelUtil
This commit is contained in:
parent
2953b27dbc
commit
5698f1ecd4
@ -9,6 +9,7 @@
|
|||||||
* 【core 】 BeanUtil拷贝对象增加空检查(issue#I58CJ3@Gitee)
|
* 【core 】 BeanUtil拷贝对象增加空检查(issue#I58CJ3@Gitee)
|
||||||
* 【db 】 Column#size改为long
|
* 【db 】 Column#size改为long
|
||||||
* 【core 】 ClassUtil增加isInterface等方法(pr#623@Gitee)
|
* 【core 】 ClassUtil增加isInterface等方法(pr#623@Gitee)
|
||||||
|
* 【socket 】 增加ChannelUtil
|
||||||
*
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【extra 】 修复SshjSftp初始化未能代入端口配置问题(issue#2333@Github)
|
* 【extra 】 修复SshjSftp初始化未能代入端口配置问题(issue#2333@Github)
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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客户端
|
||||||
@ -120,25 +115,7 @@ public class AioClient implements Closeable{
|
|||||||
* @return this
|
* @return this
|
||||||
*/
|
*/
|
||||||
private static AsynchronousSocketChannel createChannel(InetSocketAddress address, int poolSize) {
|
private static AsynchronousSocketChannel createChannel(InetSocketAddress address, int poolSize) {
|
||||||
|
return ChannelUtil.connect(ChannelUtil.createFixedGroup(poolSize), address);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
// ------------------------------------------------------------------------------------- Private method end
|
// ------------------------------------------------------------------------------------- Private method end
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,23 @@
|
|||||||
package cn.hutool.socket.aio;
|
package cn.hutool.socket.aio;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
|
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.AsynchronousChannelGroup;
|
||||||
|
|
||||||
public class AioClientTest {
|
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() {
|
AioClient client = new AioClient(new InetSocketAddress("localhost", 8899), new SimpleIoAction() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doAction(AioSession session, ByteBuffer data) {
|
public void doAction(AioSession session, ByteBuffer data) {
|
||||||
if(data.hasRemaining()) {
|
if(data.hasRemaining()) {
|
||||||
@ -19,10 +27,10 @@ public class AioClientTest {
|
|||||||
Console.log("OK");
|
Console.log("OK");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.write(ByteBuffer.wrap("Hello".getBytes()));
|
client.write(ByteBuffer.wrap("Hello".getBytes()));
|
||||||
client.read();
|
client.read();
|
||||||
|
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user