订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
java

轻轻松松写NIO

近期在Jmin Kernel中加入NIO通讯块. 由于该块的加入可使Socket通讯编写更加简单容易. 下面就三个编写步骤介绍它们的使用.

第一步: 编写消息处理器.
我们都知道使用Socket最大的特点无非是用它来发送/接受一些网络数据. 一般接收端是需要处理这些数据的,正因为如此我们编写了消息数据处理器,具体如下:

/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import org.jmin.j2ee.kernel.nio.Listener;

/**
* A sample class to receive byte message and return it to sender
*
* @author Linda
*/

public class NIOFeedBackListener extends Listener{

/**
* Handle bytes message,and write them back to sender
*
* @param data from sender
*/
public void onData(byte[] data){
try{
System.out.println("Reveive data: " + new String(data));
this.connection.write(data);
}catch(Exception e){

}
}
}

该类由org.jmin.j2ee.kernel.nio.Listener扩展而来并实现其抽象方法: onData(byte[] data),
我们可以在该方法中加入我们的处理逻辑. 目前例子类是收到消息并反馈回发送者.


第二步:创建Server
我们已经对NIO做了一些包装类,使其更为简单使用,具体例子如下:

/*
*Copyright (c) jmin Organization. All rights reserved.
*/
import java.io.IOException;

import org.jmin.j2ee.kernel.nio.ServerCluster;

/**
* Test sample to build a socket server based on NIO and don't care more detail.
*
* @author chris
*/

public class NIOServerTestor {

/**
* 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();

/**
* 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, NIOFeedBackListener.class);

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();
}
}
}



第三步:建立连接到Server的Client.代码如下:

/*
*Copyright (c) jmin Organization. All rights reserved.
*/

import java.io.IOException;

import org.jmin.j2ee.kernel.nio.Connection;
import org.jmin.j2ee.kernel.nio.ConnectionCluster;

/**
* 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.
*/
ConnectionCluster cluster = ConnectionCluster.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.
*/
Connection con = cluster.connect("localhost", 9988,new NIOFeedBackListener());

/**
* 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"




如果对该通讯块细节感兴趣的话,请联系: jmin520@21cn.com.

平均得分
(0 次评分)





文章来自: 本站原创
标签:
评论: 9 | 查看次数: 2227
  • 共有 9 条评论
xiaoxue00 [2010-06-07 10:45:31]
carte world of warcraft 30% de réduction, 100% satisfait! <Fast 15 mn de sécurité (carte world of warcraft) Commerce
Acheter carte world of warcraft , 10 min de livraison PayPal Top10 partenaire, Acheter Maintenant!>
huiru [2010-06-01 16:42:21]
tutu11 [2009-11-30 19:22:54]
xuzerona [2008-11-18 10:10:47]
游客 [2008-11-04 09:30:01]
游客 [2008-05-07 19:44:41]
下载这个包 http://jmin.dev.java.net/JminApp.zip
游客 [2008-04-15 12:53:13]
怎么下载包呀?
游客 [2008-03-18 22:48:47]
的确感觉不错!好东西,值得收藏。
游客 [2008-03-18 19:50:58]
目前我在学习NIO,下载了你的包进行了小测试,

环境:
PC:普通PC
内存:1G
CPU: 2.8G

开一个Server,连接数到1000并发,没有发现任何问题,而且还可以同时上网,感觉不错!

如果机器性能不错的话,更大的并发数也应该没问题,支持!
  • 共有 9 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启