Steps for building a server using the Mina framework:
1. Define a minaserver class to start the service and implement the servletcontextlistener interface.
2. Define a minaserverhandler class that processes business logic and inherit the iohandleradapter class
The minaserver code is as follows:
Import java.net. inetsocketaddress; import javax. servlet. servletcontextevent; import javax. servlet. servletcontextlistener; import javax. servlet. HTTP. httpsessionevent; import javax. servlet. HTTP. httpsessionlistener; import Org. apache. mina. core. session. idlestatus; import Org. apache. mina. filter. codec. protocolcodecfilter; import Org. apache. mina. filter. codec. serialization. objectserializationcodecfactory; import org.apache.mina.filter.exe cutor. executorfilter; import Org. apache. mina. transport. socket. NIO. niosocketacceptor; import COM. utils. logutil; public class minaserver implements servletcontextlistener, httpsessionlistener {Private Static niosocketacceptor acceptor; Private Static final int Port = 9999; Private Static final logutil = logutil. getlogutil (minaserver. class); // stop Mina service public void contextdestroyed (servletcontextevent SCE) {try {minaserver. acceptor. unbind (); minaserver. acceptor. dispose (); logutil. customlog ("Mina service stops... ");} catch (exception e) {logutil. customlog (e) ;}}// start Mina service public void contextinitialized (servletcontextevent SCE) {try {// create a non-blocking server socketacceptor = new niosocketacceptor (); // set the filter acceptor. getfilterchain (). addlast ("servercodec", new protocolcodecfilter (New objectserializationcodecfactory (); acceptor. getfilterchain (). addlast ("serverfilter", new executorfilter (); // sets the buffer size acceptor for Data Reading. getsessionconfig (). setreadbuffersize (1024*102400); // No operation is performed on the read/write channel within 10 seconds to enter the idle acceptor. getsessionconfig (). setidletime (idlestatus. both_idle, 10); // Add the logical processor acceptor. sethandler (New minaserverhandler (); // bind the port try {acceptor. BIND (New inetsocketaddress (port);} catch (exception e) {} logutil. customlog ("server started successfully... port: "+ port);} catch (exception e) {logutil. customlog ("server startup exception .... ") ;}} public void sessioncreated (httpsessionevent arg0) {} public void sessiondestroyed (httpsessionevent arg0 ){}}
The minaserverhandler code is as follows:
Import Org. apache. mina. core. service. iohandleradapter; import Org. apache. mina. core. session. idlestatus; import Org. apache. mina. core. session. iosession; import COM. model. deliverobject; import COM. model. orders; import COM. model. users; import COM. service. goodsservice; import COM. service. ordersservice; import COM. service. userservice; import COM. utils. getbeanutil; import COM. utils. logutil; import COM. utils. params; public class minaserverhandler extends iohandleradapter {Private Static final logutil = logutil. getlogutil (minaserverhandler. class); @ overridepublic void sessioncreated (iosession session) throws exception {logutil. customlog ("create a connection between the server and the client... ") ;}@ overridepublic void sessionopened (iosession session) throws exception {logutil. customlog ("the connection between the server and the client is opened... ") ;}@ overridepublic void messagereceived (iosession session, object message) throws exception {deliverobject recvobj = (deliverobject) message; int optiontype = recvobj. getoptiontype (); // if it is a logon operation if (optiontype = Params. login) {userservice = (userservice) getbeanutil. getbean ("userservice"); userservice. dologin (Session, (users) recvobj. getobj ();} else if (optiontype = Params. register) {// if you are registering userservice = (userservice) getbeanutil. getbean ("userservice"); userservice. doregister (Session, (users) recvobj. getobj ();} else if (optiontype = Params. updateuser) {// if the user information is modified, perform the following operations: userservice = (userservice) getbeanutil. getbean ("userservice"); userservice. doupdateuser (Session, (users) recvobj. getobj ();} else if (optiontype = Params. searchgoods) {// if it is a search product operation goodsservice = (goodsservice) getbeanutil. getbean ("goodsservice"); goodsservice. dosearchgoods (Session, (string) recvobj. getobj ();} else if (optiontype = Params. submitorders) {// if the order is submitted, the ordersservice = (ordersservice) getbeanutil. getbean ("ordersservice"); ordersservice. dosubmitorders (Session, (orders) recvobj. getobj ();} else if (optiontype = Params. searchallorders) {// if it is to query all order operations, ordersservice = (ordersservice) getbeanutil. getbean ("ordersservice"); ordersservice. dosearchallorders (Session, (users) recvobj. getobj () ;}@ overridepublic void messagesent (iosession session, object message) throws exception {logutil. customlog ("the message sent by the server is successful... ") ;}@ overridepublic void sessionclosed (iosession session) throws exception {logutil. customlog ("the connection between the server and the client is closed... "); Session. close (true) ;}@ overridepublic void sessionidle (iosession session, idlestatus status) throws exception {super. sessionidle (Session, status); logutil. customlog ("the server enters the idle state... ") ;}@ overridepublic void exceptioncaught (iosession session, throwable cause) throws exception {logutil. customlog ("the server sends an exception... "+ cause); Session. close (true );}}