当前位置:网站首页>Apache Mina开发手册
Apache Mina开发手册
2022-07-03 11:54:00 【星哥玩云】
一、介绍
Apache Mina是一个网络应用框架,简化用户开发高性能、高可扩展性的网络应用程序的难度。Mina提供了一个抽象的事件驱动的异步API,通过Java NIO实现各种传输协议如TCP/IP和UDP/IP。
Apache Mina经常用作:
1)NIO框架库
2)客户端/服务器通信框架库
3)网络Socket通信库
Apache Mina还伴随有不少子项目:
1)Asyncweb
构建于Apache Mina异步框架之上的HTTP服务器
2)FtpServer
一个FTP服务器
3)SSHd
一个Java库,支持SSHH协议
4)Vysper
一个XMPP服务器
Apache Mina 白名单实现方法 http://www.linuxidc.com/Linux/2012-08/68992.htm
Apache MINA实战 http://www.linuxidc.com/Linux/2012-04/59337.htm
二、Apache Mina下载
下载最新的Mina v2.0.8版
地址见:http://mina.apache.org/mina-project/downloads.html
三、用Mina开发时间服务器
说明一下,其实是基于官方的例子,略作了修改,因为发现官方的例子太陈旧,甚至包含了deprecated的方法。
1、先决条件
Apache Mina 2.0.8 Core
JDK 7
SLF4J+LOGBACK
2、项目依赖包
mina-core-2.0.8.jar
slf4j-api-1.6.6.jar
3、编写基于Mina的Time服务器
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){ //监听连接的对象 IoAcceptoracceptor = new NioSocketAcceptor(); //配置过滤器 //logger过滤器会输出所有的信息,例如新创建的会话、消息的接收、消息的发送、会话的关闭 //codec过滤器会转换二进制活协议规定的数据为消息对象,这里是处理基于文本的消息 acceptor.getFilterChain().addLast("logger",new LoggingFilter()); acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter( newTextLineCodecFactory(Charset.forName("UTF-8")))); // acceptor.setHandler(newTimeServerHandler()); //设置输入缓冲区的大小和会话的IDLE熟悉 acceptor.getSessionConfig().setReadBufferSize(2048); acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,10); try{ acceptor.bind(newInetSocketAddress(PORT)); }catch (IOException e) { e.printStackTrace(); } } }
4、编写Time服务的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、运行MinaTimeServer
在命令行输入telnet 127.0.0.1 9123
服务器端的输出也可以看到:
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...
边栏推荐
- Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
- Flutter: about monitoring on flutter applications
- Pragma pack syntax and usage
- DEJA_ Vu3d - cesium feature set 053 underground mode effect
- OpenGL shader use
- elastic_ L02_ install
- elastic_ L01_ summary
- PHP导出word方法(一phpword)
- Fundamentals of concurrent programming (III)
- 【ManageEngine】IP地址扫描的作用
猜你喜欢

New features of ES6
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock

Shutter: overview of shutter architecture (excerpt)

Eureka自我保护

Laravel time zone timezone

Fluent: Engine Architecture

LeetCode 0556. Next bigger element III - end of step 4

1-2 project technology selection and structure

Talk about the state management mechanism in Flink framework
随机推荐
PHP導出word方法(一mht)
Flutter: self study system
剑指Offer03. 数组中重复的数字【简单】
adb push apk
102. Sequence traversal of binary tree
347. Top k high frequency elements
C language improvement article (wchar_t) character type
Eureka自我保护
QT OpenGL texture map
2020-11_ Technical experience set
Slf4j log facade
OpenGL shader use
Self made pop-up input box, input text, and click to complete the event.
剑指Offer04. 二维数组中的查找【中等】
ES6 standard
111. Minimum depth of binary tree
剑指Offer06. 从尾到头打印链表
023 ([template] minimum spanning tree) (minimum spanning tree)
Unicode encoding table download
PHP get the file list and folder list under the folder