/** * 根据rc的设置来确定创建什么类型的transport; * * @param instance * @return */ protected TTransport createNativeTransport( ServiceInstance<RpcPayload> instance) { TSocket socket = new TSocket(instance.getAddress(), instance.getPort()); socket.setTimeout(socketTimeout); RpcPayload server = instance.getPayload(); if ((server == null) || (server.getTransport() == null) || (server.getTransport().equals("socket"))) { return socket; } else if ("framed-transport".equals(server.getTransport())) { return new TFramedTransport(socket); } // for default, use TSocket; return socket; }
private static int logThrift(HostAndPort address, List<LogEntry> messages) { try { TSocket socket = new TSocket(address.getHost(), address.getPort()); socket.open(); try { TBinaryProtocol tp = new TBinaryProtocol(new TFramedTransport(socket)); assertEquals(new scribe.Client(tp).Log(messages), ResultCode.OK); } finally { socket.close(); } } catch (TException e) { throw new RuntimeException(e); } return 1; }
public void run() { try { Scribe.Processor processor = new Scribe.Processor(new Receiver()); TNonblockingServerTransport transport = new TNonblockingServerSocket(port); THsHaServer.Args args = new THsHaServer.Args(transport); args.workerThreads(workers); args.processor(processor); args.transportFactory(new TFramedTransport.Factory(maxReadBufferBytes)); args.protocolFactory(new TBinaryProtocol.Factory(false, false)); args.maxReadBufferBytes = maxReadBufferBytes; server = new THsHaServer(args); LOG.info("Starting Scribe Source on port " + port); server.serve(); } catch (Exception e) { LOG.warn("Scribe failed", e); } }
@Test public void testScribeMessage() throws Exception { TTransport transport = new TFramedTransport(new TSocket("localhost", port)); TProtocol protocol = new TBinaryProtocol(transport); Scribe.Client client = new Scribe.Client(protocol); transport.open(); LogEntry logEntry = new LogEntry("INFO", "Sending info msg to scribe source"); List<LogEntry> logEntries = new ArrayList<LogEntry>(1); logEntries.add(logEntry); client.Log(logEntries); // try to get it from Channels Transaction tx = memoryChannel.getTransaction(); tx.begin(); Event e = memoryChannel.take(); Assert.assertNotNull(e); Assert.assertEquals("Sending info msg to scribe source", new String(e.getBody())); tx.commit(); tx.close(); }
/** * The function to create a thrift Half-Sync and Half-Async Server. * @param processor */ public static void hshaServer(PacketStreamer.Processor<PacketStreamerHandler> processor) { try { TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(port); THsHaServer.Args args = new THsHaServer.Args(serverTransport); args.processor(processor); args.transportFactory(new TFramedTransport.Factory()); args.protocolFactory(new TBinaryProtocol.Factory(true, true)); TServer server = new THsHaServer(args); log.info("Starting the packetstreamer hsha server on port {} ...", port); server.serve(); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args){ ExecutorService es = Executors.newFixedThreadPool(2); for(int i=0; i<ports.length; i++){ final int index = i; es.execute(new Runnable() { @Override public void run() { try{ TNonblockingServerSocket socket = new TNonblockingServerSocket(ports[index]); TestThriftJ.Processor processor = new TestThriftJ.Processor(new QueryImp()); TNonblockingServer.Args arg = new TNonblockingServer.Args(socket); arg.protocolFactory(new TBinaryProtocol.Factory()); arg.transportFactory(new TFramedTransport.Factory()); arg.processorFactory(new TProcessorFactory(processor)); TServer server = new TNonblockingServer (arg); logger.info("127.0.0.1:" + ports[index] + " start"); server.serve(); }catch(Exception e){ logger.error("127.0.0.1:" + ports[index] + " error"); } } }); } }
public static void main(String[] args){ ExecutorService es = Executors.newFixedThreadPool(2); for(int i=0; i<ports.length; i++){ final int index = i; es.execute(new Runnable() { @Override public void run() { try{ TNonblockingServerSocket socket = new TNonblockingServerSocket(ports[index]); TestThriftJ.Processor processor = new TestThriftJ.Processor(new QueryImp()); TNonblockingServer.Args arg = new TNonblockingServer.Args(socket); arg.protocolFactory(new TBinaryProtocol.Factory()); arg.transportFactory(new TFramedTransport.Factory()); arg.processorFactory(new TProcessorFactory(processor)); TServer server = new TNonblockingServer(arg); logger.info("127.0.0.1:" + ports[index] + " start"); server.serve(); }catch(Exception e){ logger.error("127.0.0.1:" + ports[index] + " error"); } } }); } }
public static void startClient2(String ip ,int port ,int timeout) throws Exception { TTransport transport = new TFramedTransport(new TSocket(ip,port,timeout)); TProtocol protocol = new TBinaryProtocol(transport); leafrpc.Client client = new leafrpc.Client(protocol); transport.open(); for(int i = 0; i< 1000000; i++) { client.getID(""); if (i % 100000 == 0) { System.out.println(Thread.currentThread().getName() + " " + client.getID("")); } //ai.incrementAndGet(); } transport.close(); }
@Override public TServiceClient makeObject() throws Exception { InetSocketAddress address = serverAddressProvider.selector(); if(address==null){ new ThriftException("No provider available for remote service"); } TSocket tsocket = new TSocket(address.getHostName(), address.getPort()); TTransport transport = new TFramedTransport(tsocket); TProtocol protocol = new TBinaryProtocol(transport); TServiceClient client = this.clientFactory.getClient(protocol); transport.open(); if (callback != null) { try { callback.make(client); } catch (Exception e) { logger.warn("makeObject:{}", e); } } return client; }
public void start(CountDownLatch latch, int port) { try { TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(port); //异步IO,需要使用TFramedTransport,它将分块缓存读取。 TTransportFactory transportFactory = new TFramedTransport.Factory(); //使用高密度二进制协议 TProtocolFactory proFactory = new TBinaryProtocol.Factory(); //发布多个服务 TMultiplexedProcessor processor = new TMultiplexedProcessor(); processor.registerProcessor(ClassNameUtils.getClassName(Hello.class), new Hello.Processor<>(new HelloServer())); TServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args(serverTransport) .transportFactory(transportFactory) .protocolFactory(proFactory) .processor(processor) ); System.out.println("Starting the hello server..."); latch.countDown(); server.serve(); } catch (Exception e) { e.printStackTrace(); } }
private static void connectToCMD() { QueryInput query_input = new QueryInput(); query_input.type = "ensemble"; query_input.data = new ArrayList<String>(); query_input.data.add("localhost"); query_input.tags = new ArrayList<String>(); query_input.tags.add("9090"); QuerySpec spec = new QuerySpec(); spec.content = new ArrayList<QueryInput>(); spec.content.add(query_input); // Initialize thrift objects. TTransport transport = new TSocket("localhost", 8080); TProtocol protocol = new TBinaryProtocol(new TFramedTransport(transport)); LucidaService.Client client = new LucidaService.Client(protocol); try { transport.open(); System.out.println("Connecting to CMD at port " + 8080); // Register itself to CMD. client.create("", spec); transport.close(); System.out.println("Successfully connected to CMD"); } catch (TException x) { x.printStackTrace(); } }
private static boolean createThrift() { String flag = getClassName() + ".createThrift"; try { TProcessor tProcessor = new FrcService.Processor<FrcService.Iface>(theInstance); TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(thrift_port); TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport); tnbArgs.maxReadBufferBytes = DefaultValues.THRIFT_MAX_READ_BUF; tnbArgs.processor(tProcessor); // tnbArgs.transportFactory(new LCQTFramedTransport.Factory()); tnbArgs.transportFactory(new TFramedTransport.Factory()); tnbArgs.protocolFactory(new TBinaryProtocol.Factory()); server = new TNonblockingServer(tnbArgs); // server.setServerEventHandler(new LCQTServerEventHandler()); return true; } catch (Exception e) { FRCLogger.getInstance().warn(-100, flag, "exception", e); return false; } }
@SuppressWarnings("unchecked") @Override public PooledObject makeObject() throws Exception { //logger.debug("makeObject..........."); try { String host = this.host; int port = this.port; int timeout = this.timeout; TFramedTransport transport = new TFramedTransport(new TSocket(host, port, timeout)); TBinaryProtocol protocol = new TBinaryProtocol(transport); FrcService.Client client = new FrcService.Client(protocol, protocol); transport.open(); RpcClient<Client> rpcClient = new RpcClient(client, transport, 1); return this.wrap(rpcClient); } catch (Exception e) { logger.error("exception", e); return null; } }
/** * 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; }
/** * 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; }
/** * 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; }
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); } }
private void setup(String server, int port) throws Exception { /* Establish a thrift connection to the cassandra instance */ TSocket socket = new TSocket(server, port); System.out.println(" connected to " + server + ":" + port + "."); TBinaryProtocol binaryProtocol = new TBinaryProtocol(new TFramedTransport(socket)); Cassandra.Client cassandraClient = new Cassandra.Client(binaryProtocol); socket.open(); thriftClient = cassandraClient; String seed = DatabaseDescriptor.getSeeds().iterator().next().getHostAddress(); conf = new Configuration(); ConfigHelper.setOutputPartitioner(conf, DatabaseDescriptor.getPartitioner().getClass().getName()); ConfigHelper.setOutputInitialAddress(conf, seed); ConfigHelper.setOutputRpcPort(conf, Integer.toString(DatabaseDescriptor.getRpcPort())); }
private static Cassandra.Client createThriftClient(String host, int port, String user, String passwd) throws Exception { TSocket socket = new TSocket(host, port); TTransport trans = new TFramedTransport(socket); trans.open(); TProtocol protocol = new TBinaryProtocol(trans); Cassandra.Client client = new Cassandra.Client(protocol); if (user != null && passwd != null) { Map<String, String> credentials = new HashMap<String, String>(); credentials.put(IAuthenticator.USERNAME_KEY, user); credentials.put(IAuthenticator.PASSWORD_KEY, passwd); AuthenticationRequest authenticationRequest = new AuthenticationRequest(credentials); client.login(authenticationRequest); } return client; }
/** * Write the frame at the current state, and reset the buffer to be able to * generate a new frame. * * @throws IOException On failed write. */ public void completeFrame() throws IOException { int frameSize = buffer.position(); if (frameSize > 0) { TFramedTransport.encodeFrameSize(frameSize, frameSizeBuffer); buffer.flip(); synchronized (out) { out.write(ByteBuffer.wrap(frameSizeBuffer)); while (buffer.hasRemaining()) { out.write(buffer); } } buffer.rewind(); buffer.limit(buffer.capacity()); } }
/** * @return */ public boolean initialize() { for (final ClientMetadata metadata : clientsMeta) { try { log.info("Opening server socket on port = {}, host name = {}", metadata.getPortNum(), metadata.getHostName()); TTransport socket = new TFramedTransport(new TSocket(metadata.getHostName(), metadata.getPortNum())); final TBinaryProtocol protocol = new TBinaryProtocol(socket); final CoresetService.Client client = new CoresetService.Client(protocol); socket.open(); log.info("Initializing coreset service with parameters, " + "k = {}, sample size = {} and algorithm = {}", kValue, sampleSize, coresetAlgName); if (!client.initialize(kValue, sampleSize, coresetAlgName)) { log.error("Server wasn't able to accept initialization parameters, please read server logs."); return false; } metadata.setSocket(socket); clients.put(metadata, client); } catch (Exception e) { e.printStackTrace(); return false; } } return true; }
@Test public void connectToKeyspace() throws Exception { TTransport tr = new TFramedTransport(new TSocket("localhost", Integer.getInteger( "rpcPort", 9160 ))); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); try { assertThat(client.describe_keyspace("testkeyspace").getStrategy_options().entrySet(), hasItem((Map.Entry<String, String>)new AbstractMap.SimpleEntry<String,String>("replication_factor","1"))); } finally { tr.close(); } }
@Test public void connectToKeyspace() throws Exception { TTransport tr = new TFramedTransport(new TSocket("localhost", Integer.getInteger( "rpcPort", 9160 ))); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); try { assertThat(client.describe_keyspace("testkeyspacewithspace").getStrategy_options().entrySet(), hasItem((Map.Entry<String, String>)new AbstractMap.SimpleEntry<String,String>("replication_factor","1"))); } finally { tr.close(); } }
private boolean open() { try { TSocket socket = new TSocket((String) config.get(KEY_SCRIBE_HOST), TypeUtils.getInteger(config.get(KEY_SCRIBE_PORT)), TypeUtils.getInteger(config.get(KEY_SCRIBE_TIMEOUT_MS))); transport = new TFramedTransport(socket); transport.open(); } catch (TException tx) { LOG.log(Level.SEVERE, "Failed to open connection to scribe server " + connectionString(), tx); return false; } LOG.info("Opened connection to scribe server " + connectionString()); TProtocol protocol = new TBinaryProtocol(transport); client = new scribe.Client(protocol); return true; }
public static void main(String[] args) throws IOException { try { TTransport transport = new TFramedTransport(new TSocket("localhost", 9090)); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); Calculator.Client client = new Calculator.Client(protocol); perform(client); transport.close(); } catch (TException x) { x.printStackTrace(); } }