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();
}
use of org.apache.thrift.transport.TNonblockingServerSocket in project iotdb by apache.
the class ThriftServiceThread method openNonblockingTransport.
private TServerTransport openNonblockingTransport(String bindAddress, int port, int connectionTimeoutInMS) throws TTransportException {
int maxRetry = 5;
long retryIntervalMS = 5000;
TTransportException lastExp = null;
for (int i = 0; i < maxRetry; i++) {
try {
return new TNonblockingServerSocket(new InetSocketAddress(bindAddress, port), connectionTimeoutInMS);
} catch (TTransportException e) {
lastExp = e;
try {
Thread.sleep(retryIntervalMS);
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
break;
}
}
}
throw lastExp == null ? new TTransportException() : lastExp;
}
use of org.apache.thrift.transport.TNonblockingServerSocket in project flume by apache.
the class ThriftSource method getTThreadedSelectorServer.
private TServer getTThreadedSelectorServer() {
if (isSslEnabled() || enableKerberos) {
return null;
}
Class<?> serverClass;
Class<?> argsClass;
TServer.AbstractServerArgs args;
try {
serverClass = Class.forName("org.apache.thrift" + ".server.TThreadedSelectorServer");
argsClass = Class.forName("org.apache.thrift" + ".server.TThreadedSelectorServer$Args");
TServerTransport serverTransport = new TNonblockingServerSocket(new InetSocketAddress(bindAddress, port));
ExecutorService sourceService;
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Flume Thrift IPC Thread %d").build();
if (maxThreads == 0) {
sourceService = Executors.newCachedThreadPool(threadFactory);
} else {
sourceService = Executors.newFixedThreadPool(maxThreads, threadFactory);
}
args = (TNonblockingServer.AbstractNonblockingServerArgs) argsClass.getConstructor(TNonblockingServerTransport.class).newInstance(serverTransport);
Method m = argsClass.getDeclaredMethod("executorService", ExecutorService.class);
m.invoke(args, sourceService);
populateServerParams(args);
/*
* Both THsHaServer and TThreadedSelectorServer allows us to pass in
* the executor service to use - unfortunately the "executorService"
* method does not exist in the parent abstract Args class,
* so use reflection to pass the executor in.
*
*/
server = (TServer) serverClass.getConstructor(argsClass).newInstance(args);
} catch (ClassNotFoundException e) {
return null;
} catch (Throwable ex) {
throw new FlumeException("Cannot start Thrift Source.", ex);
}
return server;
}
Aggregations