Apache Mina use the small note

Source: Internet
Author: User
Tags exception handling

Introduction to the use of small notes Apache Mina is a network application framework that helps users develop high-performance and highly reliable network applications. It provides an asynchronous API that is abstracted from Java NiO in different transmissions such as TCP/IP and UDP/IP. Environment for JDK1.6+FREEBSD9 (64bit) +apache-mina-2.0.7+vim First is the configuration of environment variables

setenv java_home "/usr/local/diablo-jdk1.6.0/"
setenv java_bin "/usr/local/bin/"

Download Mina

cd/usr/home/evoup/monsrvd-2.1/test/
Fetch http://mirror.bjtu.edu.cn/apache/mina/mina/2.0.7/dist/ apache-mina-2.0.7-bin.tar.gz
tar xzf apache-mina-2.0.7-bin.tar.gz
CP apache-mina-2.0.7/dist/ Mina-core-2.0.7.jar.
CP Apache-mina-2.0.7/lib/slf4j-api-1.6.6.jar.

In addition to SLF4J, this is the log library that Mina needs to use.

Fetch http://www.slf4j.org/dist/slf4j-1.7.2.tar.gz
tar xzf slf4j-1.7.2.tar.gz
CP slf4j-1.7.2/ Slf4j-api-1.7.2.jar.
CP Slf4j-1.7.2/slf4j-nop-1.7.2.jar.

Set the Java classpath

Setenv CLASSPATH "/usr/home/evoup/project/management/monsrvd-2.1/test/slf4j-api-1.7.2.jar:/usr/home/evoup/ project/management/monsrvd-2.1/test/slf4j-nop-1.7.2.jar:/usr/home/evoup/project/management/monsrvd-2.1/test/ Mina-core-2.0.7.jar:/usr/local/diablo-jdk1.6.0/lib:. "

Find the example of Mina's timeserver http://mina.apache.org/mina-project/userguide/ch2-basics/sample-tcp-server.html code finally looks like this, You don't have to worry about details. Java language: Import java.io.IOException;
Import java.net.InetSocketAddress;
Import Java.nio.charset.Charset;
Import Org.apache.mina.core.service.IoAcceptor;
Import Org.apache.mina.core.session.IdleStatus;
Import Org.apache.mina.filter.codec.ProtocolCodecFilter;
Import Org.apache.mina.filter.codec.textline.TextLineCodecFactory;
Import Org.apache.mina.filter.logging.LoggingFilter;
Import Org.apache.mina.transport.socket.nio.NioSocketAcceptor;

Import Org.apache.mina.core.service.IoHandlerAdapter;
Import org.apache.mina.core.session.IoSession;
Import Java.util.Date;
Import Org.slf4j.LoggerFactory;
Import Org.slf4j.Logger;

public class Minatimeserver
{
private static final int PORT = 9123;

public static void Main (String [] args) throws IOException
{
Ioacceptor acceptor = new Niosocketacceptor ();

Acceptor. Getfilterchain (). AddLast ("Logger", New Loggingfilter ());
Acceptor. Getfilterchain (). AddLast ("Codec", new Protocolcodecfilter (New Textlinecodecfactory (Charset. forname)));

Acceptor. SetHandler (New Timeserverhandler ());
Acceptor. Getsessionconfig (). Setreadbuffersize (2048);
Acceptor. Getsessionconfig (). Setidletime (Idlestatus. Both_idle, 10);
Acceptor. Bind (New Inetsocketaddress (PORT));
}
}

Class Timeserverhandler extends Iohandleradapter {
static Logger Logger = Loggerfactory. GetLogger (Timeserverhandler. Class);
static Logger Logger = Logger.getlogger (Timeserverhandler.class);
Exception handling
public void Exceptioncaught (iosession session, Throwable cause) throws Exception {
Cause. Printstacktrace ();
}
Docking the received data for business processing, where we return a current date regardless of what information is received
public void messagereceived (iosession sessions, Object message) throws Exception {
String str = message. ToString ();
if (str. Trim (). Equalsignorecase ("quit")) {
Session. Close (true);
Return
}
Logger. Debug ("REC:" + str);
Date date = new Date ();
Session. Write (date. toString ());
Logger. Debug ("message written ...");
}
Handling when the connection is idle
public void Sessionidle (iosession session, Idlestatus status) throws Exception {
Logger. Debug ("IDLE" + Session). Getidlecount (status));
}
}

[Evoup@myhost]>telnet 127.0.0.1 9123
trying 127.0.0.1 ...
Connected to localhost.

Escape character is ' ^] '.
Mon 17:34:04 CST 2013 Mon A-
17:34:04 CST 2013

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.