java 异步io 框架_基于 Java NIO 的异步 IO 框架 BaseIO

软件介绍

BaseIO是基于java nio开发的一款可快速构建网络通讯项目的异步IO框架,其以简单易用的API和优良的性能深受开发者喜爱。

项目特色支持协议扩展,已知的扩展协议有:Redis协议(仅作测试),示例:详见 {baseio-test}

LineBased协议(基于换行符的消息分割),示例:详见 {baseio-test}

FixedLength协议(固定长度报文头),支持传输文本和二进制数据

Protobase(自定义协议),支持传输文本和二进制数据及混合数据

轻松实现断线重连(轻松实现心跳机制)

支持SSL(jdkssl,openssl)

压力测试超过200W QPS的处理速度(Http1.1,I7-4790,16.04.1-Ubuntu)  wrk压测

快速入门Maven引用:

com.generallycloud

baseio-all

3.2.6.RELEASE

Simple Server:public static void main(String[] args) throws Exception {

IoEventHandle eventHandle = new IoEventHandle() {

@Override

public void accept(NioSocketChannel channel, Frame frame) throws Exception {

FixedLengthFrame f = (FixedLengthFrame) frame;

frame.write("yes server already accept your message:", channel.getCharset());

frame.write(f.getReadText(), channel.getCharset());

channel.flush(frame);

}

};

ChannelContext context = new ChannelContext(8300);

ChannelAcceptor acceptor = new ChannelAcceptor(context);

context.addChannelEventListener(new LoggerChannelOpenListener());

context.setIoEventHandle(eventHandle);

context.setProtocolCodec(new FixedLengthCodec());

acceptor.bind();

}Simple Client:public static void main(String[] args) throws Exception {

IoEventHandle eventHandle = new IoEventHandle() {

@Override

public void accept(NioSocketChannel channel, Frame frame) throws Exception {

FixedLengthFrame f = (FixedLengthFrame) frame;

System.out.println();

System.out.println("____________________" + f.getReadText());

System.out.println();

}

};

ChannelContext context = new ChannelContext(8300);

ChannelConnector connector = new ChannelConnector(context);

context.setIoEventHandle(eventHandle);

context.addChannelEventListener(new LoggerChannelOpenListener());

context.setProtocolCodec(new FixedLengthCodec());

NioSocketChannel channel = connector.connect();

FixedLengthFrame frame = new FixedLengthFrame();

frame.write("hello server!", channel);

channel.flush(frame);

ThreadUtil.sleep(100);

CloseUtil.close(connector);

}

更多样例详见 {baseio-test}

为你推荐:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值