Java Code Examples for java.nio.channels.AsynchronousChannelGroup#withThreadPool()

The following examples show how to use java.nio.channels.AsynchronousChannelGroup#withThreadPool() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Client.java    From oxygen with Apache License 2.0 6 votes vote down vote up
/**
 * 连接服务端,指定绑定地址
 *
 * @param remote 远程地址
 * @param bind 本机绑定地址
 * @return channelContext
 * @throws IOException io异常时抛出
 */
public ChannelContext connect(InetSocketAddress remote, InetSocketAddress bind)
    throws IOException {
  AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup
      .withThreadPool(groupContext.getGroupExecutor());
  groupContext.setChannelGroup(channelGroup);
  AsynchronousSocketChannel channel = IoUtils.create(channelGroup, bind);
  groupContext.setAioListener(new ComposeAioListener().add(ClientAioListener.INSTANCE)
      .add(groupContext.getAioListener()));

  ChannelContext channelContext = new ChannelContext(groupContext, channel, false);
  channelContext.setServerAddress(remote);
  channel.connect(remote, channelContext, ConnectHandler.INSTANCE);

  BeatProcessor beat = new BeatProcessor(channelContext);
  ThreadUtils.globalTimer()
      .scheduleWithDelay(beat, groupContext.getBeatInterval(), TimeUnit.MILLISECONDS, beat);
  return channelContext;
}
 
Example 2
Source File: RpcAioAcceptor.java    From hasting with MIT License 6 votes vote down vote up
@Override
public void startService() {
	super.startService();
	try {
		//启动acceptor,开始接受连接
		acceptHandler = new RpcAcceptCompletionHandler();
		acceptHandler.startService();
		channelGroup = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(channelGroupThreads));
		serverChannel = AsynchronousServerSocketChannel.open(channelGroup).bind(new InetSocketAddress(this.getHost(), this.getPort()));
		serverChannel.accept(this, acceptHandler);
		this.startListeners();
		this.fireStartNetListeners();
	} catch (IOException e) {
		throw new RpcException(e);
	}
}
 
Example 3
Source File: AsExecutor.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 4
Source File: RpcAioConnector.java    From hasting with MIT License 5 votes vote down vote up
/**
 * 通道异步线程池
 */
private void checkChannelGroup(){
	//检查group
	if(channelGroup==null){
		try {
			channelGroup = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(channelGroupThreads));
		} catch (IOException e) {
			throw new RpcException(e);
		}
	}
}
 
Example 5
Source File: AsExecutor.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 6
Source File: AsExecutor.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 7
Source File: AsExecutor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 8
Source File: AsExecutor.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 9
Source File: AsExecutor.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 10
Source File: AsExecutor.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 11
Source File: Nio2Endpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Initialize the endpoint.
 */
@Override
public void bind() throws Exception {

    // Create worker collection
    if (getExecutor() == null) {
        createExecutor();
    }
    if (getExecutor() instanceof ExecutorService) {
        threadGroup = AsynchronousChannelGroup.withThreadPool((ExecutorService) getExecutor());
    }
    // AsynchronousChannelGroup currently needs exclusive access to its executor service
    if (!internalExecutor) {
        log.warn(sm.getString("endpoint.nio2.exclusiveExecutor"));
    }

    serverSock = AsynchronousServerSocketChannel.open(threadGroup);
    socketProperties.setProperties(serverSock);
    InetSocketAddress addr = (getAddress()!=null?new InetSocketAddress(getAddress(),getPort()):new InetSocketAddress(getPort()));
    serverSock.bind(addr, getAcceptCount());

    // Initialize thread count defaults for acceptor, poller
    if (acceptorThreadCount != 1) {
        // NIO2 does not allow any form of IO concurrency
        acceptorThreadCount = 1;
    }

    // Initialize SSL if needed
    initialiseSsl();
}
 
Example 12
Source File: AsExecutor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 13
Source File: AsExecutor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 14
Source File: Server.java    From oxygen with Apache License 2.0 5 votes vote down vote up
/**
 * 启动服务
 *
 * @param address 地址
 * @throws IOException io异常时抛出
 */
@SuppressWarnings("squid:S2095")
public void start(InetSocketAddress address) throws IOException {
  groupContext.setStopped(false);
  AsynchronousChannelGroup channelGroup = AsynchronousChannelGroup
      .withThreadPool(groupContext.getGroupExecutor());
  groupContext.setChannelGroup(channelGroup);
  serverAddress = address;
  serverChannel = AsynchronousServerSocketChannel.open(channelGroup)
      .setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(address);
  serverChannel.accept(this, new AcceptHandler());
}
 
Example 15
Source File: AsynchronousChannelStep.java    From coroutines with Apache License 2.0 5 votes vote down vote up
/***************************************
 * Returns the {@link AsynchronousChannelGroup} for asynchronous channel
 * operations in the current scope. If no such group exists a new one will
 * be created with the {@link ExecutorService} of the {@link
 * CoroutineContext} and stored as {@link #CHANNEL_GROUP} in the current
 * scope.
 *
 * @param  rContinuation The channel group
 *
 * @return The channel group
 */
protected AsynchronousChannelGroup getChannelGroup(
	Continuation<?> rContinuation)
{
	AsynchronousChannelGroup rChannelGroup =
		rContinuation.getState(CHANNEL_GROUP);

	if (rChannelGroup == null)
	{
		Executor rContextExecutor = rContinuation.context().getExecutor();

		if (rContextExecutor instanceof ExecutorService)
		{
			try
			{
				rChannelGroup =
					AsynchronousChannelGroup.withThreadPool(
						(ExecutorService) rContextExecutor);
			}
			catch (IOException e)
			{
				throw new CoroutineException(e);
			}

			rContinuation.scope().set(CHANNEL_GROUP, rChannelGroup);
		}
	}

	return rChannelGroup;
}
 
Example 16
Source File: AsExecutor.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 17
Source File: AsExecutor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 18
Source File: AsExecutor.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 19
Source File: AioClient.java    From talent-aio with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * @param serverIp 可以为空
 * @param serverPort 
 * @param aioDecoder
 * @param aioEncoder
 * @param aioHandler
 *
 * @author: tanyaowu
 * @throws IOException 
 * @创建时间: 2016年11月15日 下午1:09:28
 * 
 */
public AioClient(final ClientGroupContext<SessionContext, P, R> clientGroupContext) throws IOException
{
	super();
	this.clientGroupContext = clientGroupContext;
	ExecutorService groupExecutor = clientGroupContext.getGroupExecutor();
	this.channelGroup = AsynchronousChannelGroup.withThreadPool(groupExecutor);

	startHeartbeatTask();

	startReconnTask();
}
 
Example 20
Source File: TioClient.java    From t-io with Apache License 2.0 3 votes vote down vote up
/**
 * @param serverIp 可以为空
 * @param serverPort
 * @param aioDecoder
 * @param aioEncoder
 * @param aioHandler
 *
 * @author tanyaowu
 * @throws IOException
 *
 */
public TioClient(final ClientTioConfig clientTioConfig) throws IOException {
	super();
	this.clientTioConfig = clientTioConfig;
	this.channelGroup = AsynchronousChannelGroup.withThreadPool(clientTioConfig.groupExecutor);

	startHeartbeatTask();
	startReconnTask();
}