Java NiO framework Mina Chinese course __java

Source: Internet
Author: User
Now that the World Wide Web era, countless Web application frameworks have been created to greatly improve the speed of web development. Aside from this advantage of WWW, we know that there are many other protocols that the HTTP protocol cannot replace. Sometimes, we still need to construct the C/s application to implement the appropriate protocol.

= = = What is mina? ===
Have you ever implemented a stack of protocols using Java or any other language? As you have experienced, writing web apps is not easy even for experienced developers. This is attributable to the following areas:

* No suitable Web application framework is designed for developers.
* Make it impossible for you to create your application in a limited amount of time.
* Network I/O code, message encoding/decoding, business logic is often entangled together.
* Loss of maintainability and reusability of the program
* Network application difficult to unit test
* You've lost your agility *

Mina is a network application framework that solves all of the above problems without sacrificing performance and scalability.


= = I/o layer: Write an Echo Server = =
Mina contains two layers: IO layer and protocol layer. We first use the IO layer to implement an echo service, because the protocol layer is usually built on the IO layer.
Attachment:Arch1.gif
The diagram above shows the interaction of Mina's IO layer with the client. Ioacceptor executes all the underlying IO, translates them into abstract IO events, and translates the translated events and associated Iosession
Sent to Iohandler.


= = = Iosession = =
Attachment:IoSession.gif
One represents the IO connection of the Iosession program to the same remote entity. With Iosession, you can write message to a remote entity, access the configuration of the session, and change the properties of the session.


= = = Iohandler = =
Attachment:IoHandler.gif
* Sessioncreated: Called when an IO connection is established, this method is invoked before any IO operation so that the socket parameter or session property can be set first.
* sessionopened: Called after the sessioncreated call.
* sessionclosed: Called when IO connection is closed.
* Sessionidle: Called when there is no data transfer between the remote entity and the user program.
* Exceptioncaught: Called when an exception occurs in Ioacceptor or your iohandler.
* Messagereceived: Invoked when a new protocol message is received. You can implement your control process here.
* Messagesent: Called when a user requests a message that is actually sent through Iosession#write (Object).


Let's look at how to implement the Iohandler of the Echo protocol.
= = = Implement Iohandler and boot code = = =
Typically, applications need to inherit iohandleradapter and implement the required methods:


Package org.apache.mina.examples.echoserver;

Import Org.apache.mina.common.ByteBuffer;
Import Org.apache.mina.common.IoHandlerAdapter;
Import org.apache.mina.common.IoSession;
Import Org.apache.mina.common.TransportType;
Import Org.apache.mina.transport.socket.nio.SocketSessionConfig;

public class Echoprotocolhandler extends Iohandleradapter
{
public void Sessioncreated (iosession session)
{
if (session.gettransporttype () = = Transporttype.socket) {
((Socketsessionconfig) Session.getconfig ()). Setreceivebuffersize (2048);
}

public void Exceptioncaught (iosession session, Throwable Cause)
{
Session.close ();
}

public void messagereceived (iosession sessions, Object message)
{
if (!) ( Message instanceof Bytebuffer))
Return

Bytebuffer RB = (bytebuffer) message;
Write the received data back to remote peer
Bytebuffer wb = bytebuffer.allocate (rb.remaining ());
Wb.put (RB);
Wb.flip ();
Session.write (WB);
}
}
We just used Mina to implement the ECHO protocol, and now we're binding handler to a server port.
Import java.net.InetSocketAddress;
Import Org.apache.mina.transport.socket.nio.SocketAcceptor;
Import Org.apache.mina.transport.socket.nio.SocketAcceptorConfig;

public class Main
{
/** Choose your favorite port number. */
private static final int PORT = 8080;


public static

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.