当前位置: 首页>>代码示例>>Java>>正文


Java AsynchronousSocketChannel.close方法代码示例

本文整理汇总了Java中java.nio.channels.AsynchronousSocketChannel.close方法的典型用法代码示例。如果您正苦于以下问题:Java AsynchronousSocketChannel.close方法的具体用法?Java AsynchronousSocketChannel.close怎么用?Java AsynchronousSocketChannel.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.AsynchronousSocketChannel的用法示例。


在下文中一共展示了AsynchronousSocketChannel.close方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: startClient

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@SuppressWarnings("resource")
public void startClient(SocketAddress addr, Object attachment, AsynchronousChannelGroup group)
{
	AsynchronousSocketChannel channel = null;
	try
	{
		channel = AsynchronousSocketChannel.open(group);
		int recvBufSize = onChannelCreated(channel, attachment);
		if(recvBufSize >= 0)
			channel.connect(addr, new ConnectParam(channel, recvBufSize), _connectHandler);
		else
			channel.close();
	}
	catch(Throwable e)
	{
		doException(null, e);
		closeChannel(channel);
	}
}
 
开发者ID:dwing4g,项目名称:jane,代码行数:20,代码来源:TcpManager.java

示例2: cleanUpChannel

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private void cleanUpChannel(AsynchronousSocketChannel channel) {
	try {
		if (channel != null) {
			channel.close();
		}
	} catch (Exception e){
		// Ignore
	}
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:10,代码来源:MultipleReadWriteFutureTest.java

示例3: testSingleConnect

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public void testSingleConnect() throws IOException, ExecutionException, InterruptedException, TimeoutException {
	AsynchronousSocketChannel serverConnectionChannel = null;
	try {
		assertNotNull(serverAddress);

		serverConnectionChannel = AsynchronousSocketChannel.open();
		serverConnectionChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
		
		// Ensure we have have returned an open connection
		assertNotNull(serverConnectionChannel);
		assertTrue("Channel is not open", serverConnectionChannel.isOpen());

		// Blocking connect
		Future<Void> future = serverConnectionChannel.connect(serverAddress);
		future.get(getTimeout(), TimeUnit.MILLISECONDS);
		
		// Ensure we are connected
		assertNotNull("Unable to get remote address", serverConnectionChannel.getRemoteAddress());
		
	} finally {
		if (serverConnectionChannel != null) {
			try {
				serverConnectionChannel.close();
			} catch (ClosedChannelException cce) {
				// That's ok
			}
			assertFalse("Channel was not closed", serverConnectionChannel.isOpen());
		}
	}
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:31,代码来源:MultipleConnectFutureTest.java

示例4: onClose

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public static void onClose(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {
    outputBuffer = encodeUnmaskedFrame(1, "Connection Closed");
    outputBuffer.flip();
    outputBuffer.rewind();
    while (outputBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(outputBuffer);
    }
    ByteBuffer closeBuffer = encodeUnmaskedFrame(8, "");
    closeBuffer.flip();
    while (closeBuffer.hasRemaining()) {
        asynchronousSocketChannel.write(closeBuffer);
    }
    connectionOpen = false;
    asynchronousSocketChannel.close();
}
 
开发者ID:ThreaT,项目名称:WebServers,代码行数:16,代码来源:WebSocketServer.java

示例5: closeChannel

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private static void closeChannel(AsynchronousSocketChannel channel) {
	if (channel == null) {
		return;
	}
	try {
		channel.close();
	} catch (IOException e) {
	}
}
 
开发者ID:youngor,项目名称:openclouddb,代码行数:10,代码来源:NIOAcceptor.java

示例6: completed

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@SuppressWarnings("resource")
@Override
public void completed(AsynchronousSocketChannel channel, Object attachment)
{
	beginAccept();
	TcpSession session = null;
	try
	{
		int recvBufSize = onChannelCreated(channel, attachment);
		if(recvBufSize < 0)
		{
			channel.close();
			return;
		}
		session = new TcpSession(TcpManager.this, channel, recvBufSize);
		_sessions.put(session, session);
		onSessionCreated(session);
		session.beginRecv();
	}
	catch(Throwable e)
	{
		doException(session, e);
		if(session != null)
			session.close(TcpSession.CLOSE_EXCEPTION);
		else
			closeChannel(channel);
	}
}
 
开发者ID:dwing4g,项目名称:jane,代码行数:29,代码来源:TcpManager.java

示例7: testMultipleConnectSequential

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public void testMultipleConnectSequential() throws IOException, ExecutionException, InterruptedException, TimeoutException {
	assertNotNull(serverAddress);
	System.out.println("MultipleConnectFutureTest.testMultipleConnectSequential() creating " +
			numberOfConnections + " connections");
	for (int counter = 0; counter < numberOfConnections; counter++) {
		HangNotifier.ping();
		AsynchronousSocketChannel serverConnectionChannel = null;
		try {
			serverConnectionChannel = AsynchronousSocketChannel.open();
			serverConnectionChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
			
			// Ensure we have have returned an open connection
			assertNotNull(serverConnectionChannel);
			assertTrue("Channel is not open on iteration " + counter, serverConnectionChannel.isOpen());
	
			// Blocking connect
			Future<Void> future = serverConnectionChannel.connect(serverAddress);
			future.get(getTimeout(), TimeUnit.MILLISECONDS);
			
			// Ensure we are connected
			assertNotNull("Unable to get remote address on iteration " + counter, serverConnectionChannel.getRemoteAddress());
		} finally {
			if (serverConnectionChannel != null) {
				try {
					serverConnectionChannel.close();
				} catch (ClosedChannelException cce) {
					// That's ok
				} catch (IOException ioe) {
					throw ioe;
				}
				assertFalse("Channel was not closed on iteration " + counter, serverConnectionChannel.isOpen());
			}
		}
		
		// We can't hammer the server continuously, or we'll run out of resource.
		// Sleep before the test claims completion
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {}
	}
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-systemtest,代码行数:42,代码来源:MultipleConnectFutureTest.java

示例8: addNode

import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@RestMapping(name = "addnode", auth = false, comment = "动态增加指定Group的Node节点")
public RetResult addNode(@RestParam(name = "group", comment = "Group节点名") final String group,
    @RestParam(name = "addr", comment = "节点IP") final String addr,
    @RestParam(name = "port", comment = "节点端口") final int port) throws IOException {
    InetSocketAddress address;
    try {
        address = new InetSocketAddress(addr, port);
        AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
        channel.connect(address).get(2, TimeUnit.SECONDS);  //连接超时2秒
        channel.close();
    } catch (Exception e) {
        return new RetResult(RET_TRANSPORT_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is illegal or cannot connect");
    }
    if (transportFactory.findGroupName(address) != null) return new RetResult(RET_TRANSPORT_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is exists");
    synchronized (this) {
        if (transportFactory.findGroupInfo(group) == null) {
            return new RetResult(RET_TRANSPORT_GROUP_NOT_EXISTS, "not found group (" + group + ")");
        }
        transportFactory.addGroupInfo(group, address);
        for (Service service : transportFactory.getServices()) {
            if (!Sncp.isSncpDyn(service)) continue;
            SncpClient client = Sncp.getSncpClient(service);
            if (Sncp.isRemote(service)) {
                if (client.getRemoteGroups() != null && client.getRemoteGroups().contains(group)) {
                    client.getRemoteGroupTransport().addRemoteAddresses(address);
                }
            } else {
                if (group.equals(client.getSameGroup())) {
                    client.getSameGroupTransport().addRemoteAddresses(address);
                }
                if (client.getDiffGroups() != null && client.getDiffGroups().contains(group)) {
                    for (Transport transport : client.getDiffGroupTransports()) {
                        transport.addRemoteAddresses(address);
                    }
                }
            }
        }
        DefaultAnyValue node = DefaultAnyValue.create("addr", addr).addValue("port", port);
        for (AnyValue groupconf : application.getAppConfig().getAnyValue("resources").getAnyValues("group")) {
            if (group.equals(groupconf.getValue("name"))) {
                ((DefaultAnyValue) groupconf).addValue("node", node);
                break;
            }
        }
        application.restoreConfig();
    }
    return RetResult.success();
}
 
开发者ID:redkale,项目名称:redkale,代码行数:49,代码来源:TransportWatchService.java


注:本文中的java.nio.channels.AsynchronousSocketChannel.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。