4.最后就是IoConnector实例类连接远端的Server
这对了MINA 来说非常容易,利用ApachMINA 来传递对象。并且这也是Java网络编程中很常用的应用。其实对于MINA 传送对象来说,如果看过前一篇文章的话,只要在其中做少许改动就可以实现对象传送,但这里考虑到例子的完整性,还是给出了全部代码示例。
首先看两个用来传递的Java对象MyRequestObject和MyResponseObject很简单只是实现了Serializ接口罢了
packagcom.google.code.garbagecan.minastudy.sample3;
importjava.io.Serializable;
publicclassMyRequestObjectimplementSerializ{
privatstaticfinallongserialVersionUID=1L;
privatStringname;
privatStringvalue;
Stringvalu{ publicMyRequestObjectStringname.
this.nam=name;
this.valu=value;
}
publicStringgetNam{
returnname;
}
publicvoidsetNamStringname{
this.nam=name;
}
publicStringgetValu{
returnvalue;
}
publicvoidsetValuStringvalu{
this.valu=value;
}
@Overrid
publicStringtoStr{
StringBuffsb=newStringBuff;
value:"+valu+"]"; sb.append"Request[name:"+name +".
returnsb.toStr;
}
}
packagcom.google.code.garbagecan.minastudy.sample3;
importjava.io.Serializable;
publicclassMyResponseObjectimplementSerializ{
privatstaticfinallongserialVersionUID=1L;
privatStringname;
privatStringvalue;
Stringvalu{ publicMyResponseObjectStringname.
this.nam=name;
this.valu=value;
}
publicStringgetNam{
returnname;
}
publicvoidsetNamStringname{
this.nam=name;
}
publicStringgetValu{
returnvalue;
}
publicvoidsetValuStringvalu{
this.valu=value;
}
@Overrid
publicStringtoStr{
StringBuffsb=newStringBuff;
value:"+valu+"]"; sb.append"Respons[name:"+name +".
returnsb.toStr;
}
}
看看Server端的代码
packagcom.google.code.garbagecan.minastudy.sample3;
importjava.io.IOException;
importjava.net.InetSocketA ddress;
importorg.apache.mina.core.service.IoA cceptor;
importorg.apache.mina.core.service.IoHandlerA dapter;
importorg.apache.mina.core.session.IdleStatus;
importorg.apache.mina.core.session.IoSession;
importorg.apache.mina.filter.codec.ProtocolCodecFilter;
importorg.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
importorg.apache.mina.filter.logging.LoggingFilter;
importorg.apache.mina.transport.socket.nio.NioSocketA cceptor;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
publicclassMyServ{
privatstaticfinalLoggerlogger=LoggerFactory.getLoggMyServer.class;
publicstaticvoidmainString[]arg{
IoA cceptoracceptor=newNioSocketA cceptor;
newLoggingFilt; acceptor.getFilterChain.addLast"logger".
newProtocolCodecFiltnewObjectSerializationCodecFactori; acceptor.getFilterChain.addLast"codec".
acceptor.setHandlnewIoHandlerA dapt{
@Overrid
publicvoidsessionCrIoSesssessionthrowException{
}
@Overrid
publicvoidsessionOpenIoSesssessionthrowException{
}
@Overrid
publicvoidsessionClosIoSesssessionthrowException{
}
@Overrid
IdleStatustatuthrowException{ publicvoidsessionIdlIoSesssession.
}
@Overrid
ThrowablcausthrowException{ publicvoidexceptionCaughtIoSesssession.
caus; logger.errorcause.getMessag.
session.clostrue;
}
@Overrid
ObjectmessagthrowException{ publicvoidmessageReceivIoSesssession.
logger.info"Receiv"+messag;
MyRequestObjectmyReqOjb=MyRequestObjectmessage;
myReqOjb.getValu; MyResponseObjectmyResObj=newMyResponseObjectmyReqOjb.getNam.
session.writmyResObj;
}
@Overrid
ObjectmessagthrowException{ publicvoidmessageSIoSesssession.
logger.info"Sent"+messag;
}
};
try{
acceptor.bindnewInetSocketA ddress10000;
}catchIOExceptionex{
ex; logger.errorex.getMessag.
}
}
}
1.首先创立I/OServic这里使用的NioSocketA cceptor类来创建了一个IoA cceptor实例。
另一个是ProtocolCodecFilt实例用来编码数据,2.创立I/OFilterChain这里使用了两个个IoFilt一个是LoggingFilt用来记录日志和打印事件消息。这里使用了ObjectSerializationCodecFactori类来序列化或反序列化数据成java对象。
其总接收了MyRequestObject对象,3.创立I/OHandler这里主要看一下messageReceiv方法。然后又发送了一个MyResponseObject对象给Client端。
4.最后就是让IoA cceptor类实例绑定端口实现监听。
看看Client端的代码
packagcom.google.code.garbagecan.minastudy.sample3;
importjava.net.InetSocketA ddress;
importorg.apache.mina.core.RuntimeIoException;
importorg.apache.mina.core.future.ConnectFuture;
importorg.apache.mina.core.service.IoConnector;
importorg.apache.mina.core.service.IoHandlerA dapter;
importorg.apache.mina.core.session.IdleStatus;
importorg.apache.mina.core.session.IoSession;
importorg.apache.mina.filter.codec.ProtocolCodecFilter;
importorg.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
importorg.apache.mina.filter.logging.LoggingFilter;
importorg.apache.mina.transport.socket.nio.NioSocketConnector;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
publicclassMyClient{
privatstaticfinalLoggerlogger=LoggerFactory.getLoggMyClient.class;
publicstaticvoidmainString[]arg{
IoConnectorconnector=newNioSocketConnector;
connector.setConnectTimeoutMilli10*1000;
newLoggingFilt; connector.getFilterChain.addLast"logger".
newProtocolCodecFiltnewObjectSerializationCodecFactori; connector.getFilterChain.addLast"codec".
connector.setHandlnewIoHandlerA dapt{
@Overrid
publicvoidsessionCrIoSesssessionthrowException{
}
@Overrid
publicvoidsessionOpenIoSesssessionthrowException{
"myvalue"; MyRequestObjectmyObj=newMyRequestObject"myname".
session.writmyObj;
}
@Overrid
publicvoidsessionClosIoSesssessionthrowException{
}
@Overrid
IdleStatustatuthrowException{ publicvoidsessionIdlIoSesssession.
}
@Overrid
ThrowablcausthrowException{ publicvoidexceptionCaughtIoSesssession.
caus; logger.errorcause.getMessag.
session.clostrue;
}
@Overrid
ObjectmessagthrowException{ publicvoidmessageReceivIoSesssession.
MyResponseObjectmyResObj=MyResponseObjectmessage;
logger.info"Receiv"+myResObj;
session.clostrue;
}
@Overrid
ObjectmessagthrowException{ publicvoidmessageSIoSesssession.
logger.info"Sent"+messag;
}
};
IoSesssession=null;
try{
10000; ConnectFuturfutur=connector.connectnewInetSocketA ddress"localhost".
future.awaitUninterrupt;
session=future.getSess;
}catchRuntimeIoExcepte{
e; logger.errore.getMessag.
}
session.getCloseFutur.awaitUninterrupt;
connector.dispos;
}
}
并设置连接超时为10秒。1.首先创立I/OServic这里使用的NioSocketConnector类来创建了一个IoConnector实例。
另一个是ProtocolCodecFilt实例用来编码数据,2.创立I/OFilterChain和服务器端同样设置了两个IoFilt一个是LoggingFilt用来记录日志和打印事件消息。这里也使用了ObjectSerializationCodecFactori类来序列化或反序列化数据成java对象。
其中在会话建立事件中发送了MyRequestObject对象,3.创立I/OHandler主要看一下sessionOpen方法。然后在messageReceiv方法中又接受了MyResponseObject对象。