/** * Creates a Thrift TTransport to the given address with the given timeout. All created resources * are closed if an exception is thrown. * * @param addr * The address to connect the client to * @param timeoutMillis * The timeout in milliseconds for the connection * @return A TTransport connected to the given <code>addr</code> * @throws IOException * If the transport fails to be created/connected */ public static TTransport create(SocketAddress addr, long timeoutMillis) throws IOException { return INSTANCE.createInternal(addr, timeoutMillis); }
protected InputStream wrapInputStream(Socket socket, long timeoutMillis) throws IOException { return new BufferedInputStream(getInputStream(socket, timeoutMillis), 1024 * 10); }
/** * Invokes the <code>NetUtils.getInputStream(Socket, long)</code> using reflection to handle * compatibility with both Hadoop 1 and 2. * * @param socket * The socket to create the input stream on * @param timeout * The timeout for the input stream in milliseconds * @return An InputStream on the socket */ private InputStream getInputStream(Socket socket, long timeout) throws IOException { try { return (InputStream) getNetUtilsInputStreamMethod().invoke(null, socket, timeout); } catch (Exception e) { Throwable cause = e.getCause(); // Try to re-throw the IOException directly if (cause instanceof IOException) { throw (IOException) cause; } if (e instanceof RuntimeException) { // Don't re-wrap another RTE around an RTE throw (RuntimeException) e; } else { throw new RuntimeException(e); } } }
Socket socket = null; try { socket = openSocket(addr); } catch (IOException e) { InputStream input = wrapInputStream(socket, timeoutMillis); OutputStream output = wrapOutputStream(socket, timeoutMillis); return new TIOStreamTransport(input, output); } catch (IOException e) {
addr = HostAndPort.fromParts(parts[0], 2181); transport = TTimeoutTransport.create(addr, 10 * 1000L); transport.write("stat\n".getBytes(UTF_8), 0, 5); StringBuilder response = new StringBuilder();
/** * Opens and configures a {@link Socket} for Accumulo RPC. * * @param addr * The address to connect the socket to * @return A socket connected to the given address, or null if the socket fails to connect */ protected Socket openSocket(SocketAddress addr) throws IOException { Socket socket = null; try { socket = openSocketChannel(); socket.setSoLinger(false, 0); socket.setTcpNoDelay(true); socket.connect(addr); return socket; } catch (IOException e) { try { if (socket != null) socket.close(); } catch (IOException ioe) { log.error("Failed to close socket after unsuccessful open.", e); } throw e; } }
public static TTransport create(HostAndPort addr, long timeoutMillis) throws IOException { return create(new InetSocketAddress(addr.getHost(), addr.getPort()), timeoutMillis); }
public static TTransport create(SocketAddress addr, long timeoutMillis) throws IOException { Socket socket = null; try { socket = SelectorProvider.provider().openSocketChannel().socket(); socket.setSoLinger(false, 0); socket.setTcpNoDelay(true); socket.connect(addr); InputStream input = new BufferedInputStream(getInputStream(socket, timeoutMillis), 1024 * 10); OutputStream output = new BufferedOutputStream( NetUtils.getOutputStream(socket, timeoutMillis), 1024 * 10); return new TIOStreamTransport(input, output); } catch (IOException e) { try { if (socket != null) socket.close(); } catch (IOException ioe) {} throw e; } } }
/** * Creates a Thrift TTransport to the given address with the given timeout. All created resources * are closed if an exception is thrown. * * @param addr * The address to connect the client to * @param timeoutMillis * The timeout in milliseconds for the connection * @return A TTransport connected to the given <code>addr</code> * @throws IOException * If the transport fails to be created/connected */ public static TTransport create(HostAndPort addr, long timeoutMillis) throws IOException { return INSTANCE.createInternal(new InetSocketAddress(addr.getHost(), addr.getPort()), timeoutMillis); }
private static InputStream getInputStream(Socket socket, long timeout) { try { return (InputStream) getNetUtilsInputStreamMethod().invoke(null, socket, timeout); } catch (Exception e) { throw new RuntimeException(e); } }
addr = HostAndPort.fromParts(parts[0], 2181); transport = TTimeoutTransport.create(addr, 10 * 1000l); transport.write("stat\n".getBytes(UTF_8), 0, 5); StringBuilder response = new StringBuilder();