当前位置:网站首页>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...
边栏推荐
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- AOSP ~ NTP (Network Time Protocol)
- Shutter widget: centerslice attribute
- How to deploy web pages to Alibaba cloud
- (构造笔记)ADT与OOP
- ES6新特性
- OpenGL draws colored triangles
- 02_ Lock the code, and don't let the "lock" become a worry
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- Integer int compare size
猜你喜欢

PHP导出word方法(一phpword)

网络通讯之Socket-Tcp(一)

OpenGL index cache object EBO and lineweight mode

4000字超详解指针

If you can't learn, you have to learn. Jetpack compose writes an im app (II)

Integer string int mutual conversion

Summary of development issues
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock

New features of ES6

Qt+vtk+occt reading iges/step model
随机推荐
DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
Flutter 退出登录二次确认怎么做才更优雅?
Adult adult adult
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
Dart: self study system
2020-10_ Development experience set
Use of QT OpenGL camera
Pki/ca and digital certificate
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
Integer string int mutual conversion
[combinatorics] permutation and combination (example of permutation and combination)
The difference between lambda and anonymous inner class
2.8 overview of ViewModel knowledge
Itext7 uses iexternalsignature container for signature and signature verification
Flutter Widget : KeyedSubtree
elastic_ L01_ summary
SLF4J 日志门面
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
(database authorization - redis) summary of unauthorized access vulnerabilities in redis