-------------------
AIO |
-------------------
# jdk7µÄ²úÎï
# ²ÅÊÇÕæÕýµÄÒì²½·Ç×èÈûIO,ѧϰµÄÊÇ Linux epoll ģʽ
# API
AsynchronousServerSocketChannel
# ·þÎñ¶Ë Channel
public abstract void accept(A attachment,CompletionHandler handler);
AsynchronousSocketChannel
# ¿Í»§¶Ë Channel
public final void read(ByteBuffer dst,A attachment,CompletionHandler handler)
*
AsynchronousChannelGroup
# Ïß³Ì×é
ExecutorService
# Ï̳߳Ø,¼òµ¥
CompletionHandler
# ½Ó¿Ú,Á©³éÏó·½·¨
void completed(V result, A attachment);
void failed(Throwable exc, A attachment);
-------------------
AIO-Server |
-------------------
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by Kevin on 2017/2/16 20:22.
*/
public class AioServer {
//Ï̳߳Ø
private ExecutorService executorService;
//Ïß³Ì×é
private AsynchronousChannelGroup asynchronousChannelGroup;
//·þÎñÆ÷ͨµÀ
private AsynchronousServerSocketChannel asynchronousServerSocketChannel;
public AioServer(int port){
try {
//ʵÀý»¯Ï̳߳Ø
executorService = Executors.newCachedThreadPool();
//ʵÀý»¯Ïß³Ì×é
asynchronousChannelGroup = AsynchronousChannelGroup.withCachedThreadPool(executorService,1);
//´´½¨·þÎñͨµÀ
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open(asynchronousChannelGroup);
//°ó¶¨±¾µØ¶Ë¿Ú
asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
System.out.println("·þÎñ¶ËÆô¶¯,port=" + port);
//½øÐÐ×èÈû
asynchronousServerSocketChannel.accept(this,new AioServerCompletionHandler());
//Ò»Ö±×èÈû ²»È÷þÎñÆ÷Í£Ö¹
Thread.sleep(Integer.MAX_VALUE);
}catch (Exception e){
e.printStackTrace();
}
}
public AsynchronousServerSocketChannel getAsynchronousServerSocketChannel() {
return asynchronousServerSocketChannel;
}
public void setAsynchronousServerSocketChannel(AsynchronousServerSocketChannel asynchronousServerSocketChannel) {
this.asynchronousServerSocketChannel = asynchronousServerSocketChannel;
}
}
-------------------
AIO-AioServerCompletionHandler|
-------------------
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.ExecutionException;
/**
* Created by Kevin on 2017/2/16 20:27.
*/
public class AioServerCompletionHandler implements CompletionHandler {
/**
* Á¬½Ó³É¹¦µÄʱºòÖ´ÐеIJÙ×÷
* @param result
* @param attachment
*/
@Override
public void completed(AsynchronousSocketChannel result, AioServer attachment) {
//µ±ÓÐÏÂÒ»¸ö¿Í»§¶Ë½ÓÈëµÄʱºò Ö±½Óµ÷ÓÃServerµÄaccept·½·¨£¬ÕâÑù·´¸´Ö´ÐÐÏÂÈ¥£¬±£Ö¤¶à¸ö¿Í»§¶Ë¶¼¿ÉÒÔ×èÈû
attachment.getAsynchronousServerSocketChannel().accept(attachment,this);
read(result);
}
private void read(final AsynchronousSocketChannel asynchronousSocketChannel) {
//¹¹½¨»º³åÇø
ByteBuffer buf = ByteBuffer.allocate(1024);
asynchronousSocketChannel.read(buf, buf, new CompletionHandler() {
/**
* resultSize¾ÍÊǶÁÈ¡µ½µÄÊý¾Ý³¤¶È(×Ö½ÚÊý)
* @param resultSize
* @param attachment
*/
@Override
public void completed(Integer resultSize, ByteBuffer attachment) {
//½øÐжÁȡ֮ºó,ÖØÖñêʶλ
attachment.flip();
//»ñµÃ¶ÁÈ¡µÄ×Ö½ÚÊý
System.out.println("Server -> " + "ÊÕµ½¿Í»§¶ËµÄÊý¾Ý³¤¶ÈΪ:" + resultSize);
//»ñÈ¡¶ÁÈ¡µÄÊý¾Ý
String resultData = new String(attachment.array()).trim();
System.out.println("Server -> " + "ÊÕµ½¿Í»§¶ËµÄÊý¾ÝÐÅϢΪ:" + resultData);
String response = "·þÎñÆ÷ÏìÓ¦, ÊÕµ½Á˿ͻ§¶Ë·¢À´µÄÊý¾Ý: " + resultData;
write(asynchronousSocketChannel, response);
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
exc.printStackTrace();
}
});
}
private void write(AsynchronousSocketChannel asynchronousSocketChannel, String response) {
try {
ByteBuffer buf = ByteBuffer.allocate(1024);
buf.put(response.getBytes());
buf.flip();
asynchronousSocketChannel.write(buf).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
/**
* Ò쳣ʱִÐÐ
* @param exc
* @param attachment
*/
@Override
public void failed(Throwable exc, AioServer attachment) {
exc.printStackTrace();
}
}
-------------------
AIO-AioClient |
-------------------
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
/**
* Created by Kevin on 2017/2/16 20:38.
*/
public class AioClient implements Runnable{
private AsynchronousSocketChannel asc ;
public AioClient() throws Exception {
asc = AsynchronousSocketChannel.open();
}
public void connect(){
asc.connect(new InetSocketAddress("127.0.0.1", 1024));
}
public void write(String request){
try {
asc.write(ByteBuffer.wrap(request.getBytes())).get();
read();
} catch (Exception e) {
e.printStackTrace();
}
}
private void read() {
ByteBuffer buf = ByteBuffer.allocate(1024);
try {
asc.read(buf).get();
buf.flip();
byte[] respByte = new byte[buf.remaining()];
buf.get(respByte);
System.out.println(new String(respByte,"utf-8").trim());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
}
}
public static void main(String[] args) throws Exception {
AioClient c1 = new AioClient();
c1.connect();
AioClient c2 = new AioClient();
c2.connect();
AioClient c3 = new AioClient();
c3.connect();
new Thread(c1, "c1").start();
new Thread(c2, "c2").start();
new Thread(c3, "c3").start();
Thread.sleep(1000);
c1.write("c1 aaa");
c2.write("c2 bbbb");
c3.write("c3 ccccc");
}
}