AsyncIO

AsynchronousServerSocketChannel - Channels of this type are safe for use by multiple concurrent threads though at most one accept operation can be outstanding at any time. If a thread initiates an accept operation before a previous accept operation has completed then an AcceptPendingException will be thrown.

AsynchronousChannelGroup specifies the thread pool to manage the async operation callbacks, if no Executor is specified, a default

Need to pass in a custom ThreadFactory to AsynchronousChannelGroup or all the threads will have generic names and not be clear what they are for without inspecting the stack. Here are options for custom threadFactory.

Server/Consumer

class Consumer implements Runnable .....

class SocketAcceptHandler implements CompletionHandler .....

AsynchronousServerSocketChannel socket =
          AsynchronousServerSocketChannel
              .open(AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(1)));

socket.setOption(StandardSocketOptions.SO_REUSEADDR, true);

socket.bind(new InetSocketAddress("localhost", 61616));

socket.accept((Consumer) this, new SocketAcceptHandler());


Producer

interface Callback
  {
    void callBack();

  }

class Producer implements Runnable, Callback .....

class ChannelHolder implements CompletionHandler ....

ChannelHolder ch = new ChannelHolder(i, AsynchronousSocketChannel.open(), (Callback) this);

chan.connect(new InetSocketAddress("localhost", 61616), null, ch);
ch.chan.bind(new InetSocketAddress("localhost", 0));
ch.chan.connect(new InetSocketAddress("localhost", 61616), null, ch);

Comments

Popular posts from this blog

Sites, Newsletters, and Blogs

Oracle JDBC ReadTimeout QueryTimeout