项目组要写一个心跳机制,用的netty3.x,所以就学习了一下netty,网上有很多例子,自己记录下做个参考,先把简单的例子跑通,后面再深入研究。
服务端Server启动代码
package com.netty3.demo;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Create by koma on 2017/11/27
*/
public class Server {
public static void main(String[] args) {
// Server启动类
ServerBootstrap bootstrap = new ServerBootstrap();
// boss线程监听端口,worker线程负责数据读写
ExecutorService boss = Executors.newCachedThreadPool();
ExecutorService worker = Executors.newCachedThreadPool();
// 设置niosocket工厂
bootstrap.setFactory(new NioServerSocketChannelFactory(boss, worker));
// 设置管道的工厂
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
// 字符串编解码,如果传递的是自己自定义的bean类,可以使用ObjectDecoder和ObjectEncoder
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("serverHandler", new ServerHandler());
return pipeline;
}
});
// 绑定到本机的端口号
bootstrap.bind(new InetSocketAddress(10086));
System.out.println("Server Start!!!");
}
}
Server端处理的Handler
package com.netty3.demo;
import org.jboss.netty.channel.*;
/**
* Create by koma on 2017/11/27
*/
public class ServerHandler extends SimpleChannelHandler {
// 消息处理
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
super.messageReceived(ctx, e);
// 可以直接强转,因为已经定义过了字符串编解码了
String s = (String) e.getMessage();
System.out.println("Server get message: "+s);
//回写到client数据
ctx.getChannel().write("Server already get your message!!");
}
// 异常处理
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
super.exceptionCaught(ctx, e);
System.out.println("出现异常了!!!");
}
// 建立新的连接触发
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelConnected(ctx, e);
System.out.println("连接建立");
}
// 连接已经建立,通道关闭触发
@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
super.channelDisconnected(ctx, e);
System.out.println("通道关闭!!");
}
}
客户端Client启动类
package com.netty3.demo;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Create by koma on 2017/11/27
*/
public class Client {
public static void main(String[] args) {
//服务类
ClientBootstrap bootstrap = new ClientBootstrap();
//线程池
ExecutorService boss = Executors.newCachedThreadPool();
ExecutorService worker = Executors.newCachedThreadPool();
//socket工厂
bootstrap.setFactory(new NioClientSocketChannelFactory(boss, worker));
//管道工厂
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("clientHandler", new ClientHandler());
return pipeline;
}
});
//连接服务端
ChannelFuture connect = bootstrap.connect(new InetSocketAddress("127.0.0.1", 10086));
Channel channel = connect.getChannel();
System.out.println("client start!!");
channel.write("hello, netty");
// bootstrap.releaseExternalResources();
}
}
Client端Handler
package com.netty3.demo;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
/**
* Create by koma on 2017/11/27
*/
public class ClientHandler extends SimpleChannelHandler {
// 只重写这一个方法了,ServerHandler已经重写过了
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
super.messageReceived(ctx, e);
// 收到从server端传来的信息
String str=(String)e.getMessage();
System.out.println("Client get mesage: " + str);
}
}