apache之mina
来源:互联网 发布:mac播放器哪个好 编辑:程序博客网 时间:2024/06/06 04:50
市面上比较成熟的nio框架很少,其中做的比较好的就是apache的nio和jboss的netty了。因为apache是主流,所以可能mina用的较多。
到apache官网下载最新版的mina。接着mina官方给出的例子一起学习下mina。
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */package org.apache.mina.example.gettingstarted.timeserver;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.charset.Charset;import org.apache.mina.core.service.IoAcceptor;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.textline.LineDelimiter;import org.apache.mina.filter.codec.textline.TextLineCodecFactory;import org.apache.mina.filter.logging.LoggingFilter;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;/** * A minimal 'time' server, returning the current date. Opening a telnet server, * you will get the current date by typing any string followed by a new line. * * In order to quit, just send the 'quit' message. * * @author <a href="http://mina.apache.org">Apache MINA Project</a> */public class MinaTimeServer {/** * We will use a port above 1024 to be able to launch the server with a * standard user */private static final int PORT = 9123;/** * The server implementation. It's based on TCP, and uses a logging filter * plus a text line decoder. */public static void main(String[] args) throws IOException {// Create the acceptorIoAcceptor acceptor = new NioSocketAcceptor();//ServerSocketChannel的实现,作为服务器// Add two filters : a logger and a codecacceptor.getFilterChain().addLast("logger", new LoggingFilter());acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),LineDelimiter.WINDOWS.getValue())));//linedelimiter是输入流的简单封装//getfilerechain函数是过滤器,知道日志和编码// Attach the business logic to the serveracceptor.setHandler(new TimeServerHandler());//数据接收发送处理类// Configurate the buffer size and the iddle timeacceptor.getSessionConfig().setReadBufferSize(2048);acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);//监听idle时间的,超过10秒自动断开会话// And bind !acceptor.bind(new InetSocketAddress(PORT));//绑定到9123端口}}
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */package org.apache.mina.example.gettingstarted.timeserver;import java.util.Date;import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IdleStatus;import org.apache.mina.core.session.IoSession;/** * The Time Server handler : it return the current date when a message is * received, or close the session if the "quit" message is received. * * @author <a href="http://mina.apache.org">Apache MINA Project</a> */public class TimeServerHandler extends IoHandlerAdapter {//该函数处理数据,当遇到quit时,断开会话。/** * Trap exceptions. */@Overridepublic void exceptionCaught(IoSession session, Throwable cause)throws Exception {cause.printStackTrace();}/** * If the message is 'quit', we exit by closing the session. Otherwise, we * return the current date. */@Overridepublic void messageReceived(IoSession session, Object message)throws Exception {String str = message.toString();if (str.trim().equalsIgnoreCase("quit")) {// "Quit" ? let's get out ...session.close(true);return;}// Send the current date back to the clientDate date = new Date();session.write(date.toString());//把时间写入session内System.out.println("Message written..." + str);}/** * On idle, we just write a message on the console */@Overridepublic void sessionIdle(IoSession session, IdleStatus status)throws Exception {System.out.println("IDLE " + session.getIdleCount(status));}}
运行minaTimeServer,在命令行内容输入telnet 127.0.0.1 9123,输入信息,客户端输出你输入的信息。这就是简单的通信时间。
0 0
- apache之mina
- Apache MINA实战之 牛刀小试
- Apache MINA实战之 牛刀小试
- APACHE MINA之ECHO服务器
- Apache MINA实战之 对象传输
- Apache MINA 框架之IoService介绍
- Apache MINA 框架之Session介绍
- Apache MINA 框架之Filters介绍
- Apache MINA 框架之Handler介绍
- Apache MINA 框架之Transports介绍
- apache MINA之心跳协议运用
- Apache MINA实战之 对象传输
- Apache Mina开发手册之二
- Apache Mina开发手册之三
- Apache Mina开发手册之四
- apache MINA之心跳协议运用
- apache MINA之心跳协议运行机制
- 网络框架 - 安卓之Apache MINA
- 数据结构之双线链表
- highcharts useUTC:针对xAxis的type为datetime类型
- 基础教程2——Start Developing iOS Apps Today——从今天开始开发IOS(IOS7版)系列源文档翻译(三)
- Python 注意
- Catch That Cow
- apache之mina
- javah命令提示无法访问android.app.activity类文件
- 新中国建国后的战略漫谈(转载)
- DTrace C++ Mysteries Solved
- x64中oralce建库弹出警告“Enterprise Manager配置失败...”引起的google
- shellExcute执行带图片路径的exe程序
- OpenGL Pipeline
- 如何获取精确到毫秒的当前时间以及 日期与时间的相互转化
- Linux下使用Jmeter進行壓力測試