Yet another nio framework for java
有这么多nio框架了,为什么要another?重复造轮子也罢,这框架脱胎于一个服务器项目的网络层代码,期间参考了cindy、grizzly等nio框架的实现,加上自己的一些心得体会实现的。特点是简单、小巧、超轻量级。项目没有多大野心,目标是高效、简单地实现非阻塞模式的Server和Client(包括UDP和TCP)并且保证不错的性能要求,不提供阻塞模式。如果你的项目需要实现一个socket server/client并且不希望用太重量级的框架,yanf4j是个不错的选择。目前仅支持 jdk>=6.0 ,毕竟nio在>=6.0以上版本的jdk中有最好的表现。
Yanf4j is another nio framework for java,it is light weight,simple and have a great performance.It only supports non-blocking mode for tcp/udp server or client,it doesn't supports blocking mode. It needs jdk>=6.0.
Example
A Time Server
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.google.code.yanf4j.nio.Session;
import com.google.code.yanf4j.nio.impl.HandlerAdapter;
public class TimeHandler extends HandlerAdapter<String> {
@Override
public void onSessionStarted(Session session) {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
session.asyncWrite(dateFormat.format(date));
session.flush();
session.close();
}
}
import java.io.IOException;
import com.google.code.yanf4j.nio.Controller;
import com.google.code.yanf4j.nio.TCPController;
import com.google.code.yanf4j.nio.impl.StringCodecFactory;
import java.net.InetSocketAddress;
public class TimeServer {
public static void main(String[] args) {
Controller controller = new TCPController();
try {
controller.open(new InetSocketAddress(8090), new TimeHandler(),
new TextLineCodecFactory());
} catch (IOException e) {
e.printStackTrace();
}
}
}