当前位置:网站首页>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...
边栏推荐
猜你喜欢
Self made pop-up input box, input text, and click to complete the event.
Wechat applet - basic content
LeetCode 0556. Next bigger element III - end of step 4
Shutter: add gradient stroke to font
Swagger
Prompt unread messages and quantity before opening chat group
Eureka自我保护
Why can't my MySQL container start
PHP export word method (phpword)
雲計算未來 — 雲原生
随机推荐
temp
Flutter: about monitoring on flutter applications
[official MySQL document] deadlock
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Cloud Computing future - native Cloud
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
雲計算未來 — 雲原生
elastic_ L01_ summary
Dart: self study system
Kubectl_ Command experience set
Shell: basic learning
Redis notes 01: Introduction
Fluent: Engine Architecture
pragma-pack语法与使用
QT OpenGL texture map
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
剑指Offer06. 从尾到头打印链表
(构造笔记)ADT与OOP
PHP導出word方法(一mht)
Self made pop-up input box, input text, and click to complete the event.