相对于服务端编程,客户端的处理策略更加多样。
先沿用服务端的编程风格
,开发一个同样采用 ``IoHandler`` 进行异步事件处理的客户端:
::
1 package test.mina.time.client;
2
3 import java.net.InetSocketAddress;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.mina.common.IoConnector;
8 import org.apache.mina.common.IoHandlerAdapter;
9 import org.apache.mina.common.IoSession;
10 import org.apache.mina.filter.codec.ProtocolCodecFilter;
11 import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
12 import org.apache.mina.filter.logging.LoggingFilter;
13 import org.apache.mina.transport.socket.nio.NioSocketConnector;
14
15 public class IoHandlerStyleTimeClient {
16
17 static Log log = LogFactory.getLog( IoHandlerStyleTimeClient.class );
18
19 public static void main( final String[] args ) {
20 final IoConnector connector = new NioSocketConnector();
21
22 connector.setHandler( new IoHandlerAdapter() {
23
24 @Override
25 public void messageReceived( final IoSession session,
26 final Object message )
27 throws Exception {
28 log.info( message );
29 }
30
31 } );
32
33 connector.getFilterChain().addLast( "codec",
34 new ProtocolCodecFilter( new TextLineCodecFactory() ) );
35 connector.getFilterChain().addLast( "logging", new LoggingFilter() );
36
37 connector.connect( new InetSocketAddress( 8150 ) );
38 connector.dispose();
39 }
40
41 }
可以看到客户端的代码和服务端几乎如出一辙,大体上只不过是把 ``IoAcceptor`` 和
``NioSocketAcceptor`` 替换成了 ``IoConnector`` 和 ``NioSocketConnector`` 而已。
我们仍然逐行 来检视一下。
创建连接器
----------
::
20 final IoConnector connector = new NioSocketConnector();
传统的 TCP 客户端编程中,首先需要建立一个用于连接到服务端监听端口的客户端套接字
。你也许已经猜到, 如同 ``IoAcceptor`` 一样, MINA 中的这个 ``IoConnector`` 也
不仅仅对 应单个客户端套接字,而是一个客户端套接字工厂。利用 IoConnector 对不同
的远端地址 发起连接,便可以得到多个对应于客户端与不同服务端监听地址所建立的 TCP
连接的 ``IoSession`` 对象。
下面这个客户端则要简洁很多:
::
1 package test.mina.time.client;
2
3 import java.net.InetSocketAddress;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.apache.mina.common.IoConnector;
8 import org.apache.mina.common.IoHandlerAdapter;
9 import org.apache.mina.filter.codec.ProtocolCodecFilter;
10 import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
11 import org.apache.mina.transport.socket.nio.NioSocketConnector;
12
13 public class ReadStyleTimeClient {
14
15 static Log log = LogFactory.getLog( ReadStyleTimeClient.class );
16
17 public static void main( final String[] args ) {
18 final IoConnector connector = new NioSocketConnector();
19
20 connector.setHandler( new IoHandlerAdapter() );
21 connector.getSessionConfig().setUseReadOperation( true );
22
23 connector.getFilterChain().addLast( "codec",
24 new ProtocolCodecFilter( new TextLineCodecFactory() ) );
25
26 log.info( connector.connect( new InetSocketAddress( 8150 ) )
27 .awaitUninterruptibly().getSession().read()
28 .awaitUninterruptibly().getMessage() );
29
30 connector.dispose();
31 }
32
33 }
read 方法说明
-------------
::
final ConnectFuture connectFuture = connector
.connect( new InetSocketAddress( 8150 ) );
connectFuture.awaitUninterruptibly();
final IoSession session = connectFuture.getSession();
final ReadFuture readFuture = session.read();
readFuture.awaitUninterruptibly();
final Object message = readFuture.getMessage();
log.info( message );
以上代码是 26 至 28 行的等价展开。