Java Code Examples for org.apache.thrift.transport.TFramedTransport#open()

The following examples show how to use org.apache.thrift.transport.TFramedTransport#open() . 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: CoronaTaskTracker.java    From RDFS with Apache License 2.0 7 votes vote down vote up
private synchronized void initializeClusterManagerClient()
    throws IOException {
  // Connect to cluster manager thrift service
  String target = CoronaConf.getClusterManagerAddress(fConf);
  LOG.info("Connecting to Cluster Manager at " + target);
  InetSocketAddress address = NetUtils.createSocketAddr(target);
  transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  TProtocol protocol = new TBinaryProtocol(transport);
  client = new ClusterManagerService.Client(protocol);
  try {
    transport.open();
  } catch (TTransportException e) {
    throw new IOException(e);
  }
}
 
Example 2
Source File: ConnectionPool.java    From suro with Apache License 2.0 6 votes vote down vote up
public void connect() throws Exception {
    TSocket socket = new TSocket(server.getHost(), server.getPort(), config.getConnectionTimeout());
    socket.getSocket().setTcpNoDelay(true);
    socket.getSocket().setKeepAlive(true);
    socket.getSocket().setSoLinger(true, 0);
    transport = new TFramedTransport(socket);
    transport.open();

    TProtocol protocol = new TBinaryProtocol(transport);

    client = new SuroServer.Client(protocol);
    ServiceStatus status = client.getStatus();
    if (status != ServiceStatus.ALIVE) {
        transport.close();
        throw new RuntimeException(server + " IS NOT ALIVE!!!");
    }
}
 
Example 3
Source File: CassandraConnection.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Construct an CassandaraConnection with optional authentication.
 * 
 * @param host the host to connect to
 * @param port the port to use
 * @param username the username to authenticate with (may be null
 * for no authentication)
 * @param password the password to authenticate with (may be null
 * for no authentication)
 * @throws Exception if the connection fails
 */
public CassandraConnection(String host, int port,
    String username, String password, int timeout) throws Exception {
  TSocket socket = new TSocket(host, port);
  if (timeout > 0) {
    socket.setTimeout(timeout);
  }
  
  m_transport = new TFramedTransport(socket);
  TProtocol protocol = new TBinaryProtocol(m_transport);
  m_client = new Cassandra.Client(protocol);      
  m_transport.open();
  
  if (!Const.isEmpty(username) && !Const.isEmpty(password)) {
    Map<String, String> creds = new HashMap<String, String>();
    creds.put("username", username);
    creds.put("password", password);
    m_client.login(new AuthenticationRequest(creds));
  }
}
 
Example 4
Source File: CoronaAdmin.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Persists the state of the ClusterManager
 * @return 0 if successful.
 * @throws IOException
 */
private int persistState() throws IOException {
  // Get the current configuration
  CoronaConf conf = new CoronaConf(getConf());

  InetSocketAddress address = NetUtils.createSocketAddr(conf
    .getClusterManagerAddress());
  TFramedTransport transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  ClusterManagerService.Client client = new ClusterManagerService.Client(
    new TBinaryProtocol(transport));

  try {
    transport.open();
    if (!client.persistState())  {
      System.err.println("Persisting Cluster Manager state failed. ");
    }
  } catch (TException e) {
    throw new IOException(e);
  }

  return 0;
}
 
Example 5
Source File: CoronaAdmin.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Turns on the Safe Mode if safeMode is true. Turns off the Safe Mode if
 * safeMode is false.
 * @param safeMode Is true if we want the Safe Mode to be on. false
 *                 otherwise.
 * @return 0 if successful.
 * @throws IOException
 */
private int setSafeMode(boolean safeMode) throws IOException {
  // Get the current configuration
  CoronaConf conf = new CoronaConf(getConf());

  InetSocketAddress address = NetUtils.createSocketAddr(conf
    .getClusterManagerAddress());
  TFramedTransport transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  ClusterManagerService.Client client = new ClusterManagerService.Client(
    new TBinaryProtocol(transport));

  try {
    transport.open();
    if (client.setSafeMode(safeMode)) {
      System.out.println("The safeMode is: " +
                          (safeMode ? "ON" : "OFF"));
    } else {
      System.err.println("Could not set the safeMode flag");
    }
  } catch (TException e) {
    throw new IOException(e);
  }

  return 0;
}
 
Example 6
Source File: ClusterManagerAvailabilityChecker.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Used for getting a client to the CoronaProxyJobTracker
 * @param conf
 * @return Returns a client to the CPJT
 * @throws IOException
 */
public static CoronaProxyJobTrackerService.Client
  getPJTClient(CoronaConf conf) throws IOException {
  InetSocketAddress address =
    NetUtils.createSocketAddr(conf.getProxyJobTrackerThriftAddress());
  TFramedTransport transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  CoronaProxyJobTrackerService.Client client =
    new CoronaProxyJobTrackerService.Client(new TBinaryProtocol(transport));
  try {
    transport.open();
  } catch (TException e) {
    LOG.info("Transport Exception: ", e);
  }
  return client;
}
 
Example 7
Source File: ThriftUserServiceClient.java    From rpc-benchmark with Apache License 2.0 5 votes vote down vote up
public ThriftUserServiceClient(String host, int port) {
	transport = new TFramedTransport(new TSocket(host, port));
	protocol = new TBinaryProtocol(transport);
	client = new UserService.Client(protocol);

	try {
		transport.open();
	} catch (TTransportException e) {
		throw new Error(e);
	}
}
 
Example 8
Source File: AbstractClient.java    From luxun with Apache License 2.0 5 votes vote down vote up
protected void connect() {
	long connectBackoffMs = 1;
	long beginTimeMs = System.currentTimeMillis();
	while(luxunClient == null && !shutdown) {
		try {
			TSocket socket = new TSocket(host, port, soTimeoutMs);
			transport = new TFramedTransport(socket);
			TProtocol protocol = new TBinaryProtocol(transport);
			luxunClient = new QueueService.Client(protocol);
			transport.open();
            logger.info("Connected to " + host + ":" + port + " for operating");
		} catch (TTransportException e) {
			disconnect();
			long endTimeMs = System.currentTimeMillis();
               if ((endTimeMs - beginTimeMs + connectBackoffMs) > connectTimeoutMs) {
                   logger.error(
                           "Connection attempt to " + host + ":" + port + " timing out after " + connectTimeoutMs + " ms",
                           e);
                   throw new ConnectionRefusedException(host + ":" + port, e);
               }
               logger.error(
                       "Connection attempt to " + host+ ":" + port + " failed, next attempt in " + connectBackoffMs + " ms",
                       e);
               try {
                   Thread.sleep(connectBackoffMs);
               } catch (InterruptedException e1) {
                   logger.warn(e1.getMessage());
                   Thread.currentThread().interrupt();
               }
               connectBackoffMs = Math.min(10 * connectBackoffMs, MaxConnectBackoffMs);
		}
	}
}
 
Example 9
Source File: CoronaClient.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Get the thrift client to communicate with the cluster manager
 * @return a thrift client initialized to talk to the cluster manager
 * @param conf The configuration.
 * @throws TTransportException
 */
private static ClusterManagerService.Client getCMSClient(CoronaConf conf)
  throws TTransportException {
  InetSocketAddress address = NetUtils.createSocketAddr(conf
      .getClusterManagerAddress());
  TFramedTransport transport = new TFramedTransport(
    new TSocket(address.getHostName(), address.getPort()));
  ClusterManagerService.Client client = new ClusterManagerService.Client(
      new TBinaryProtocol(transport));
  transport.open();
  return client;
}
 
Example 10
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.InitializationException
 */
public void initCassandraConnection() throws InitializationException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, cassandraPort));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new InitializationException(message, ex, getSymbolicName());
  }
}
 
Example 11
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.ProcessingException
 */
public void openCassandraConnection() throws ProcessingException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, 9160));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new ProcessingException(message, ex, getSymbolicName());
  }
}
 
Example 12
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.ProcessingException
 */
public void openCassandraConnection() throws ProcessingException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, 9160));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new ProcessingException(message, ex, getSymbolicName());
  }
}
 
Example 13
Source File: AbstractCassandraOutputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to connect to Cassandra.
 *
 * @throws OpenRate.exception.InitializationException
 */
public void initCassandraConnection() throws InitializationException {
  tr = new TFramedTransport(new TSocket(cassandraIPAddr, cassandraPort));
  TProtocol proto = new TBinaryProtocol(tr);
  client = new Cassandra.Client(proto);

  try {
    tr.open();
  } catch (TTransportException ex) {
    message = "Transport exception opening Cassandra transport";
    throw new InitializationException(message, ex, getSymbolicName());
  }
}
 
Example 14
Source File: ThriftConnectionFactory.java    From ThriftJ with Apache License 2.0 5 votes vote down vote up
@Override
public PooledObject<TTransport> makeObject(ThriftServer thriftServer) throws Exception {
	TSocket tsocket = new TSocket(thriftServer.getHost(), thriftServer.getPort());
	tsocket.setTimeout(timeout);
	TFramedTransport transport = new TFramedTransport(tsocket);
	
	transport.open();
	DefaultPooledObject<TTransport> result = new DefaultPooledObject<TTransport>(transport);
	logger.trace("Make new thrift connection: {}:{}", thriftServer.getHost(), thriftServer.getPort());
	
	return result;
}
 
Example 15
Source File: OriginThriftTest.java    From octo-rpc with Apache License 2.0 5 votes vote down vote up
private static Twitter.Iface getOriginThriftClient() throws TTransportException {
    transport = new TFramedTransport(new TSocket("localhost", serverPort));
    transport.open();
    TProtocol protocol = new TBinaryProtocol(transport);
    Twitter.Iface client = new Twitter.Client(protocol);
    return client;
}
 
Example 16
Source File: OFMessageFilterManager.java    From floodlight_with_topoguard with Apache License 2.0 4 votes vote down vote up
@LogMessageDoc(level="ERROR",
               message="Failed to establish connection with the " +
                       "packetstreamer server.",
               explanation="The message tracing server is not running " +
               		"or otherwise unavailable.",
               recommendation=LogMessageDoc.CHECK_CONTROLLER)
public boolean connectToPSServer() {
    int numRetries = 0;
    if (transport != null && transport.isOpen()) {
        return true;
    }

    while (numRetries++ < MaxRetry) {
        try {
            TFramedTransport t = 
                    new TFramedTransport(new TSocket("localhost", 
                                                     serverPort));
            t.open();

            TProtocol protocol = new  TBinaryProtocol(t);
            packetClient = new PacketStreamer.Client(protocol);

            log.debug("Have a connection to packetstreamer server " +
            		  "localhost:{}", serverPort);
            transport = t;
            break;
        } catch (TException x) {
            try {
                // Wait for 1 second before retry
                if (numRetries < MaxRetry) {
                    Thread.sleep(1000);
                }
            } catch (Exception e) {}
        } 
    }

    if (numRetries > MaxRetry) {
        log.error("Failed to establish connection with the " +
        		  "packetstreamer server.");
        return false;
    }
    return true;
}
 
Example 17
Source File: ThriftQueueClientDemo.java    From bigqueue with Apache License 2.0 4 votes vote down vote up
private void initClient() throws TTransportException {
	transport = new TFramedTransport(new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT));
	TProtocol protocol = new TBinaryProtocol(transport);
	client = new BigQueueService.Client(protocol);
	transport.open();
}
 
Example 18
Source File: TestClient.java    From bigqueue with Apache License 2.0 4 votes vote down vote up
public TestClient() throws TTransportException {
	transport = new TFramedTransport(new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT));
	TProtocol protocol = new TBinaryProtocol(transport);
	innerClient = new BigQueueService.Client(protocol);
	transport.open();
}