当前位置:网站首页>Apache Mina Development Manual
Apache Mina Development Manual
2022-07-03 12:32:00 【Brother Xing plays with the clouds】
One 、 Introduce
Apache Mina It's a network application framework , Simplify user development for high performance 、 The difficulty of highly scalable network applications .Mina Provides an abstract event driven asynchronous API, adopt Java NIO Realize various transmission protocols, such as TCP/IP and UDP/IP.
Apache Mina Often used as :
1)NIO The framework library
2) client / The server Communication framework library
3) The Internet Socket Communication library
Apache Mina There are also many sub projects :
1)Asyncweb
Built on Apache Mina On top of the asynchronous framework HTTP The server
2)FtpServer
One FTP The server
3)SSHd
One Java library , Support SSHH agreement
4)Vysper
One XMPP The server
Apache Mina White list implementation method http://www.linuxidc.com/Linux/2012-08/68992.htm
Apache MINA actual combat http://www.linuxidc.com/Linux/2012-04/59337.htm
Two 、Apache Mina download
Download the latest Mina v2.0.8 edition
See address :http://mina.apache.org/mina-project/downloads.html
3、 ... and 、 use Mina Development time server
Explain , It's actually based on official examples , A slight modification has been made , Because the official examples are too old , It even includes deprecated Methods .
1、 precondition
Apache Mina 2.0.8 Core
JDK 7
SLF4J+LOGBACK
2、 Project dependency package
mina-core-2.0.8.jar
slf4j-api-1.6.6.jar
3、 Based on Mina Of Time The server
package ch.chiqms.server;
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.charset.Charset; importorg.apache.mina.core.service.IoAcceptor; importorg.apache.mina.core.session.IdleStatus; importorg.apache.mina.filter.codec.ProtocolCodecFilter; importorg.apache.mina.filter.codec.textline.TextLineCodecFactory; importorg.apache.mina.filter.logging.LoggingFilter; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; public class MinaTimeServer { privatestatic final int PORT = 9123; publicstatic void main(String[] args){ // Listen for connected objects IoAcceptoracceptor = new NioSocketAcceptor(); // Configure filters //logger The filter will output all the information , For example, a newly created session 、 Reception of messages 、 Sending of messages 、 The closing of the session //codec The filter converts the data specified in the binary live protocol to message objects , Here's dealing with text-based messages acceptor.getFilterChain().addLast("logger",new LoggingFilter()); acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter( newTextLineCodecFactory(Charset.forName("UTF-8")))); // acceptor.setHandler(newTimeServerHandler()); // Set the size of the input buffer and the IDLE be familiar with acceptor.getSessionConfig().setReadBufferSize(2048); acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10); try{ acceptor.bind(newInetSocketAddress(PORT)); }catch (IOException e) { e.printStackTrace(); } } }
4、 To write Time Service Handler
package ch.chiqms.server; import java.text.SimpleDateFormat; import java.util.Calendar; import org.apache.mina.core.service.IoHandlerAdapter; importorg.apache.mina.core.session.IdleStatus; importorg.apache.mina.core.session.IoSession; public class TimeServerHandler extendsIoHandlerAdapter { @Override publicvoid exceptionCaught(IoSession session, Throwable cause) throwsException { cause.printStackTrace(); } @Override publicvoid messageReceived(IoSession session, Object message) throws Exception { Stringstr = message.toString(); if(str.trim().equalsIgnoreCase("quit")){ session.close(true); return; } Calendartime = Calendar.getInstance(); SimpleDateFormatdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); session.write(df.format(time.getTime())); System.out.println("TimeMessage written..."); } @Override publicvoid sessionIdle(IoSession session, IdleStatus status) throws Exception { System.out.println("IDLE"+session.getIdleCount(status)); } }
5、 function MinaTimeServer
Enter at the command line telnet 127.0.0.1 9123
The output of the server can also be seen :
SLF4J: Failed to load class"org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP)logger implementation SLF4J: Seehttp://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Time Message written... Time Message written... Time Message written...
边栏推荐
猜你喜欢

Swagger

为什么我的mysql容器启动不了呢
![Sword finger offer03 Repeated numbers in the array [simple]](/img/cf/c1ad2f2a45560b674b5b8c11fed244.png)
Sword finger offer03 Repeated numbers in the array [simple]

OpenGL draws colored triangles

Sword finger offer05 Replace spaces

剑指Offer07. 重建二叉树

2.7 overview of livedata knowledge points

1-2 project technology selection and structure

Wechat applet - basic content
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use
随机推荐
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
347. Top k high frequency elements
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
2.7 overview of livedata knowledge points
How to convert a numeric string to an integer
regular expression
PHP export word method (phpword)
QT OpenGL rotate, pan, zoom
[embedded] - Introduction to four memory areas
What is more elegant for flutter to log out and confirm again?
ES6 standard
AOSP ~ NTP (Network Time Protocol)
(construction notes) ADT and OOP
Talk about the state management mechanism in Flink framework
Prompt unread messages and quantity before opening chat group
Php Export word method (One MHT)
Self made pop-up input box, input text, and click to complete the event.
Integer string int mutual conversion
Symlink(): solution to protocol error in PHP artisan storage:link on win10
(construction notes) grasp learning experience