Mina入门:mina版之HelloWorld[z]
Mina入门:mina版之HelloWorld
[z]
一,前言:
在完成上篇文章《Mina入门:Java NIO框架Mina、Netty、Grizzly简介与对比》之后,我们现在可以正式切入Mina入门学习了。
http://i.cnblogs.com/EditPosts.aspx?opt=1
二,搭建项目结构与解决项目依赖
本人使用Maven来管理项目的依赖。好了,废话也不多说,直接上pom.xml。这里主要是加入mina-core以及其依赖的SLF4J。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.androidwhy</groupId>
- <artifactId>mina-helloworld</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>mina-helloworld</name>
- <url>http://www.androidwhy.com</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.apache.mina</groupId>
- <artifactId>mina-core</artifactId>
- <version>2.0.4</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>jcl-over-slf4j</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-nop</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
这里需要注意一点的就是Mina与SLF4J版本的匹配,官方也有文档提到了,注意一下就是。
三,编写服务器
我们的mina版的HelloWorld的业务逻辑很简单,就是接收客户端的请求,并回应给客户端一个字符"Hello,I am Server!"就可以了。
MinaTimeServer的主代码如下。
package com.androidwhy.mina.helloworld.server;
- 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.TextLineCodecFactory;
- import org.apache.mina.filter.logging.LoggingFilter;
- import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
- public class MinaTimeServer {
- <span style="white-space: pre;"> </span>private static final int PORT = 8888;
- public static void main(String[] args) throws IOException {
- // Create the acceptor
- IoAcceptor acceptor = new NioSocketAcceptor();
- // Add two filters : a logger and a codec
- acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
- acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
- // Attach the business logic to the server
- acceptor.setHandler( new HelloWorldServerHandler() );
- // Configurate the buffer size and the iddle time
- acceptor.getSessionConfig().setReadBufferSize( 2048 );
- acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
- // And bind !
- acceptor.bind( new InetSocketAddress(PORT) );
- }
- }
Handler代码如下:
package com.androidwhy.mina.helloworld.server;
- import org.apache.mina.core.service.IoHandlerAdapter;
- import org.apache.mina.core.session.IdleStatus;
- import org.apache.mina.core.session.IoSession;
- public class HelloWorldServerHandler extends IoHandlerAdapter
- {
- /**
- * Trap exceptions.
- */
- @Override
- public 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 message.
- */
- @Override
- public 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;
- }
- System.out.println("Received message:"+str);
- // Send the "Hello, I am Server!" back to the client
- String helloWorld = "Hello, I am Server!";
- session.write(helloWorld);
- System.out.println("Message written...");
- }
- /**
- * On idle, we just write a message on the console
- */
- @Override
- public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
- {
- System.out.println( "IDLE " + session.getIdleCount( status ));
- }
- }
你会发现,使用Mina写一个Server就是如此之简单!上面的MinaTimeServer主是建立一个监听在8888端口(建议使用大于1024的端口号,因为1024以下的是系统保留的)服务,并设置了一个日志filter与一个编码的filter;HelloWorldServerHandler对handler的各位状态进行了处理,在这里最为主要的就是override了messageReceived方法,并在其中处理我们的业务逻辑。
四,运行程序
到目前为止,我们只是写了一个Server,那程序怎么跑呢?客户端就暂时采用Telnet吧!
五,总结
我只想说一句:任何开源框架的入门都是如此的Easy,Mina也一样。
Mina入门:mina版之HelloWorld[z]的更多相关文章
- Mina入门:mina版之HelloWorld
一,前言: 在完成上篇文章<Mina入门:Java NIO框架Mina.Netty.Grizzly简介与对比>之后,我们现在可以正式切入Mina入门学习了. 二,搭建项目结构与解决项目依赖 ...
- Apache Mina入门
Mina第一次听到这个名称的时候,我以为是个MM的名字米娜,后来才知道… Apache MINA(Multipurpose Infrastructure for Network Application ...
- JAVA通信系列二:mina入门总结
一.学习资料 Mina入门实例(一) http://www.cnblogs.com/juepei/p/3939119.html Mina入门教程(二)----Spring4 集成Mina http:/ ...
- Apache Mina入门实例
一.mina是啥 ApacheMINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可扩展性的网络应用程序.它提供了一个通过Java NIO在不同的传输例如TCP/IP和UDP/IP上抽象的 ...
- SpringMVC基础入门,创建一个HelloWorld程序
ref:http://www.admin10000.com/document/6436.html 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要 ...
- Spring Boot 2.x 快速入门(下)HelloWorld示例详解
上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...
- ActiveMQ Pub/Sub版的HelloWorld
1. pom.xml 这个和上一篇是一样的: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...
- ActiveMQ P2P版的HelloWorld
1.2 JMS应用程序接口 ConnectionFactory: 用户用来创建到JMS提供者的连接的被管对象.JMS客户通过可移植的接口访问连接,这样当下层的实现改变时,代码不需要进行修改. 管理员 ...
- Mina入门实例(一)
mina现在用的很多了,之前也有用到,但是毕竟不熟悉,于是查了一些资料,做了一些总结.看代码是最直观的,比什么长篇大论都要好.不过其中重要的理论,也要理解下. 首先是环境,程序运行需要几个包,这里用m ...
随机推荐
- 【Unix网络编程】chapter2传输层:TCP,UDP和SCTP
2.1 概述 TCP:复杂,可靠的字节流协议 UDP:简单的,不可靠的数据包协议 SCTP:流控制传输协议 2.2 总图 2.3 用户数据报协议2.4 传输控制协议2.5 流控制传输协议(SCTP)2 ...
- SpringBoot入门篇--热部署
在项目的开发过程中我们难免会涉及到对代码的修改,有bug我们也需要对代码进行修改,这时候我们就需要重启服务器.但是,我们知道的是这个过程是相当的痛苦的,重启服务器涉及到了所有资源的重新加载,过程那是相 ...
- django-template-forloop
forloop.counter0 # 是每次循环的index 红色的div标签,居然可以这样写. ex:第一次循环的结果 <div class="item active" ...
- C# WEB.API 接收并解析保存base64格式的图片
using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...
- go遍历目录
package main import ( "fmt" "io/ioutil" "os" "path/filepath" ...
- Error 2503 and 2502 when installing/uninstalling on Windows 10
1. Hold Ctrl+Shift and press Esc. 2. Locate “Windows Explorer” under “Windows processes”, now right ...
- python: no module named bz2
https://stackoverflow.com/questions/8115280/importerror-no-module-named-bz2-for-python-2-7-2 centos6 ...
- React Native指南汇集了各类react-native学习资源、开源App和组件
来自:https://github.com/ele828/react-native-guide React Native指南汇集了各类react-native学习资源.开源App和组件 React-N ...
- vnc服务安装与配置
一.Redhat上VNC Server配置 本文以当前Linux系统未安装VNC服务器为基本,如果已安装请跳过第1节! 前提: 1.安装 TigerVNC Server # yum search ti ...
- Haskell语言学习笔记(60)Biapplicative
Biapplicative class Bifunctor p => Biapplicative p where bipure :: a -> b -> p a b (<< ...