netty4.0學習筆記

1.初步使用netty框架創建服務端


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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.timeout.IdleStateHandler;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class NettyServer {
private int port;
private String ipAddress;
private EventLoopGroup bossGroup = null;
private EventLoopGroup workerGroup = null;
private ServerDecoder serverDecoder = null;
private ServerEncoder serverEncoder = null;
private BusinessHandler businessHandler = null;
public NettyServer(int port,String ipAddress){
this.port = port;
this.ipAddress = ipAddress;
}

public void start() throws Exception {
ServerBootstrap b = new ServerBootstrap();
bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors()*2);
workerGroup = new NioEventLoopGroup();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("idleStateHandler", new IdleStateHandler(60, 60, 90,
TimeUnit.SECONDS));
serverDecoder = new ServerDecoder();//當服務端接收數據時自己的業務處理類
serverEncoder = new ServerEncoder();//當服務端發送數據時自己的業務處理類
businessHandler = new BusinessHandler();//發送和接收handler[code="java"][/code]
pipeline.addLast("encoder",serverEncoder);
pipeline.addLast("decoder",serverDecoder);
pipeline.addLast("handler",businessHandler);
}
});
b.bind(this.port).sync();
System.out.println("TCP服務器已啓動。。。");
}

public void shutdown() {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
//下面兩個方法是我需要下發的兩條命令提供的方法
//提供外部調用的方法根據實際情況傳參數
public boolean sendSetParameter(Map<String,Object> obj,String ipAddress) {
return this.businessHandler.sendSetParameter(obj,ipAddress);
}
public Object sendCopyData(Map<String,Object> obj,String ipAddress) {
return this.businessHandler.sendcopyData(obj, ipAddress);
}
}

//-----------------------------

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;


/**
* 讀取數據是處理業務類
* @Description 模塊描述
* @version 1.00
* @see 參考類1
* @Date 2014-10-27 下午3:09:14
*/
public class ServerDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception {
in.resetReaderIndex();
ByteBufToBytes reader = new ByteBufToBytes();
byte[] b = reader.read(in);
//根據自己的業務邏輯需要處理接收到的byte數組
//.....
out.add("處理後的數據");
}
}

//-------------------------

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.util.Map;


/**
* 下發命令時處理業務
* @Description 模塊描述
* @version 1.00
* @see 參考類1
* @Date 2014-10-8 上午11:25:25
*/
//注意根據你通過handler類下發的數據類型需要和 Encoder類數據類型保持一致 我這裏使用的 Map<String, Object>
public class ServerEncoder extends MessageToByteEncoder<Map<String, Object>> {
@Override
protected void encode(ChannelHandlerContext ctx, Map<String, Object> map,ByteBuf out) throws Exception {
//需要處理自己的業務邏輯然後下發命令
// 比如由此需求 取出MAP對象裏面的數據然後按16進制發送
out.writeBytes("處理後的數據");
}
}

//------------

package com.biiway.gmrc.netty.server;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
//我使用的是ChannelDuplexHandler 他包含了讀和寫的操作實際上它實現了
//ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler 這三個接口 --具體詳解請查閱API
public class BusinessHandler extends ChannelDuplexHandler {
//記錄每個連接的客戶端使用IP地址作爲KEY
public Map<String, ChannelHandlerContext> channelMap = new HashMap<String, ChannelHandlerContext>();
//得到返回的參數存放的量
public Object dataObj = new Object();

//讀取客戶端反饋的信息
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.out.println("讀取返回信息。。。。"+msg);
dataObj = msg;
}
//緩衝機制
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}

/**
*監聽當有終端連接的時候記錄連接
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String ipOrPort = ctx.channel().remoteAddress().toString();//得到IP地址和端口因爲我的使用情況是端口號隨機所以就使用IP來區分
System.out.println("已連接...."+ipOrPort);
channelMap.put(ipOrPort.split(":")[0], ctx);
super.channelActive(ctx);
}

/**
*監聽當有終端連接斷開時刪除記錄連接
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
String ipOrPort = ctx.channel().remoteAddress().toString();
//當客戶端連接中斷時清除自己存放的客戶端連接
channelMap.remove(ipOrPort.split(":")[0]);
System.out.println("斷開連接....");
super.channelInactive(ctx);
}

/**
* 心跳機制
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent _evt = (IdleStateEvent) evt;
//當狀態正常時
if (_evt.state() == IdleState.WRITER_IDLE) {
ctx.writeAndFlush("根據需求定義自己的心跳協議");
} else if (_evt.state() == IdleState.READER_IDLE) {
//當超過自定義的響應時間,如果客戶端還沒響應,則服務端主動斷開與此客戶端的連接
ctx.fireExceptionCaught(new SocketTimeoutException(
"force to close channel("
+ ctx.channel().remoteAddress()
+ "), reason: time out."));
ctx.channel().close();
}
}
super.userEventTriggered(ctx, evt);
}

/**
* 設置參數
* @throws Exception
* @Date 2014-10-9 下午5:26:18
*/
public boolean sendSetParameter(Map<String, Object> obj,String ipOrPort){
Map<String, ChannelHandlerContext> map = this.channelMap;
//找到需要下發命令的客戶端連接
ChannelHandlerContext cxt = map.get(ipOrPort);
cxt.writeAndFlush(obj);
return null;
}
/**
* 抄收數據
* @Date 2014-10-9 下午5:26:33
*/
public Object sendcopyData(Map<String, Object> obj, String ipAndPort){
Map<String, ChannelHandlerContext> map = this.channelMap;
ChannelHandlerContext cxt = map.get(ipAndPort);
try {
cxt.writeAndFlush(obj);
if(this.dataObj != null){
return this.dataObj;
}
} catch (Exception e) {
return this.dataObj;
}
return this.dataObj;
}

}

//---------把得到的byteBuf轉爲BYTE數組工具類

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class ByteBufToBytes {
private ByteBuf temp;

private boolean end = true;

public ByteBufToBytes() {}

public ByteBufToBytes(int length) {
temp = Unpooled.buffer(length);
}

public void reading(ByteBuf datas) {
datas.readBytes(temp, datas.readableBytes());
if (this.temp.writableBytes() != 0) {
end = false;
} else {
end = true;
}
}

public boolean isEnd() {
return end;
}

public byte[] readFull() {
if (end) {
byte[] contentByte = new byte[this.temp.readableBytes()];
this.temp.readBytes(contentByte);
this.temp.release();
return contentByte;
} else {
return null;
}
}

public byte[] read(ByteBuf datas) {
byte[] bytes = new byte[datas.readableBytes()];
datas.readBytes(bytes);
return bytes;
}
}

----netty框架很牛掰,學習後的一點經驗,如果有用過的大牛給點意見。。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章