本文整理汇总了Java中java.nio.channels.AsynchronousSocketChannel.open方法的典型用法代码示例。如果您正苦于以下问题:Java AsynchronousSocketChannel.open方法的具体用法?Java AsynchronousSocketChannel.open怎么用?Java AsynchronousSocketChannel.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.AsynchronousSocketChannel
的用法示例。
在下文中一共展示了AsynchronousSocketChannel.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: retry
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private void retry() {
try {
TimeUnit.SECONDS.sleep(1);
if (null != this.channel && this.channel.isOpen()) {
this.channel.close();
}
log.debug("连接:{}", this.socketAddress.toString());
final AsynchronousSocketChannel asynchronousSocketChannel = AsynchronousSocketChannel.open(this.group);
asynchronousSocketChannel.setOption(StandardSocketOptions.TCP_NODELAY, true);
asynchronousSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
asynchronousSocketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
asynchronousSocketChannel.connect(this.socketAddress).get(5, TimeUnit.SECONDS);
this.channel = new FastChannel(asynchronousSocketChannel, this.serializer, timeout);
} catch (final Exception e) {
retry();
}
}
示例2: connect
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private void connect() throws BindException{
String errorMessage = "";
if (socketChannel == null || !socketChannel.isOpen() || closed) {
try {
socketChannel = AsynchronousSocketChannel.open();
socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
socketChannel.connect(new InetSocketAddress(ip, port)).get(timeout, TimeUnit.MILLISECONDS);
closed = false;
//when connect to the server, keep receiving data either server response or server call
receive();
} catch (Exception e) {
log.error("Connection error: " + e.getMessage());
errorMessage = e.getMessage();
}
}
if (socketChannel == null) {
throw new BindException(errorMessage);
}
}
示例3: connect
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
private void connect() throws BindException{
String errorMessage = "";
if (socketChannel == null || !socketChannel.isOpen() || closed) {
try {
socketChannel = AsynchronousSocketChannel.open();
socketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);
socketChannel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
socketChannel.connect(new InetSocketAddress(ip, port)).get(timeout, TimeUnit.MILLISECONDS);
closed = false;
log.debug("Session start to " + socketChannel.getRemoteAddress());
//when connect to the server, keep receiving data either server response or server call
receive();
} catch (Exception e) {
log.error("Connection error: " + e.getMessage());
errorMessage = e.getMessage();
}
}
if (socketChannel == null) {
throw new BindException(errorMessage);
}
}
示例4: 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);
}
}
示例5: SocketClient
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public SocketClient(PoolManager pool_manager, InetSocketAddress server, Consumer<Node> callback_on_connection) throws IOException {
if (pool_manager == null) {
throw new NullPointerException("\"pool_manager\" can't to be null");
}
this.distant_server_addr = server;
if (server == null) {
throw new NullPointerException("\"server\" can't to be null");
}
this.callback_on_connection = callback_on_connection;
if (callback_on_connection == null) {
throw new NullPointerException("\"callback_on_connection\" can't to be null");
}
handler_connect = new SocketConnect();
channel = AsynchronousSocketChannel.open(pool_manager.getChannelGroup());
channel.connect(server, new Node(this, pool_manager, channel), handler_connect);
}
示例6: openSocketChannel
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
protected NetworkChannel openSocketChannel(boolean isAIO)
throws IOException {
if (isAIO) {
return AsynchronousSocketChannel
.open(MycatServer.getInstance().getNextAsyncChannelGroup());
} else {
SocketChannel channel = null;
channel = SocketChannel.open();
channel.configureBlocking(false);
return channel;
}
}
示例7: 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());
}
}
}
示例8: AsyncTimeClientHandler
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public AsyncTimeClientHandler(String host, int port) {
this.port = port;
this.host = host;
try{
this.asynchronousSocketChannel = AsynchronousSocketChannel.open();
}catch(Exception e){
e.printStackTrace();
}
}
示例9: AsyncClientHandler
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public AsyncClientHandler(String host, int port) {
this.host = host;
this.port = port;
try {
// 创建异步的客户端通道
clientChannel = AsynchronousSocketChannel.open();
} catch (IOException e) {
e.printStackTrace();
}
}
示例10: openSocketChannel
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
protected NetworkChannel openSocketChannel(boolean isAIO)
throws IOException {
if (isAIO) {
return AsynchronousSocketChannel.open(DbleServer.getInstance().getNextAsyncChannelGroup());
} else {
SocketChannel channel = null;
channel = SocketChannel.open();
channel.configureBlocking(false);
return channel;
}
}
示例11: connect
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
@Override
public void connect(String host, int port, AioClientListener listener) {
this.host = host;
this.port = port;
this.listener = listener;
try {
socketChannel = AsynchronousSocketChannel.open();
socketChannel.connect(new InetSocketAddress(host, port), null, this);
} catch (IOException e) {
logger.warn(e, "连接到服务端[{}:{}]时发生异常!", host, port);
}
}
示例12: ready
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
void ready() {
try {
channel = AsynchronousSocketChannel.open();
channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
Future<Void> future = channel.connect(remote.getAddress());
if (future.get() != null) {
log.log(Level.WARNING, "Can't connect to {0}", remote);
} else {
log.log(Level.INFO, "Connected to server {0}", remote);
}
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: AsyncClientHandler
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public AsyncClientHandler(String host, int port) {
this.host = host;
this.port = port;
try {
//创建异步的客户端通道
clientChannel = AsynchronousSocketChannel.open();
} catch (IOException e) {
e.printStackTrace();
}
}
示例14: AsyncTimeClientHandler
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
public AsyncTimeClientHandler(String host, int port) {
this.host = host;
this.port = port;
try {
client = AsynchronousSocketChannel.open();
} catch (IOException e) {
e.printStackTrace();
}
}
示例15: openSocketChannel
import java.nio.channels.AsynchronousSocketChannel; //导入方法依赖的package包/类
protected AsynchronousSocketChannel openSocketChannel(
AsynchronousChannelGroup asynchronousChannelGroup) throws IOException {
AsynchronousSocketChannel channel =
AsynchronousSocketChannel.open(asynchronousChannelGroup);
channel.setOption(StandardSocketOptions.TCP_NODELAY, true);
channel.setOption(StandardSocketOptions.SO_REUSEADDR, false);
channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
return channel;
}