简单的Mina客户端代码MinaSimpleClient.java:
package com.bypay.mina.client; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.mina.core.future.ConnectFuture; import org.apache.mina.core.session.IoSession; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.nio.NioSocketConnector; import com.bypay.mina.handler.ClientTimerHandler; /** * Mina客户端 * @author 唐延波 * @date 2015-1-20 * */ public class MinaSimpleClient { private static String HOSTNAME = "localhost"; private static int PORT = 9123; /** * @author 唐延波 * @throws InterruptedException * @date 2014-11-4 */ public static void main(String[] args) throws InterruptedException { NioSocketConnector connector = new NioSocketConnector(); //设置编码解码器 connector.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset .forName("UTF-8")))); //设置Handler connector.setHandler(new ClientTimerHandler()); //获取连接,该方法为异步执行 ConnectFuture future = connector.connect(new InetSocketAddress( HOSTNAME, PORT)); //等待连接建立 future.awaitUninterruptibly(); //获取session IoSession session = future.getSession(); //等待session关闭 session.getCloseFuture().awaitUninterruptibly(); //释放connector资源 //实际开发中,不要频繁关闭connector,因为它是重量级资源,应该设计成单例或者连接池 connector.dispose(); } }
处理消息发送和消息接收
ClientTimerHandler.java代码:
package com.bypay.mina.handler; import org.apache.mina.core.service.IoHandlerAdapter; import org.apache.mina.core.session.IdleStatus; import org.apache.mina.core.session.IoSession; /** * ClientHandler * * @author 唐延波 * @date 2015-1-20 * */ public class ClientTimerHandler extends IoHandlerAdapter { @Override public void sessionCreated(IoSession session) throws Exception { // session 创建时调用 session.write("test"); } @Override public void messageReceived(IoSession session, Object message) throws Exception { // 异步接收消息 String str = message.toString(); System.out.println("接收服务端消息:" + str); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { // 出现异常 cause.printStackTrace(); session.close(true); } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { // 心跳 System.out.println("客户端ide:"); } }
时间: 2024-10-26 10:20:24