Apache Mina Server

Source: Internet
Author: User

Apache Mina Server is a network communication application framework, mainly for TCP/IP-based, UDP/IP protocol stack-based communication frameworks. Mina can help quickly develop high-performance, high-scalability network communication applications, mina provides a programming model for event-driven and asynchronous operations.

Mina also provides the encapsulation of the server and client for network communication. Mina is in the following position throughout the structure:

It can be seen that the Mima API isolates real network communication from civilized applications. You only need to care about the data you want to send and receive and your business logic.

Similarly, the Mina execution process is as follows:

1) ioservice: this interface is used to establish a socket on a thread and has its own selector to monitor whether a new connection is established.

2) ioprocessor: this interface is responsible for checking whether data is read and written on the channel in another thread, that is, it also has its own selector.

3) iofilter: this interface defines a set of interceptors, which can include log output, blacklist filtering, data encoding, decoding, and so on. encoding and decoding are the most important, this is also the most important point when you want to use Mina.

4) iohandler: this interface is used to compile the business logic, that is, to receive and send data.

Instance: simple tcpserver

/*** TCP server * @ function: * @ Author: JY * @ company: Wanli net * @ Date: 2011-7-23 */public class myserver {public static ioacceptor acceptor = NULL; /*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {acceptor = new niosocketacceptor (); acceptor. getsessionconfig (). setreadbuffersize (2048); // read the buffer size acceptor. getsessionconfig (). setidletime (idlestatus. both_idle, 10); // enter the idle time acceptor. getsessionconfig (). setusereadoperation (true); // data-readable acceptor. getsessionconfig (). setwritetimeout (30); // write timeout // acceptor of the character filter. getfilterchain (). addlast ("codec1", new protocolcodecfilter (New textlinecodecfactory (); // acceptor. getfilterchain (). addlast ("myiofilter", new referencecountingfilter (New myiofilter (); // acceptor. getfilterchain (). addlast ("codec", new protocolcodecfilter (New cmsssipccodecfactory (charset. forname ("UTF-8"); // acceptor. getfilterchain (). addlast ("codec", new protocolcodecfilter (New flightcodecfactory (charset. forname ("UTF-8"); // bind the business logic acceptor. sethandler (New myiohandler (); acceptor. BIND (New inetsocketaddress (9123); // listener port number }}

Simple client:

/*** @ Function: * @ Author: JY * @ company: Wanli net * @ Date: 2011-7-24 */public class myclient {/*** @ Param ARGs * @ throws ioexception */public static void main (string [] ARGs) throws ioexception {ioconnector conneector = new niosocketconnector (); conneector. setconnecttimeoutmillis (30000); // conneector. getfilterchain (). addlast ("codec1", new protocolcodecfilter (New textlinecodecfactory (); // conneector. getfilterchain (). addlast ("codec", new protocolcodecfilter (New cmsssipccodecfactory (charset. forname ("UTF-8"); conneector. getfilterchain (). addlast ("codec", new protocolcodecfilter (New flightcodecfactory (charset. forname ("UTF-8"); conneector. getsessionconfig (). setusereadoperation (true); conneector. sethandler (New myclienthandler (); conneector. connect (New inetsocketaddress ("localhost", 9123 ));}}

Note: Both TCP server and tcp client must be bound to hanndler; otherwise, an exception is thrown.

Server handler: myiohandler

/*** @ Function: * @ Author: JY * @ company: Wanli net * @ Date: 2011-7-23 */public class myiohandler extends iohandleradapter {private final static logger log = loggerfactory. getlogger (myiohandler. class);/*** the method called when a message is received, that is, the method used to receive information. * Message is generally an iobuffer class. If you use a protocol decoder, You can forcibly convert it to the type you need. * Protocol codecs are usually used. * For example, you can use textlinecodecfactory to forcibly convert a message to the string type. * // @ Overridepublic void messagereceived (iosession session, object message) {flight SMS = (flight) message; // string SMS = (string) message; log.info ("The message recevied is [" + SMS + "]"); // session. write (New Java. util. date (); // session. close (true); // session. write (Session. getattribute ("test"); // If (Str. equals ("all") {// sendall (); //} // If (Str. endswith ("quit") {// session. close (true); // return ;//}}///* * // * This method is called when a session object is created. // * For TCP connections, it is called when the connection is accepted. Note that the TCP connection is not established at this time, that is, when the iosession of the connected object is created, callback method. // * For UDP, this method is called back when a packet is received because UDP is connectionless. // * // @ Override // public void sessioncreated (iosession session) {// session. Write ("Welcome! \ R "); //} // *** callback when the program encounters an exception. Generally, iosession */@ overridepublic void exceptioncaught (iosession session, throwable) is disabled here) throws exception {able. printstacktrace ();} // *** // call this method when the message is successfully sent. // * Note that this method cannot be used when a message is sent successfully. // * // @ Override // public void messagesent (iosession arg0, object arg1) throws exception {// todo auto-generated method stub //} // *** // when the TCP connection is closed, call this method. // * For UDP, this method is destroyed only when the close () method of iosession is called. // * // @ Override // public void sessionclosed (iosession arg0) throws exception {// todo auto-generated method stub //} // *** // called when the iosession channel enters idle state. // * For UDP, this method will never be called. // * // @ Override // public void sessionidle (iosession arg0, idlestatus arg1) throws exception {// todo auto-generated method stub //} // *** // called when the connection is opened, it is always called after the sessioncreated () method. // * For TCP, it is called after the connection is established. You can perform some authentication and data sending operations here. // * For UDP, this method is no different from sessioncreated (), but it is followed by execution. If you send some data at intervals, the // * sessioncreated () method will only be called for the first time, but the sessionopened () method will be called each time. // * // @ Override // public void sessionopened (iosession arg0) throws exception {// todo auto-generated method stub ////}}

Client hanlder: myclienthandler

/*** @ Function: * @ Author: JY * @ company: Wanli net * @ Date: 2011-7-24 */public class myclienthandler extends iohandleradapter {/* (non-javadoc) * @ see Org. apache. mina. core. service. iohandler # sessioncreated (Org. apache. mina. core. session. iosession) * // @ overridepublic void sessioncreated (iosession session) throws exception {// smsobject SMS = new smsobject (); // SMS. setsender ("13721047855"); // SMS. setcycler (" 15855533814 "); // SMS. setmessage ("Hello Mina"); // session. write (SMS); // flight = new flight (); // flight. setfirstcity ("Sha"); // flight. setfirstday ("2011-07-28"); // flight. setflightway ("dobuble"); // flight. setsecondcity ("BJs"); // flight. setsecondday ("2011-07-29"); // flight. setthirdcity ("Sha"); // session. write (flight); // an object can be sent after the Custom Data encode and decode are created. Session. Write ("Hello Mina ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.