Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP、UDP/IP协议栈的通信框架(然,也可以提供JAVA 对象的序列化服务、虚拟机管道通信服务等),Mina 可以帮助我们快速开发高性能、高扩展性的网络通信应用,Mina 提供了事件驱动、异步(Mina 的异步IO 默认使用的是JAVA NIO 作为底层支持)操作的编程模型。Mina 主要有1.x 和2.x 两个分支,这里我们讲解最新版本2.0,如果你使用的是Mina 1.x,那么可能会有一些功能并不适用。
Mina 同时提供了网络通信的Server 端、Client 端的封装。eclipse下的代码结构如下:
import java.io.Serializable;public class User implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public User() { } public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IoSession;/** * 对象客户端接受处理类 * * @author Royal * */public class ObjectMinaClientHandler extends IoHandlerAdapter { // 当一个服务端连结进入时 @Override public void sessionOpened(IoSession session) throws Exception { System.out.println("connect server : " + session.getRemoteAddress()); session.write(new User("mina", 18)); } // 当一个服务端关闭时 @Override public void sessionClosed(IoSession session) { System.out.println(session.getRemoteAddress() + " server Disconnect !"); } // 当服务器发送的消息到达时: @Override public void messageReceived(IoSession session, Object message) throws Exception { User u = (User) message; System.out.println("这里是客户端(" + session.getLocalAddress() + ")\t服务器(" + session.getRemoteAddress() + ")发来的消息: " + u.getName() + "\t" + u.getAge()); // 发送到服务端 session.write(u); }}
import java.net.InetSocketAddress;import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;import org.apache.mina.core.future.ConnectFuture;import org.apache.mina.core.service.IoConnector;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;import org.apache.mina.transport.socket.nio.NioSocketConnector;/** * mina测试客户端 * @author Royal * */public class MinaClient { public static void main(String[] args) { // Create TCP/IP connector. IoConnector connector = new NioSocketConnector(); // 创建接收数据的过滤器 DefaultIoFilterChainBuilder chain = connector.getFilterChain(); // 设定这个过滤器将以对象为单位读取数 ProtocolCodecFilter filter = new ProtocolCodecFilter(new ObjectSerializationCodecFactory()); chain.addLast("objectFilter", filter); // 设定客户端的消息处理器:一个ObjectMinaClientHandler对象, connector.setHandler(new ObjectMinaClientHandler()); // 连结到服务器: ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 9988)); // 等待连接创建完成 cf.awaitUninterruptibly(); // 等待连接断开 cf.getSession().getCloseFuture().awaitUninterruptibly(); // 客户端断开链接,释放资源 connector.dispose(); }}
import org.apache.mina.core.service.IoHandlerAdapter;import org.apache.mina.core.session.IoSession;/** * 对象服务接受处理类 * * @author Royal * */public class ObjectMinaServerHandler extends IoHandlerAdapter { // 记录接受数据的次数 private int count = 0; /** * 当客户端 发送 的消息到达时 */ @Override public void messageReceived(IoSession session, Object message) throws Exception { // 我们己设定了服务器解析消息的规则一个User对象为单位传输: User u = (User) message; System.out.println("这里是服务器(" + session.getLocalAddress() + ")\t收到客户机(" + session.getRemoteAddress() + ")发来的用户对象:" + u.toString() + "---------" + count); count++; if (count == 1000) { count = 0; // 服务器主动断开与客户端的连接 session.close(true); } // 发送到客户端 session.write(u); } /** * 当一个客户端连接进入时 */ @Override public void sessionOpened(IoSession session) throws Exception { System.out.println("incomming client: " + session.getRemoteAddress()); } /** * 当一个客户端关闭时 */ @Override public void sessionClosed(IoSession session) throws Exception { System.out.println(session.getRemoteAddress() + " client Disconnect!"); } /** * 当捕获到异常的时候 */ @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { System.err.println("error!!!!!!!!!!!!!"); super.exceptionCaught(session, cause); }}
import java.io.IOException;import java.net.InetSocketAddress;import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;import org.apache.mina.core.service.IoAcceptor;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;import org.apache.mina.transport.socket.nio.NioSocketAcceptor;/** * mina服务端 * @author Royal * */public class MinaServer { public static void main(String[] args) throws IOException { // 创建一个非阻塞的Server端 Socket,用NIO IoAcceptor acceptor = new NioSocketAcceptor(); // 创建接收数据的过滤器 DefaultIoFilterChainBuilder chain = acceptor.getFilterChain(); // 设定这个过滤器将以对象为单位读取数 ProtocolCodecFilter filter = new ProtocolCodecFilter(new ObjectSerializationCodecFactory()); chain.addLast("objectFilter", filter); // 设定服务器端的消息处理器:一个ObjectMinaServerHandler对象, acceptor.setHandler(new ObjectMinaServerHandler()); // 服务器端绑定的端口 int bindPort = 9988; // 绑定端口,启动服务器 acceptor.bind(new InetSocketAddress(bindPort)); System.out.println("Mina Server is Listing on:= " + bindPort); }}