当前位置:网站首页>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...
边栏推荐
- ES6 standard
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- 手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
- Fluent: Engine Architecture
- Wechat applet development - page Jump transfer parameters
- Eureka自我保护
- 剑指Offer07. 重建二叉树
- Fundamentals of concurrent programming (III)
- Lambda表达式
- [official MySQL document] deadlock
猜你喜欢
Symlink(): solution to protocol error in PHP artisan storage:link on win10
Wechat applet - basic content
Itext7 uses iexternalsignature container for signature and signature verification
剑指Offer09. 用两个栈实现队列
(construction notes) ADT and OOP
Socket TCP for network communication (I)
Sword finger offer04 Search in two-dimensional array [medium]
Why can't my MySQL container start
【ManageEngine】IP地址扫描的作用
为什么我的mysql容器启动不了呢
随机推荐
Shardingsphere sub database and sub table < 3 >
adb push apk
(构造笔记)MIT reading部分学习心得
PHP export word method (one MHT)
Flutter Widget : Flow
OpenGL index cache object EBO and lineweight mode
temp
Laravel time zone timezone
Flutter 退出登录二次确认怎么做才更优雅?
Socket TCP for network communication (I)
Jsup crawls Baidu Encyclopedia
[embedded] - Introduction to four memory areas
232. Implement queue with stack
LeetCode 0556.下一个更大元素 III - 4步讲完
剑指Offer09. 用两个栈实现队列
Fundamentals of concurrent programming (III)
Sword finger offer06 Print linked list from end to end
Use of atomicinteger
What is more elegant for flutter to log out and confirm again?
Flutter Widget : KeyedSubtree