轻轻松松写NIO(改进版)

 对已存在的NIO模块进行增强型改进,加入事件处理接口,下面提供一个参考实现的使用例子.

准备条件:接口介绍

1:服务器监听器 用来捕获远程连接或断开事件,并处理它们,接口定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net;
 
/**
 * A listener run in service side and catch connection request event from
 * client.
 *
 * @author Chris Liao
 */
 
public interface ServerListener extends NetEventListener {
 
  /**
   * When a connection requst arrives,this method will run.
   */
  public void onConnect(ConnectEvent event);
 
  /**
   * Method run after acceptance in server side.
   */
  public void onConnected(ConnectedEvent event);
 
  /**
   * Method run after remote host close connection.
   */
  public void onClosed(DisconnectEvent event);
 
}



2:连接器的监听器 它用来处理一些发生在Connection上的读写事件,如读写事件,接口如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net;
 
/**
 * A communication event listener
 *
 * @author Chris Liao
 */
 
public interface NetConnectorListener extends NetEventListener {
 
  /**
   * Connection to remote host is stable
   */
  public void onConnected(ConnectedEvent event);
 
  /**
   * Read data from remote host
   */
  public void onRead(ReadEvent event);
 
  /**
   * Write data to remote host
   */
  public void onWrite(WriteEvent event);
 
  /**
   * Remote host close connection
   */
  public void onClosed(DisconnectEvent event);
 
  /**
   * When errors occur during communication
   */
  public void onError(ErrorEvent event);
 
}


下面就依据以上接口编写一个NIO参考实现例子.

第一步:编写Server Listener实现,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
 
import org.jmin.j2ee.kernel.net.ConnectEvent;
import org.jmin.j2ee.kernel.net.ConnectedEvent;
import org.jmin.j2ee.kernel.net.DisconnectEvent;
import org.jmin.j2ee.kernel.net.ServerListener;
import org.jmin.j2ee.kernel.net.nio.NIOConnector;
 
/**
 * A sample implementation of server listener interface and action by event.
 * This listener act on server channel.
 *
 * @author Chris Liao
 */
 
public class NIOServerListener implements ServerListener {
 
  /**
   * Method to handle a event represents request from remote host
   *
   * @param event
   */
  public void onConnect(ConnectEvent event){
 
    /**
     * cast event source to a connector
     */
    NIOConnector con = (NIOConnector)event.getSource();
 
    /**
     * print request
     */
    System.out.println("Find a connection request from: " + con.getRemoteHost());
  }
 
  /**
   * If the comming connection is accepted by server, a notification event will
   * be sent out.
   *
   * @param event
   */
  public void onConnected(ConnectedEvent event){
 
    /**
     * cast event source to a connector
     */
    NIOConnector con = (NIOConnector)event.getSource();
 
    /**
     * print request
     */
    System.out.println("Accepted a connection request from: " + con.getRemoteHost());
  }
 
  /**
   * If remote host close connection to server, a event will be sent and handled
   * by this method.
   *
   * @param event
   */
  public void onClosed(DisconnectEvent event){
 
    /**
     * cast event source to a connector
     */
    NIOConnector con = (NIOConnector)event.getSource();
 
    /**
     * print a hint message
     */
    System.out.println("Connection to host[" + con.getRemoteHost() +"] is closed from server");
  }
}
 


第二步:编写Server启动程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
 
import java.io.IOException;
 
import org.jmin.j2ee.kernel.net.nio.ServerCluster;
 
/**
 * Test sample to build a socket server based on NIO and don't care more detail.
 *
 * @author chris
 */
 
public class NIOServer {
 
  /**
   * Entrance method to build a server.If get familiar with nio, it is easy to be
   * catched.
   */
  public static void main(String args[]) throws IOException {
 
    /**
     * Cluster object works as a server collection manager,some servers can be open
     * in it.Before useful operation,<method>open</method>must be invoked to create
     * a cluster instance.
     */
    ServerCluster cluster = ServerCluster.open();
 
    /**
     * Create a server listener
     */
    NIOServerListener serverListener = new NIOServerListener();
 
    /**
     * Call<method>openServer</method>to create a socket server,two necessary
     * arguments are applied here: server port and message lister class.
     *
     * We can assume that every server can handle matched messages from clients,
     * this listener class is extended from super <class>org.jmin.j2ee.kernel.Listener</class>,
     * of course,you can define sub implementation based on the class.
     */
    cluster.openServer(9988,serverListener,NIOConnectionListener.class);
 
    /**
     * Print a successful creation message on console.
     */
    System.out.println("A nio server run on port: "+ 9988);
 
    /**
     * Create a synchronized object to block the main thread and make the server kept running.
     */
    Object obj = new Object();
    try {
      synchronized (obj) {
        obj.wait();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}



第三步:编写连接监听器实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
 
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
 
import org.jmin.j2ee.kernel.net.NetConnectorListener;
import org.jmin.j2ee.kernel.net.DisconnectEvent;
import org.jmin.j2ee.kernel.net.ErrorEvent;
import org.jmin.j2ee.kernel.net.ReadEvent;
import org.jmin.j2ee.kernel.net.WriteEvent;
import org.jmin.j2ee.kernel.net.ConnectedEvent;
import org.jmin.j2ee.kernel.net.nio.NIOConnector;
 
/**
 * A reference implementation of listener interface on net connector.
 * This object can handle some events from net connector, for example:
 * connection event, read event, write event and so on.
 *
 * Class instances work in client side and server side.
 *
 * @author Chris Liao
 */
 
public class NIOConnectionListener implements NetConnectorListener {
 
  /**
   * When connection to remote host is stable, a connection event will be published,
   * and catched by this method.
   */
  public void onConnected(ConnectedEvent event){
 
    /**
     * cast event source to a connector
     */
    NIOConnector con = (NIOConnector)event.getSource();
 
    /**
     * print a hint message
     */
    System.out.println("Connection to host[" + con.getRemoteHost() +"] is stable");
  }
 
  /**
   * When some data has been read from remote host,then a read event is sent out.
   * This method is used to handle the event,some detailed logic can be added here.
   */
  public void onRead(ReadEvent event) {
 
    /**
     * get a byte array from event object
     */
    byte[] data = event.getData();
 
    /**
     * cast event source to a connector
     */
    NIOConnector con = (NIOConnector)event.getSource();
 
    /**
     * print message on console
     */
    System.out.println("Read a meessage: " + new String(data));
 
    /**
     * send the data to remote, in fact the byte data will stored in temp pool
     * util ready to be sent.
     */
    con.write(data);
  }
 
  /**
   * When a connector associated with the current listener send a message to remote,
   * then publish a event as notification.
   */
  public void onWrite(WriteEvent event) {
    byte[] data = event.getData();
    System.out.println("Send a message: " + new String(data));
  }
 
  /**
   * When remote host close connection, a event will be published.
   *
   * @param event
   */
  public void onClosed(DisconnectEvent event) {
 
    /**
     * cast event source to a connector
     */
    NIOConnector con = (NIOConnector)event.getSource();
 
    /**
     * print a hint message
     */
    System.out.println("Connection to host[" + con.getRemoteHost() +"] is closed");
  }
 
  /**
   * When a error cause during communication,publish a error event
   *
   * @param event
   */
  public void onError(ErrorEvent event) {
 
    /**
     * get the cuase of error
     */
    Throwable cause = event.getCause();
 
    /**
     * Print the cause of error
     */
    System.out.println("Catch a error: " + cause.getMessage());
  }
}


该类被应用在Server与Client两端,但是根据具体情况,一般可以分别编写Server与Client端的实现,以上代码只供参考.


第四步:编写客户端的测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
*Copyright (c) jmin Organization. All rights reserved.
*/
package org.jmin.j2ee.kernel.net.nio.test;
 
import java.io.IOException;
 
import org.jmin.j2ee.kernel.net.nio.NIOConnector;
import org.jmin.j2ee.kernel.net.nio.NIOConnectorCluster;
 
/**
 * Test sample to show that how to build a nio client.
 *
 * @author Chris Liao
 */
 
public class NIOClient {
 
  /**
   * Main method to run a nio client.
   *
   */
  public static void main(String args[]) throws IOException {
 
    /**
     * Call static method from ConnectionCluster class to build its a instance,
     * this action is similar with nio.
     */
    NIOConnectorCluster cluster = NIOConnectorCluster.open();
 
    /**
     * Build a connection to remote host with threa parameters
     *
     * @arg remote host
     * @arg remote port
     * @arg message listener
     *
     * Message listener is extended from<class>org.jmin.j2ee.kernel.Listener</class>
     * You can define yourself message listeners for it.
     */
    NIOConnector con = cluster.connect("localhost", 9988,new NIOConnectionListener());
 
    /**
     * Send a message to the remote host.
     */
    con.write("I am a Chinese".getBytes());
 
    /**
     * synchronized objec to block main thread.
     */
    Object obj = new Object();
    try {
      synchronized (obj) {
        obj.wait();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}



经过上面四个步骤,我们就已经完成建立NIO通讯的全部过程,至此我们可以编译他们,并分别运行Server 与Client来看运行效果吧. 正确运行后将会在Console频繁看到 "Receive data: I am a Chinese"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值