Java内容重新整理删除过期的东西

This commit is contained in:
estom
2025-09-14 03:49:42 -04:00
parent 9b8524ff80
commit 885b795e45
413 changed files with 643 additions and 1340 deletions

View File

@@ -0,0 +1,55 @@
package cn.aofeng.demo.netty40x.echo;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
/**
* Echo客户端。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class EchoClient {
private static Logger _logger = Logger.getLogger(EchoClient.class);
public void start(String host, int port) {
EventLoopGroup worker = new NioEventLoopGroup(1);
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.group(worker)
.channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)
.handler(new EchoClientHandler())
.remoteAddress(host, port);
ChannelFuture cf = bootstrap.connect().sync();
cf.channel().closeFuture().sync();
} catch (Exception e) {
_logger.error("occurs error", e);
} finally {
worker.shutdownGracefully();
}
}
/**
* @param args
*/
public static void main(String[] args) {
if (args.length != 2) {
_logger.error("Invalid arguments。Usagejava cn.aofeng.demo.netty40x.echo.EchoClient 192.168.56.102 8080");
System.exit(-1);
}
String host = args[0];
int port = NumberUtils.toInt(args[1], 8080);
EchoClient client = new EchoClient();
client.start(host, port);
}
}

View File

@@ -0,0 +1,49 @@
package cn.aofeng.demo.netty40x.echo;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* 发送数据给服务端,并接收服务端的数据。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
private static Logger _logger = Logger.getLogger(EchoClientHandler.class);
private final Charset _utf8 = CharsetUtil.UTF_8;
public void channelActive(ChannelHandlerContext ctx) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
String currTimeStr = format.format(new Date());
String data = String.format("Hello, current time is %s", currTimeStr);
ctx.writeAndFlush( Unpooled.copiedBuffer(data, _utf8) );
_logger.info( String.format("Client send data:%s", data) );
}
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf inData = (ByteBuf) msg;
_logger.info( String.format("Client receive data:%s", inData.toString(_utf8)) );
}
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.close();
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
_logger.error("occurs error", cause);
ctx.close();
}
}

View File

@@ -0,0 +1,58 @@
package cn.aofeng.demo.netty40x.echo;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
/**
* Echo服务端将客户端发送的请求原样返回。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class EchoServer {
private static Logger _logger = Logger.getLogger(EchoServer.class);
public void start(int port) {
EventLoopGroup boss = new NioEventLoopGroup(1);
EventLoopGroup worker = new NioEventLoopGroup(10);
ServerBootstrap bootstrap = new ServerBootstrap();
try {
bootstrap.group(boss, worker)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childHandler(new EchoServerHandler())
.localAddress(port);
ChannelFuture bindFuture = bootstrap.bind().sync();
ChannelFuture closeFuture = bindFuture.channel().closeFuture().sync();
_logger.info( String.format("%s started and listen on %s", this.getClass().getSimpleName(), closeFuture ) );
} catch(Exception ex) {
_logger.info( String.format("%s start failed", this.getClass().getSimpleName()), ex);
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
/**
* @param args
*/
public static void main(String[] args) {
if (args.length != 1) {
_logger.error("Invalid arguments。Usagejava cn.aofeng.demo.netty40x.echo.EchoServer 8080");
System.exit(-1);
}
int port = NumberUtils.toInt(args[0], 8080);
EchoServer server = new EchoServer();
server.start(port);
}
}

View File

@@ -0,0 +1,38 @@
package cn.aofeng.demo.netty40x.echo;
import java.nio.charset.Charset;
import org.apache.log4j.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelHandler.Sharable;
/**
* 将接收到的数据原样返回给发送方。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
private static Logger _logger = Logger.getLogger(EchoServerHandler.class);
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf inData = (ByteBuf) msg;
String str = inData.toString(Charset.forName("UTF-8"));
_logger.info( String.format("Server receive data:%s", str) );
ctx.write(inData);
}
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush().close();
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
_logger.error("occurs error", cause);
ctx.close();
}
}