admin 管理员组

文章数量: 887021


2024年2月24日发(作者:变量名命名的原则有哪些)

ApacheMINA和SpringMVC集成1.创建项目创建一个SpringMVC的WEB应用项目,导入SpringMVC相关的jar包。另外还需要导入支持Spring面向切面编程的几个包,如下:两个日志相关的包,如下:

另外导入MINA的包,如下:2.配置1)配置其实就是SpringMVC的相关配置。SpringMVC的配置SpringMVCcherServletcontextConfigLocationalue>classpath:,classpath:1SpringMVC*.htm

2)配置也是很普通宾配置项。/>3)配置这里只保留了MINA需要的几个bean的配置。

/schema/tx/schema/tx/">class="tIoFilterChainBuilder">

class="ketAcceptor"init-method="bind"destroy-method="unbind">上面标蓝色底的那段,是配置的自已开发的MINA处理客户端请求的服务器端程序。下面标蓝色底的配置指明,本服务器端程序起动后的服务器端的端口是1235。3.程序实现1)服务器端程序;lerAdapter;atus;ion;;Factory;publicclassMinaProtocolHandlerextendsIoHandlerAdapter{privatefinalLoggerlogger=ger(getClass());//privatefinalSetsessions=onizedSet(newHashSet());publicvoidmessageReceived(IoSessionsession,Objectmessage){Stringmsg=(String)message;n("ServerReceived:"+msg);("ServerSend:"+msg);//(session);

}publicvoidsessionIdle(IoSessionsession,IdleStatusstatus){//();}//publicvoidsessionClosed(IoSessionsession)throwsException{//(session);//}publicvoidexceptionCaught(IoSessionsession,Throwablecause){("Unexpectedexception.",cause);();}}2)客户端程序下面是二个测试用的客户端程序:1、这个Client端设置的handler程序,是用于接收服务端回复过来的信息用的。;er;ion;lerAdapter;olCodecFilter;gFilter;neCodecFactory;publicclassMinaClientHandlerextendsIoHandlerAdapter{privatestaticfinalIoFilterLOGGING_FILTER=newLoggingFilter();privatestaticfinalIoFilterCODEC_FILTER=newProtocolCodecFilter(newTextLineCodecFactory());@OverridepublicvoidsessionCreated(IoSessionsession)throwsException{terChain().addLast("codec",CODEC_FILTER);terChain().addLast("logger",LOGGING_FILTER);

}@OverridepublicvoidmessageReceived(IoSessionsession,Objectmessage)Stringmsg=(String)message;}}throwsException{n("ClientReceived:"+msg);2、这个客户端的主应用程序,负责发送客户端的请求到服务器端。;cketAddress;Address;tFuture;ler;ion;ectionFilter;ketConnector;publicclassMinaClientSupport{privateIoHandlerhandler=null;privateIoSessionsession;privateStringhost;privateintport;publicbooleansend(Objectmessage){if(session!=null&&ected()){thrownewIllegalStateException("nectfirst.");}SocketAddressaddress=newInetSocketAddress(host,port);NioSocketConnectorconnector=newNioSocketConnector();try{terChain().addLast("mdc",newMdcInjectionFilter());

dler(handler);ConnectFuturefuture1=t(address);ninterruptibly();if(!ected()){}returnfalse;session=sion();(message);}catch(Exceptione){}returnfalse;returntrue;}publicvoidclose(){if(session!=null){if(ected()){//Waituntilthechatends.}}seFuture().awaitUninterruptibly();();}publicStringgetHost(){}returnhost;publicvoidsetHost(Stringhost){}=host;publicintgetPort(){}returnport;publicvoidsetPort(intport){}=port;publicvoidsetHandler(IoHandlerhandler){}r=handler;publicstaticvoidmain(String[]args){

n("---------------------");MinaClientSupportclient=newMinaClientSupport();dler(handler);t(1235);(msg);();MinaClientHandlerhandler=newMinaClientHandler();t("localhost");Stringmsg="helloworld!";n("ClientSend:"+msg);}}


本文标签: 服务器端 程序 客户端 配置 导入