使用MINA和XML marshal 传递JAVA 对象

APACHE MINA 是一个比较好的网络应用开发框架,比较适合用于java 网络通讯应用程序开发,默认支持的JAVA 对象传递是Serializer ,但Serializer 有些问题,加上如果使用XML传递网络监控比较简单,因此可以考虑利用JAVA 5/6支持的XML MASHAL将java对象先XML mashal,然后在对端unmashal。

以下是一些关键代码,先创建用于传递信息的类:

public class XMLObjectPackage {
 
 static Logger logger = Logger.getLogger(XMLObjectPackage.class.getName());
 /**
  * 总长度,等于XML长度+PACKAGE NAME 长度+4
  */
 int length;
 String XML;
 int ClassLength;
 String ClassName;
 
 

 public XMLObjectPackage(Object obj, Charset XmlCharset) throws JAXBException {
  //String s;
  XML = XmlMarshaller.marshal(obj,XmlCharset);
  //length=;
  ClassName=obj.getClass().getName();
  ClassLength=ClassName.getBytes(XmlCharset).length;
  length=XML.getBytes(XmlCharset).length+ClassLength+4;
  logger.debug(length+":"+ClassLength+":"+ClassName+":"+XML);
  //super();
  // TODO Auto-generated constructor stub
 }


 public int getLength() {
  return length;
 }
 
 
 public String getXML() {
  return XML;
 }


 public int getClassLength() {
  return ClassLength;
 }


 public String getClassName() {
  return ClassName;
 }

}

 

encode 关键代码:

@Override
 public void encode(IoSession arg0, Object obj, ProtocolEncoderOutput arg2)
   throws Exception {
  
  
  
  XMLObjectPackage request = new XMLObjectPackage(obj,XmlCharset);
        IoBuffer buffer = IoBuffer.allocate(request.getLength()+request.getClassLength()+8, false);
        buffer.setAutoExpand(true);
        buffer.putInt(request.getLength());
        buffer.putInt(request.getClassLength());
       
        buffer.put(request.getClassName().getBytes(XmlCharset));
        buffer.put(request.getXML().getBytes(XmlCharset));
        buffer.flip();
        logger.debug("buffer length:"+buffer.array().length);
        arg2.write(buffer);

  
 }

 

decode 关键代码:

@Override
 protected boolean doDecode(IoSession arg0, IoBuffer arg1,
   ProtocolDecoderOutput arg2) throws Exception {
  // TODO Auto-generated method stub
  logger.debug("doDecode...."+arg1.remaining());
  if (arg1.prefixedDataAvailable(4)){
   arg1.position(0);
   int length=arg1.getInt();
   
   
   //int request.getLength()+request.getPackageLength()
   
   if (arg1.remaining()>=length){ //read all
    
    int ClassLength=arg1.getInt();
    logger.debug("doDecode...."+length+":"+arg1.remaining()+":"+ClassLength);    
    byte ClassName[]=new byte[ClassLength];
    logger.debug("doDecode...."+length+":"+arg1.remaining()+":"+ClassLength);  
    byte dst[]=new byte[length-ClassLength-4];
    arg1.get(ClassName);
    arg1.get(dst);
    //String pname=new String(packagename);
    //String xml=new String(dst);
    
    String cname=new String(ClassName,XmlCharset);
    String xml=new String(dst,XmlCharset);
    logger.debug("package xml:"+ClassName+" "+xml);
    Object o=XmlMarshaller.unmarshal(Class.forName(cname), xml);
    
    //XMLObjectPackage pack = new XMLObjectPackage(length, dst,XmlCharset); 
    //System.out.println(pack.getXML());
    
    
    logger.debug("after doDecode...."+length+":"+arg1.remaining());
                arg2.write(o);  
   }
  }
  
  return false;
 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值