Java 7 NIO2 Asynchronousç³»ã®ä½¿ãæ¹
æ¤ç´¢ãããã©ããã¾ãã¡ããã¨ããæ
å ±ã誰ãè¼ãã¦ãªãæ°ãããã®ã§ã
å¿ããªããã¡ã«ã¾ã¨ãã¦ããã¾ãã
ã¾ãã¯ã·ã³ãã«ãªServerã®ä½ãæ¹ã
System.out.println("Start"); try (AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open();) { server.bind(new InetSocketAddress(12345)); Future<AsynchronousSocketChannel> sock = server.accept(); try (AsynchronousSocketChannel ch = sock.get();) { ByteBuffer buf = ByteBuffer.allocate(1024); Future<Integer> result = ch.read(buf); buf.flip(); Integer i = result.get(); System.out.println("Read :"+i); System.out.println(Charset.defaultCharset().decode(buf)); } } catch (IOException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); }
æ®éã®ServerSocketã¨éãã¨ããã¯acceptã®è¿ãå¤ãFutureåãªã¨ããã
acceptã¡ã½ããã§ã¯ãããã¯ããªãã§ãFutureåãè¿ãã
ãªã®ã§ãAsynchronousã®ååãä»ãã¦ããã¨ãããã¨ãã§ãããã
ãã®ããã°ã©ã ã§ã¯ã¨ããããsock.get()ãå¼ã³åºãã¦ããã§ãããã¯ã
getã¡ã½ããã«ã¯ã¿ã¤ã ã¢ã¦ããæå®ã§ããã®ã§ã¹ã¬ãããä»ã®å¦çã«åããã人ã¯ã¿ã¤ã ã¢ã¦ãããã¦ãã ããã
ãã£ã¨ãããããªä½¿ãæ¹ããã®ãªããããããã®Futureåã使ã£ãå®è£
ã¯åãã¦ããªãæ°ãããã®ã§å¾è¿°ããæ¹æ³ã使ã£ãæ¹ããã£ãããã¾ãã
ã§ãget()ã®è¿ãå¤ã¨ãã¦AsynchronousSocketChannelãè¿ã£ã¦æ¥ãã®ã§ãããã使ã£ã¦ã¯ã©ã¤ã¢ã³ãã¨éä¿¡ã
readã¡ã½ãããå¼ã³åºãã¨åæ§ã«Futureåãè¿ã£ã¦æ¥ãã®ã§åãããã«getã¡ã½ããã®å¼ã³åºãã§ãããã¯ã
å¾ã¯sysoãã¦çµããã
ãã©ã¦ã¶çã§localhost:12345ã«ã¢ã¯ã»ã¹ããã¨HTTPã®ãªã¯ã¨ã¹ããè¦ããã¯ãã§ãã
ãã®å®è£
ã§ã¯ããã表示ãã¦çµããããµã¼ãã¼çã«ä½¿ãã®ã§ããã°Whileã«ã¼ããããã°ãããã ãã©ã
誰ãã®ãReadãã¦ããéã¯Acceptã§ããªãã®ã§ãReadé¨åã¯ã¹ã¬ããã«ããã®ãæ®éã§ãã
ã¡ãªã¿ã«ãSocketã®Optionãæå®ããã«ã¯Java 7ããä¸è¨ã®ãããªæ¹æ³ããµãã¼ãããã¾ããã
server.setOption(StandardSocketOptions.SO_REUSEADDR, true);
setOptionã¡ã½ããã§ã¯SocketOption
æ®éã®Optionã¯StandardSocketOptionsã«å
¥ã£ã¦ãã®ã§äºè¶³ãã¾ãã
SO_REUSEADDRã®å ´åã¯Booleanã®Genericsãæå®ããã¦ããã®ã§ã第2å¼æ°ã«Boolean以å¤ãå
¥ããã¨ã¨ã©ã¼ã«ãªãã¾ãã
ããã°ã©ã ä¸ã§ããããªå¼æ°ãæå®ãããã¨ãã¯ãã®æ¸ãæ¹ã便å©ã§ããã
ãã®Futureã使ã£ãå¼ã³åºãæ¹ã¯ãããã¾ã§ã®å®è£
ã«é
æ
®ãã¦ãç°¡åã«ç½®æã§ããããã«ç¨æããããã§ã¯ãªãããªããã¨ã
Java 7ã§ãããããæå³ã¯ããã¾ãç¡ãã¨æãã¾ãã
ã¨ãããã¨ã§ã次ã¯CompletionHandlerã使ã£ããµã¼ãã¼ã
public class AsyncServer { public class SimpleHandler implements CompletionHandler<AsynchronousSocketChannel, Integer> { @Override public void completed(AsynchronousSocketChannel result, Integer attachment) { server.accept(0, handler); ByteBuffer buf = ByteBuffer.allocate(1024); Future<Integer> f = result.read(buf); try { int i = f.get(); buf.flip(); System.out.println(Charset.defaultCharset().decode(buf)); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } @Override public void failed(Throwable exc, Integer attachment) { } } private final AsynchronousServerSocketChannel server; private final SimpleHandler handler; public AsyncServer() throws IOException { server = AsynchronousServerSocketChannel.open(); handler = new SimpleHandler(); } public void start(){ try { server.bind(new InetSocketAddress(12345)); server.accept(0, handler); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { AsyncServer server = new AsyncServer(); server.start(); while(true); } }
ç°¡åã«è¨ãã¨ãacceptããã¨ãã«æåããå ´åã¨å¤±æããå ´åã®å¦çãç»é²ãã¦ããã®ãCompletionHandleråã使ã£ãæ¹æ³ã§ãã
æåã«å®£è¨ããã¦ããã¤ã³ãã¯ã©ã¹ã¯acceptæã«ä½¿ãCompletionHandlerãå®è£
ãã¦ãã¾ãã
Genericsã®æåã¯ãã£ãã®Futureã§ããã¨ããã®è¿ãå¤ãäºã¤ç®ã¯æ·»ä»ããObjectã®åã§ããï¼Selectorã使ã£ãã¨ãã®attachã¨åã使ãæ¹ï¼
completedã¡ã½ãããæåããã¨ãã«å®è¡ãããã¡ã½ãããfailedã失æããã¨ãã«å®è¡ãããã¡ã½ããã§ãã
completedã¡ã½ããã¯æ¬¡ã®handlerãç»é²ãã¦ããã¦ãAcceptããã¨ãã®å¦çã«é²ã¿ã¾ãã
ãã®ä¾ã§ã¯Futureåã使ã£ã¦æåã®ä¾ã¨åæ§ã«è¡¨ç¤ºãã¦çµãããï¼Closeãã¦ãªããã©ï¼
å½ç¶ããã®readã¡ã½ãããCompletionHandlerã使ã£ã¦æ¸ãã¾ãã
å®è¡ããã«ã¯ãacceptããã¨ãã«attachãããªãã¸ã§ã¯ãã¨CompletionHandlerãæå®ããã°ã
å¾ã¯åæã«æå®ãããã¼ãã§acceptããã¨ãã«Handlerãå¼ã°ãã¦ã¹ã¬ããã«ããå¦çãè¡ããã¾ãã
ãã®ããã«CompletionHandlerã使ãã°ãacceptããreadã¾ã§ãããã¯ãããã¨ãªãå²ã¨ç解ããããå½¢ã§æ¸ããã®ãAsynchronousç³»ã®ç¹å¾´ããããã¾ããã
ããã§æ³¨æãªã®ããããã¾ã§acceptããã¨ãã®å¦çãè¨è¿°ããã ããªã®ã§ãä»ã«ä½ãåãã¦ããªããã°Javaã¯ããã°ã©ã ãçµäºãã¾ãã
Javaã®GUIãªãä»ã®ç£è¦ã¹ã¬ãããªããåãã¦ããå¿
è¦ãããã®ã§ããã®ããã°ã©ã ã§ã¯æå¾ã«while(true)ã¨ããã¦ã¾ãã
ã¡ãªã¿ã«ããã£ã¨ç´æçã«æ¸ããã人ã¯å¿åã¤ã³ãã¯ã©ã¹ã¨ã使ã£ã¦ä¸è¨ã®ããã«æ¸ãã®ãæ®éããªãã¨ã
public static void main(String[] args) throws IOException { final AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(); server.bind(new InetSocketAddress(12345)); server.accept(0, new CompletionHandler<AsynchronousSocketChannel, Integer>() { @Override public void completed(AsynchronousSocketChannel result, Integer attachment) { // completed server.accept(0, this); } @Override public void failed(Throwable exc, Integer attachment) { // failed } }); while(true); }
ã¨ã¦ãã·ã³ãã«ã§ããï¼
å¿åã¤ã³ãã¯ã©ã¹å
ã§ä½¿ãããã¡ã³ãã«ã¯finalãä»ããããã¡ããã¨æå®å¯è½ãªããã«ãã¾ãããã
CompletionHandlerã使ãã¨ãã«ä¸ã¤æ³¨æããªãã¨ãããªãã®ã¯
CompletionHandlerã®ä¸èº«ã¯ãã«ãã¹ã¬ããã«ããå¦çããªãããã¨ããã§ãã
ã¤ã¾ããã¡ã½ããã¯ã¹ã¬ããã»ã¼ãã«æ¸ããªãã¨ããã¾ããã
ä»®ã«acceptããæ°ãã«ã¦ã³ããããã¨ãã¦ãããªã½ã¼ã¹ãæ¸ããããã¡ã§ãã
public class SimpleHandler implements CompletionHandler<AsynchronousSocketChannel, Integer> { private int i = 0; @Override public void completed(AsynchronousSocketChannel result, Integer attachment) { server.accept(0, handler); i++;
i++ã®å®è¡ã¯ã¹ã¬ããã»ã¼ãã§ã¯ãªãã®ã§ãè¤æ°ã®ã¹ã¬ãããèµ°ãå¯è½æ§ãããé¨åã«æ¸ãã¦ã¯ããã¾ããã
ã¤ãã§ã«iã®å®£è¨ã«finalãvolatileãä»ãã¦ãªãã®ã§å
¨ç¶ãã¡ã§ãã
ã«ã¦ã³ãããã人ã¯AtomicIntegerã使ãã¾ãããã
æå¾ã«ãããã°ã©ã ä¸ã§ExecutorServiceãæ¢ã«ä½¿ã£ã¦ãã¦ãã¹ã¬ãããã¡ããã¨ç®¡çãããã¨ãã人ã¯Channelãéãã¨ãã«Groupãæå®ã§ãã¾ãã
ExecutorService service = Executors.newCachedThreadPool(); AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(service); AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(group);
AsynchronousChannelGroupã®ä½ãæ¹ã¯ä»ã«ãããããããã®ã§APIãåèã«ãã¦ãã ããã
以ä¸ã§Asynchronousç³»ã®ä½¿ãæ¹ã¯ã ãããçµããã§ãã
å人çã«ã¯Selectorã使ãæ¹æ³ããããªã使ãããããªã£ããªããã¨ã
ãã®ä¾ã§ã¯acceptã¨readã ãã ãã©ãå½ç¶writeãconnectãAsynchronousã«å®è¡ã§ãã¾ãã
Selector使ãã¨ãã®è¾ºãã¾ã§å³å¯ã«NonBlockingã«æ¸ãã®ã¯ããã©ãããã£ãã®ã§ãçµæ§å©ããã¾ãã
ãã ãåé¡ã¯ããAsynchronousãã£ã¦ã®ãé·ãããï¼
Eclipseåæã ã¨ãã¦ãé·ããããä¸è¨ã®groupæå®ããã³ã¼ããªãã¦ã±ã£ã¨è¦ãã«ãªã¹ç¶æ
ã
Asynchronous âãAsyncã«ããã ãã§ããªãéãã®ã«ãªãã
ã¾ããJavaã«ä»æ´ãããªãã¨è¨ã£ã¦ãä»æ¹ãªãã§ããã