use of org.apache.thrift.transport.TNonblockingServerSocket in project commons by twitter.
the class ThriftServer method getServerSocketFor.
/**
* Thrift doesn't provide access to socket it creates,
* this is the only way to know what ephemeral port we bound to.
* TODO: Patch thrift to provide access so we don't have to do this.
*/
@VisibleForTesting
static ServerSocket getServerSocketFor(TNonblockingServerSocket thriftSocket) throws TTransportException {
try {
Field field = TNonblockingServerSocket.class.getDeclaredField("serverSocket_");
field.setAccessible(true);
return (ServerSocket) field.get(thriftSocket);
} catch (NoSuchFieldException e) {
throw new TTransportException("Couldn't get listening port", e);
} catch (SecurityException e) {
throw new TTransportException("Couldn't get listening port", e);
} catch (IllegalAccessException e) {
throw new TTransportException("Couldn't get listening port", e);
}
}
use of org.apache.thrift.transport.TNonblockingServerSocket in project mlib by myshzzx.
the class AsyncTest1 method test2.
@Test
public void test2() throws Exception {
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress("l", 19000), 0);
TServer server = new THsHaServer(new THsHaServer.Args(serverTransport).processor(new TService1.AsyncProcessor<TService1.AsyncIface>(new Service2Impl())).protocolFactory(new TCompactProtocol.Factory()));
new Thread() {
@Override
public void run() {
server.serve();
}
}.start();
Thread.sleep(1000);
TNonblockingSocket transport = new TNonblockingSocket("l", 19000, 5000);
TService1.AsyncIface client = new TService1.AsyncClient(new TCompactProtocol.Factory(), new TAsyncClientManager(), transport);
byte[] b = { 1, 2, 3 };
AsyncMethodCallback<TService1.AsyncClient.getStr_call> h = new AsyncMethodCallback<TService1.AsyncClient.getStr_call>() {
@Override
public void onComplete(TService1.AsyncClient.getStr_call response) {
try {
System.out.println(response.getResult());
} catch (TException e) {
log.error("async get rsp fail.", e);
}
}
@Override
public void onError(Exception e) {
log.error("async call fail.", e);
}
};
for (int i = 0; i < 2; i++) {
client.getStr("mysh", ByteBuffer.wrap(b), h);
}
Thread.sleep(10000000);
}
use of org.apache.thrift.transport.TNonblockingServerSocket in project providence by morimekta.
the class NonblockingSocketClientHandlerTest method setUpServer.
@Before
public void setUpServer() throws Exception {
setDefaultPollDelay(10, TimeUnit.MILLISECONDS);
serializer = new BinarySerializer();
TProtocolFactory factory = new TBinaryProtocol.Factory();
impl = Mockito.mock(Iface.class);
TNonblockingServerTransport transport = new TNonblockingServerSocket(0);
server = new TNonblockingServer(new TNonblockingServer.Args(transport).protocolFactory(factory).processor(new Processor<>(impl)));
port = ((TNonblockingServerSocket) transport).getPort();
executor = Executors.newSingleThreadExecutor();
executor.submit(server::serve);
address = new InetSocketAddress("localhost", port);
}
use of org.apache.thrift.transport.TNonblockingServerSocket in project dubbo by apache.
the class ThriftProtocol method getTServer.
private <T> TServer getTServer(T impl, Class<T> type, URL url) {
TThreadedSelectorServer.Args tArgs = null;
String typeName = type.getName();
TServer tserver;
if (typeName.endsWith(THRIFT_IFACE)) {
String processorClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_PROCESSOR;
try {
Class<?> clazz = Class.forName(processorClsName);
Constructor constructor = clazz.getConstructor(type);
TProcessor tprocessor = (TProcessor) constructor.newInstance(impl);
processor.registerProcessor(typeName, tprocessor);
tserver = SERVER_MAP.get(url.getAddress());
if (tserver == null) {
/**
*Solve the problem of only 50 of the default number of concurrent connections
*/
TNonblockingServerSocket.NonblockingAbstractServerSocketArgs args = new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs();
/**
*1000 connections
*/
args.backlog(1000);
String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost());
if (url.getParameter(ANYHOST_KEY, false)) {
bindIp = ANYHOST_VALUE;
}
int bindPort = url.getParameter(Constants.BIND_PORT_KEY, url.getPort());
args.bindAddr(new InetSocketAddress(bindIp, bindPort));
/**
*timeout: 10s
*/
args.clientTimeout(10000);
TNonblockingServerSocket transport = new TNonblockingServerSocket(args);
tArgs = new TThreadedSelectorServer.Args(transport);
tArgs.workerThreads(200);
tArgs.selectorThreads(4);
tArgs.acceptQueueSizePerThread(256);
tArgs.processor(processor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());
} else {
// if server is starting, return and do nothing here
return null;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e);
}
}
if (tArgs == null) {
logger.error("Fail to create native thrift server(" + url + ") due to null args");
throw new RpcException("Fail to create nativethrift server(" + url + ") due to null args");
}
tserver = new TThreadedSelectorServer(tArgs);
return tserver;
}
use of org.apache.thrift.transport.TNonblockingServerSocket in project EFAK by smartloli.
the class WorkNodeServer method start.
private void start(int port) throws TTransportException {
TNonblockingServerSocket socket = new TNonblockingServerSocket(port);
final WorkNodeService.Processor processor = new WorkNodeService.Processor(new WorkNodeServiceHandler());
THsHaServer.Args arg = new THsHaServer.Args(socket);
/**
* Binary coded format efficient, intensive data transmission, The use
* of non blocking mode of transmission, according to the size of the
* block, similar to the Java of NIO.
*/
arg.protocolFactory(new TCompactProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processor));
TServer server = new THsHaServer(arg);
server.serve();
}
Aggregations