Netty笔记6----Java序列化
-
实体bean
请求实体
public class SubscribeReq implements Serializable { /** * 默认的序列号ID */ private static final long serialVersionUID = 1L; private int subReqID; private String userName; private String productName; private String phoneNumber; private String address; /** * @return the subReqID */ public final int getSubReqID() { return subReqID; } /** * @param subReqID * the subReqID to set */ public final void setSubReqID(int subReqID) { this.subReqID = subReqID; } /** * @return the userName */ public final String getUserName() { return userName; } /** * @param userName * the userName to set */ public final void setUserName(String userName) { this.userName = userName; } /** * @return the productName */ public final String getProductName() { return productName; } /** * @param productName * the productName to set */ public final void setProductName(String productName) { this.productName = productName; } /** * @return the phoneNumber */ public final String getPhoneNumber() { return phoneNumber; } /** * @param phoneNumber * the phoneNumber to set */ public final void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } /** * @return the address */ public final String getAddress() { return address; } /** * @param address * the address to set */ public final void setAddress(String address) { this.address = address; } @Override public String toString() { return "SubscribeReq [subReqID=" + subReqID + ", userName=" + userName + ", productName=" + productName + ", phoneNumber=" + phoneNumber + ", address=" + address + "]"; } }
响应实体
import java.io.Serializable; public class SubscribeResp implements Serializable { /** * 默认序列ID */ private static final long serialVersionUID = 1L; private int subReqID; private int respCode; private String desc; /** * @return the subReqID */ public final int getSubReqID() { return subReqID; } /** * @param subReqID * the subReqID to set */ public final void setSubReqID(int subReqID) { this.subReqID = subReqID; } /** * @return the respCode */ public final int getRespCode() { return respCode; } /** * @param respCode * the respCode to set */ public final void setRespCode(int respCode) { this.respCode = respCode; } /** * @return the desc */ public final String getDesc() { return desc; } /** * @param desc * the desc to set */ public final void setDesc(String desc) { this.desc = desc; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "SubscribeResp [subReqID=" + subReqID + ", respCode=" + respCode + ", desc=" + desc + "]"; } }
-
服务端启动类
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class SubReqServer {
public void bind(int port) throws Exception {
// 配置服务端的NIO线程组
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline()
.addLast(
new ObjectDecoder(
1024 * 1024,
ClassResolvers
.weakCachingConcurrentResolver(this
.getClass()
.getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new SubReqServerHandler());
}
});
// 绑定端口,同步等待成功
ChannelFuture f = b.bind(port).sync();
// 等待服务端监听端口关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放线程池资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8080;
new SubReqServer().bind(port);
}
}
-
服务端实现类
import com.xdja.domain.SubscribeReq;
import com.xdja.domain.SubscribeResp;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class SubReqServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
SubscribeReq req = (SubscribeReq) msg;
if ("Li".equalsIgnoreCase(req.getUserName())) {
System.out.println("Service accept client subscrib req : ["
+ req.toString() + "]");
ctx.writeAndFlush(resp(req.getSubReqID()));
}
}
private SubscribeResp resp(int subReqID) {
SubscribeResp resp = new SubscribeResp();
resp.setSubReqID(subReqID);
resp.setRespCode(0);
resp.setDesc("Netty book order succeed");
return resp;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();// 发生异常,关闭链路
}
}
-
客户端启动类
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
public class SubReqClient {
public void connect(int port, String host) throws Exception {
// 配置客户端NIO线程组
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(
new ObjectDecoder(1024, ClassResolvers
.cacheDisabled(this.getClass()
.getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new SubReqClientHandler());
}
});
// 发起异步连接操作
ChannelFuture f = b.connect(host, port).sync();
// 当代客户端链路关闭
f.channel().closeFuture().sync();
} finally {
// 优雅退出,释放NIO线程组
group.shutdownGracefully();
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int port = 8080;
new SubReqClient().connect(port, "127.0.0.1");
}
}
-
客户端实现类
import com.xdja.domain.SubscribeReq;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class SubReqClientHandler extends ChannelInboundHandlerAdapter {
/**
* Creates a client-side handler.
*/
public SubReqClientHandler() {
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
for (int i = 0; i < 10; i++) {
ctx.write(subReq(i));
}
ctx.flush();
}
private SubscribeReq subReq(int i) {
SubscribeReq req = new SubscribeReq();
req.setAddress("南京");
req.setPhoneNumber("138xxxxxxxxx");
req.setProductName("Netty");
req.setSubReqID(i);
req.setUserName("Li");
return req;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("Receive server response : [" + msg + "]");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
-
主要有:
1)用到的POJO要实现Serializable的接口
2)SocketChannel中加入ObjectDecoder解码器和ObjecetEncoder编码器
3)服务端的实现类中的OdjectDecoder负责对实现了Serializable接口的POJO对象进行解码,它有多个构造函数,支持不同的ClassResolver,在此我们使用ClassResolvers .weakCachingConcurrentResolver创建县城安全的WeakReferenceMap对类加载器进行缓存,它支持多线程并发访问,当虚拟机内存不足是,会释放缓存中的内存,防止内存泄漏。为了防止异常码流和解码错位导致的额内存溢出,这里将单个对象的最大序列化后的字节数组长度设置为1M。
4)客户端的实现类中OdjectDecoder禁止对类加载器进行缓存。动态模块化编程中很少对类加载器进行缓存,因为它随时可能发生变化。