Netty发送序列化对象
发送对象:
public class Message implements Serializable{
private static final long serialVersionUID = 3663916219982277840L;
private String msg;
private Long msgId;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Long getMsgId() {
return msgId;
}
public void setMsgId(Long msgId) {
this.msgId = msgId;
}
@Override
public String toString() {
return "Message{" +
"msg='" + msg + '\'' +
", msgId=" + msgId +
'}';
}
}
客户端:
public class MessageClient {
public static void main(String[] args) throws InterruptedException {
int port = 5501;
EventLoopGroup workGroup = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workGroup).channel(NioSocketChannel.class)
// .option(ChannelOption.TCP_NODELAY, true)
// .option(ChannelOption.SO_KEEPALIVE,true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected 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 MessageClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("localhost", port).sync();
channelFuture.channel().closeFuture().sync();
}finally {
workGroup.shutdownGracefully();
}
}
}
客户处理端:
public class MessageClientHandler extends SimpleChannelInboundHandler<Object>{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0;i<10;i++){
Message message = new Message();
message.setMsg("hello server");
message.setMsgId((long) i);
ctx.write(message);
}
ctx.flush();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(msg);
// super.channelRead(ctx, msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
// super.channelReadComplete(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
protected void channelRead0(ChannelHandlerContext arg0, Object arg1) throws Exception {
}
}
服务端:
public class MessageServer {
public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
// .option(ChannelOption.SO_BACKLOG,2014)
// .handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ObjectDecoder(1024*1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new MessageServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(5501).sync();
System.out.println("MessageServer.main()");
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
服务处理:
public class MessageServerHandler extends SimpleChannelInboundHandler<Object> {
private int count;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Message message = (Message) msg;
System.out.println(msg);
Message tmp = new Message();
tmp.setMsg("hello client");
tmp.setMsgId((long) ++count);
System.out.println("MessageServerHandler.channelRead()");
ctx.writeAndFlush(tmp);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
protected void channelRead0(ChannelHandlerContext arg0, Object arg1) throws Exception {
}
}