netty对象序列化传输机制(序)
来源:互联网 发布:存活的数据叫删失数据 编辑:程序博客网 时间:2024/06/05 08:37
4.4. 对象传输的另一种实现机制
这种机制主要是为了应对其中一方不采用netty框架传输对象。
实现方案:采用通用的ObjectInputStream和ObjectOutputStream将对象转换成byte数组,然后传输(即实际传输的是byte数组)。
此种方式下无需设定编解码器就可传递对象,通过调试助手等终端也可以传输对象
(1) UDPServerPipelineFactory.java
ChannelPipeline pipeline = Channels.pipeline(
this.EXECUTION_UP_HANDLER,
this.EXECUTION_DOWN_HANDLER,
new UDPServerHandler());
return pipeline;
(2) UDPServerHandler.java
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
ChannelBuffer cb = (ChannelBuffer) e.getMessage();
Object obj = Utils.bytes2Obj(cb.array());
Plan plan = (Plan)obj;
LOGGER.info(plan);
plan.setStatus(" server received");
byte[] b = Utils.obj2Byte(plan);
ChannelBuffer backBuffer = ChannelBuffers.buffer(b.length);
backBuffer.writeBytes(b);
e.getChannel().write(backBuffer,e.getRemoteAddress());
}
(3) UDPClient.java
Plan plan = new Plan();
plan.setAuthor("linfenliang");
plan.setContent("something about sequence");
plan.setDate("2012-11-07");
plan.setStatus("client send ");
byte[] b = Utils.obj2Byte(plan);
System.out.println(Utils.bytesToHex(b));
Channel channel = MoreClient.initChannel();
ChannelBuffer cb = ChannelBuffers.buffer(b.length);
cb.writeBytes(b);
channel.write(cb);
(4) UdpClientPipelineFactory.java
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline(
this.EXECUTION_UP_HANDLER,
this.EXECUTION_DOWN_HANDLER,
new UdpClientHandler());
return pipeline;
}
(5) Utils.java
/**
* 输入流关闭
*
* @author linfenliang
* @date 2012-7-4
* @version V1.0.0
* @param inStream
* void
*/
public static void closeInStreamQuietly(InputStream inStream) {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
LOGGER.error("quiet close, no deal:"+e);
} finally {
inStream = null;
}
}
}
/**
* 输出流关闭
*
* @author linfenliang
* @date 2012-7-4
* @version V1.0.0
* @param outStream
* void
*/
public static void closeOutStreamQuietly(OutputStream outStream) {
if (outStream != null) {
try {
outStream.flush();
outStream.close();
} catch (IOException e) {
LOGGER.error("quiet close, no deal:"+e);
} finally {
outStream = null;
}
}
}
/**
* Object对象转byte数组
*
* @author linfenliang
* @date 2012-11-8
* @version V1.0.0
* @param obj
* @return byte[]
*/
public static byte[] obj2Byte(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bo = null;
ObjectOutputStream oo = null;
try {
bo = new ByteArrayOutputStream();
oo = new ObjectOutputStream(bo);
oo.writeObject(obj);
bytes = bo.toByteArray();
} catch (Exception e) {
LOGGER.error("Object对象转byte数组异常:"+e);
} finally {
closeOutStreamQuietly(bo);
closeOutStreamQuietly(oo);
}
return bytes;
}
/**
* byte数组转对象
*
* @author linfenliang
* @date 2012-11-8
* @version V1.0.0
* @param bytes
* @return Object
*/
public static Object bytes2Obj(byte[] bytes) {
Object obj = null;
ByteArrayInputStream bi = null;
ObjectInputStream oi = null;
try {
bi = new ByteArrayInputStream(bytes);
oi = new ObjectInputStream(bi);
obj = oi.readObject();
} catch (Exception e) {
LOGGER.error("byte数组转对象异常:"+e);
} finally {
closeInStreamQuietly(bi);
closeInStreamQuietly(oi);
}
return obj;
}
- netty对象序列化传输机制(序)
- Netty中对象序列化传输机制研究分析
- Netty中对象序列化传输机制研究分析
- netty 对象序列化传输示例
- 序列化对象传输
- netty学习七:集成protobuf完成单个对象序列化以及在网络上传输
- Netty发送序列化对象
- netty传输对象
- netty对象传输
- Socket传输序列化对象
- 序列化对象如何接收和传输
- Java Socket传输对象(序列化)
- 使用boost库序列化传输对象
- JSON对象序列化传输格式设置
- android对象实现序列化传输
- aidl ( 六) 加入序列化对象传输
- 使用boost库序列化传输对象
- JAVA对象序列化机制
- Netty中execution包功能详解
- C++重载和函数模板
- Netty多线程处理机制
- linux模块安装和卸载
- Netty多线程配置机制
- netty对象序列化传输机制(序)
- netty 数据分包、组包、粘包处理机制(一)
- netty 数据分包、组包、粘包处理机制(二)
- TCP/IP、Http、Socket的区别
- TCP三次握手协议
- 运放参数的详细解释和分析
- Top 20 Programming Lessons I\'ve Learned in 20 Years
- Where is the “Father of Java?”
- 流媒体传输知识整理(一)