庄周梦蝶

生活、程序、未来
   :: 首页 ::  ::  :: 聚合  :: 管理

Yet another nio framework for java

Posted on 2008-10-11 14:25 dennis 阅读(2823) 评论(6)  编辑  收藏 所属分类: javamy open-source
项目名称:Yanf4j(Yet another nio framework for java)
项目地址:http://code.google.com/p/yanf4j/,当前版本0.30-beta1
协议:Apache License, Version 2.0
简单描述:
    有这么多nio框架了,为什么要another?重复造轮子也罢,这框架脱胎于一个服务器项目的网络层代码,期间参考了cindy、grizzly等nio框架的实现,加上自己的一些心得体会实现的。特点是简单、小巧、超轻量级。项目没有多大野心,目标是高效、简单地实现非阻塞模式的Server(TCP、UDP)并且保证不错的性能要求,不提供客户端API 和阻塞模式。如果你的项目需要实现一个socket server并且不希望用太重量级的框架,yanf4j是个不错的选择。

    例子,在source archive中带有例子,这里描述下tcp和udp的echo server的实现。
 一、先看TCP的Echo Server
1、实现处理handler,继承HandlerAdapter类,实现相应的回调方法,这与其他nio框架没啥区别:
import com.google.code.yanf4j.nio.Dispatcher;
import com.google.code.yanf4j.nio.Session;
import com.google.code.yanf4j.nio.impl.HandlerAdapter;
import com.google.code.yanf4j.nio.util.DispatcherFactory;

public class EchoHandler extends HandlerAdapter<String> {

    Dispatcher dispatcher 
= DispatcherFactory.newDispatcher(4);

    @Override
    
public void onException(Session session, Throwable t) {
        t.printStackTrace();
    }

    @Override
    
public void onMessageSent(Session session, String t) {
        System.out.println("sent " + t + " to "
                    
+ session.getRemoteSocketAddress());
    }

    @Override
    
public void onSessionStarted(Session session) {
        System.out.println("session started");
        session.setUseBlockingRead(
true);
        session.setUseBlockingWrite(
false);
    }

    
public void onSessionCreated(Session session) {
        System.out.println(session.getRemoteSocketAddress().toString()
                    
+ " connected");
    }

    
public void onSessionClosed(Session session) {
        System.out.println(session.getRemoteSocketAddress().toString()
                    
+ " disconnected");

    }

    
public void onReceive(final Session session, final String msg) {
        System.out.println("recv:" + msg);
        
if (msg != null)
            dispatcher.dispatch(
new Runnable() {
                
public void run() {

                    
if (msg.equals("q"))
                        session.close();
                    session.send(msg);
                }
            });
    }

}


2、实现EchoServer,核心是TCPController类的使用:

        Configuration configuration = new Configuration();
        configuration.setStatisticsServer(
true);
        configuration.setTcpSessionReadBufferSize(
256 * 1024); // 设置读的缓冲区大小
        AbstractController controller = new TCPController(configuration,
                
new StringCodecFactory());
        controller.setPort(
8080); // 设置端口
        controller.setReadThreadCount(1); // 设置读线程数,通常为1
        controller.setReceiveBufferSize(16 * 1024); // 设置socket接收缓冲区大小
        controller.setReuseAddress(false); // 设置是否重用端口
        controller.setHandler(new EchoHandler()); // 设置handler
        controller.setHandleReadWriteConcurrently(true); // 设置是否允许读写并发处理
        controller.addStateListener(new ServerStateListener());
        controller.start();

Configuration 默认会在classpath查找yanf4j.properties属性文件,用于配置服务器属性,然而,你也看到,可以编码设置这些属性,具体参考wiki。

3、然后?没然后了,一个TCP的echo server已经搞定了,你可以telnet到8080端口试试了。

二、UDP的Echo server
1、handler,可以复用前面的EchoHandler
2、UDP的核心类是UDPController:

        Configuration configuration = new Configuration();
        configuration.setTcpPort(
8090);
        configuration.setTcpReuseAddress(
false);
        configuration.setStatisticsServer(
true);
        configuration.setTcpNoDelay(
true);
        configuration.setTcpReadThreadCount(
1);
        configuration.setTcpRecvBufferSize(
16 * 1024);
        UDPController controller 
= new UDPController(configuration);
        controller.setMaxDatagramPacketLength(
1024);
        controller.setHandler(
new EchoHandler());
        controller.start();

  更多细节,请参考项目主页上的wiki



评论

# re: Yet another nio framework for java  回复  更多评论   

2008-10-15 09:48 by wavefly
编程风格和Mina有些类似:pre-event,看看你的源码先……

# re: Yet another nio framework for java  回复  更多评论   

2008-10-15 10:17 by dennis
@wavefly
嗯,多多指教。整体架构上并无多少新意,仍然是reactor+事件触发机制,只是在cindy的基础上揉和了grizzly的一些长处。私下里说,其实就是俺想锻炼学习nio罢了。

# re: Yet another nio framework for java  回复  更多评论   

2009-02-18 18:11 by 菜鸟问道
能否举例说明客户端API调用?让我学一下??

# re: Yet another nio framework for java  回复  更多评论   

2009-02-18 23:22 by dennis
@菜鸟问道
本来没有客户端API的,yanf4j一开始定位在服务器框架上。不过我最近添加了客户端API支持,请从svn获取

# re: Yet another nio framework for java  回复  更多评论   

2009-02-20 00:59 by 菜鸟问道
请从svn获取?
多谢大侠回答,问下SVN是什么意思?

# re: Yet another nio framework for java  回复  更多评论   

2010-03-22 12:23 by yonlist
厉害,膜拜一下,学习一下

只有注册用户登录后才能发表评论。


网站导航: