Search in sources :

Example 1 with AsynchronousChannelGroup

use of java.nio.channels.AsynchronousChannelGroup in project baseio by generallycloud.

the class AioSocketChannelConnector method connect.

@Override
protected void connect(InetSocketAddress socketAddress) throws IOException {
    AsynchronousChannelGroup group = context.getAsynchronousChannelGroup();
    final AsynchronousSocketChannel _channel = AsynchronousSocketChannel.open(group);
    _channel.connect(socketAddress, this, new CompletionHandler<Void, AioSocketChannelConnector>() {

        @Override
        public void completed(Void result, AioSocketChannelConnector connector) {
            CachedAioThread aioThread = (CachedAioThread) Thread.currentThread();
            AioSocketChannel channel = new AioSocketChannel(aioThread, _channel, 1);
            connector.finishConnect(channel.getSession(), null);
            aioThread.getReadCompletionHandler().completed(0, channel);
        }

        @Override
        public void failed(Throwable exc, AioSocketChannelConnector connector) {
            connector.finishConnect((UnsafeSocketSession) getSession(), exc);
        }
    });
    wait4connect();
}
Also used : CachedAioThread(com.generallycloud.baseio.component.CachedAioThread) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) UnsafeSocketSession(com.generallycloud.baseio.component.UnsafeSocketSession) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) AioSocketChannel(com.generallycloud.baseio.component.AioSocketChannel)

Example 2 with AsynchronousChannelGroup

use of java.nio.channels.AsynchronousChannelGroup in project jdk8u_jdk by JetBrains.

the class AsExecutor method main.

public static void main(String[] args) throws Exception {
    // create channel groups
    ThreadFactory factory = new PrivilegedThreadFactory();
    AsynchronousChannelGroup group1 = AsynchronousChannelGroup.withFixedThreadPool(5, factory);
    AsynchronousChannelGroup group2 = AsynchronousChannelGroup.withCachedThreadPool(Executors.newCachedThreadPool(factory), 0);
    AsynchronousChannelGroup group3 = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(10, factory));
    try {
        // execute simple tasks
        testSimpleTask(group1);
        testSimpleTask(group2);
        testSimpleTask(group3);
        // install security manager and test again
        System.setSecurityManager(new SecurityManager());
        testSimpleTask(group1);
        testSimpleTask(group2);
        testSimpleTask(group3);
        // attempt to execute tasks that run with only frames from boot
        // class loader on the stack.
        testAttackingTask(group1);
        testAttackingTask(group2);
        testAttackingTask(group3);
    } finally {
        group1.shutdown();
        group2.shutdown();
        group3.shutdown();
    }
}
Also used : AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup)

Example 3 with AsynchronousChannelGroup

use of java.nio.channels.AsynchronousChannelGroup in project baseio by generallycloud.

the class AioSocketChannelAcceptor method bind.

@Override
protected void bind(InetSocketAddress socketAddress) throws IOException {
    AioSocketChannelContext context = (AioSocketChannelContext) getContext();
    AsynchronousChannelGroup group = context.getAsynchronousChannelGroup();
    final FixedAtomicInteger channelIds = new FixedAtomicInteger(1, Integer.MAX_VALUE);
    serverSocketChannel = AsynchronousServerSocketChannel.open(group);
    serverSocketChannel.bind(socketAddress);
    serverSocketChannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {

        @Override
        public void completed(AsynchronousSocketChannel _channel, Void attachment) {
            // 接受下一个连接
            serverSocketChannel.accept(null, this);
            int channelId = channelIds.getAndIncrement();
            CachedAioThread aioThread = (CachedAioThread) Thread.currentThread();
            AioSocketChannel channel = new AioSocketChannel(aioThread, _channel, channelId);
            channel.fireOpend();
            aioThread.getReadCompletionHandler().completed(0, channel);
        }

        @Override
        public void failed(Throwable exc, Void attachment) {
            logger.error(exc.getMessage(), exc);
        }
    });
}
Also used : AioSocketChannelContext(com.generallycloud.baseio.component.AioSocketChannelContext) CachedAioThread(com.generallycloud.baseio.component.CachedAioThread) AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) FixedAtomicInteger(com.generallycloud.baseio.concurrent.FixedAtomicInteger) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) AioSocketChannel(com.generallycloud.baseio.component.AioSocketChannel)

Example 4 with AsynchronousChannelGroup

use of java.nio.channels.AsynchronousChannelGroup in project baseio by generallycloud.

the class AioServerDemo method main.

public static void main(String[] args) throws Exception {
    AsynchronousChannelGroup group = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(4));
    AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open(group).bind(new InetSocketAddress("0.0.0.0", 8013));
    server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {

        @Override
        public void completed(AsynchronousSocketChannel result, Void attachment) {
            // 接受下一个连接
            server.accept(null, this);
            try {
                ByteBuffer readBuffer = ByteBuffer.allocate(999);
                result.read(readBuffer);
                String now = new Date().toString();
                ByteBuffer buffer = encoder.encode(CharBuffer.wrap(now + "\r\n"));
                // result.write(buffer, null, new
                // CompletionHandler<Integer,Void>(){...}); //callback
                // or
                Future<Integer> f = result.write(buffer);
                f.get();
                System.out.println("sent to client: " + now);
                result.close();
            } catch (IOException | InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failed(Throwable exc, Void attachment) {
            exc.printStackTrace();
        }
    });
    ThreadUtil.sleep(9999999);
    group.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) InetSocketAddress(java.net.InetSocketAddress) AsynchronousChannelGroup(java.nio.channels.AsynchronousChannelGroup) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) Future(java.util.concurrent.Future) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date)

Aggregations

AsynchronousChannelGroup (java.nio.channels.AsynchronousChannelGroup)4 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)3 AioSocketChannel (com.generallycloud.baseio.component.AioSocketChannel)2 CachedAioThread (com.generallycloud.baseio.component.CachedAioThread)2 AioSocketChannelContext (com.generallycloud.baseio.component.AioSocketChannelContext)1 UnsafeSocketSession (com.generallycloud.baseio.component.UnsafeSocketSession)1 FixedAtomicInteger (com.generallycloud.baseio.concurrent.FixedAtomicInteger)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)1 Date (java.util.Date)1 Future (java.util.concurrent.Future)1