版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012727852/article/details/78087407
一:java序列化的缺点
1、无法跨语言。
2、序列化后的码流太大。
3、序列化性能提低。
Netty 对Pojo对象序列化.
Model
package com.play.netty.serializable.model;
import java.io.Serializable;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/9/15
*/
public class MsgReq implements Serializable {
private static final long serialVersionUID=1L;
private int reqId;
private String userName;
private String productname;
private String phoneNo;
public int getReqId() {
return reqId;
}
public void setReqId(int reqId) {
this.reqId = reqId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("MsgReq{");
sb.append("reqId=").append(reqId);
sb.append(", userName='").append(userName).append('\'');
sb.append(", productname='").append(productname).append('\'');
sb.append(", phoneNo='").append(phoneNo).append('\'');
sb.append('}');
return sb.toString();
}
}
package com.play.netty.serializable.model;
import java.io.Serializable;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/9/15
*/
public class MsgResp implements Serializable {
private static final long serialVersionUID=1L;
private String code;
private String msg;
private String productmsg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getProductmsg() {
return productmsg;
}
public void setProductmsg(String productmsg) {
this.productmsg = productmsg;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("MsgResp{");
sb.append("code='").append(code).append('\'');
sb.append(", msg='").append(msg).append('\'');
sb.append(", productmsg='").append(productmsg).append('\'');
sb.append('}');
return sb.toString();
}
}
Server
package com.play.netty.serializable.server;
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;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/9/15
*/
public class SeriMsgServer {
public void bind(Integer port) throws InterruptedException {
EventLoopGroup bossGrop = new NioEventLoopGroup();
EventLoopGroup workGrop = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGrop, workGrop)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(
new ObjectDecoder(1024 * 1024,
ClassResolvers.weakCachingConcurrentResolver(
this.getClass().getClassLoader()
)));
//
socketChannel.pipeline().addLast(new ObjectEncoder());
socketChannel.pipeline().addLast(new SeriMsgServerHandler());
}
});
//
ChannelFuture channelFuture = bootstrap.bind(port).sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGrop.shutdownGracefully();
workGrop.shutdownGracefully();
}
}
public static void main(String[] args) throws InterruptedException {
new SeriMsgServer().bind(7001);
}
}
package com.play.netty.serializable.server;
import com.play.netty.serializable.model.MsgReq;
import com.play.netty.serializable.model.MsgResp;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/9/15
*/
public class SeriMsgServerHandler extends ChannelHandlerAdapter {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
MsgReq req= (MsgReq) msg;
MsgResp msgResp=new MsgResp();
if(req!=null){
System.out.println(String.format("收到的信息为[%s]",req.toString()));
msgResp.setCode("OK200");
msgResp.setMsg("交互成功");
msgResp.setProductmsg("当前产品编号"+req.getReqId()+"");
}
//
ctx.writeAndFlush(msgResp);
}
}
client
package com.play.netty.serializable.client;
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;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/9/15
*/
public class SeriMsgClient {
public void connect(String host, Integer port) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ObjectDecoder(
1024, ClassResolvers.cacheDisabled(
this.getClass().getClassLoader()
)));
socketChannel.pipeline().addLast(new ObjectEncoder());
socketChannel.pipeline().addLast(new SeriMsgClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) throws InterruptedException {
new SeriMsgClient().connect("127.0.0.1", 7001);
}
}
package com.play.netty.serializable.client;
import com.play.netty.serializable.model.MsgReq;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
/**
* Created by IntelliJ IDEA.
* User: GongQi
* Date: 2017/9/15
*/
public class SeriMsgClientHandler extends ChannelHandlerAdapter {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for(int i=0;i<10;i++){
ctx.write(subreq(i));
}
ctx.flush();
}
private MsgReq subreq(Integer integer){
MsgReq req=new MsgReq();
req.setPhoneNo("12345"+integer);
req.setProductname("xiaohua");
req.setReqId(integer);
return req;
}
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("收到数据"+msg);
}
}