use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.
the class Http2ClientMultipleMain method main.
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setApplication(application).setInterfaceId(ProtoService.class.getName()).setProtocol("h2c").setDirectUrl("h2c://127.0.0.1:12300").setSerialization("protobuf").setTimeout(3000);
final ProtoService protoService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
final int threads = 50;
final AtomicLong cnt = new AtomicLong(0);
final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
new NamedThreadFactory("client-"));
for (int i = 0; i < threads; i++) {
service1.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
EchoResponse s = protoService.echoObj(request);
cnt.incrementAndGet();
} catch (Throwable e) {
LOGGER.error("", e);
}
}
}
});
}
Thread thread = new Thread(new Runnable() {
private long last = 0;
@Override
public void run() {
while (true) {
long count = cnt.get();
long tps = count - last;
LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
last = count;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}, "Print-tps-THREAD");
thread.start();
}
use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.
the class RestClientMultipleMain method main.
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setApplication(application).setInterfaceId(RestService.class.getName()).setProtocol("rest").setBootstrap("rest").setDirectUrl("rest://127.0.0.1:8888").setTimeout(3000);
final RestService helloService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
final int threads = 50;
final AtomicLong cnt = new AtomicLong(0);
final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
new NamedThreadFactory("client-"));
for (int i = 0; i < threads; i++) {
service1.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
String s = helloService.get("1234567890");
cnt.incrementAndGet();
} catch (Exception e) {
LOGGER.error("", e);
}
}
}
});
}
Thread thread = new Thread(new Runnable() {
private long last = 0;
@Override
public void run() {
while (true) {
long count = cnt.get();
long tps = count - last;
LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
last = count;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}, "Print-tps-THREAD");
thread.start();
}
use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.
the class ProtobufServiceClientMultipleMain method main.
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setInterfaceId(// 指定接口
ProtoService.class.getName()).setProtocol(// 指定协议
"bolt").setApplication(application).setDirectUrl(// 指定直连地址
"bolt://127.0.0.1:12200").setSerialization(// 指定序列化协议,默认为hessian
"protobuf").setConnectTimeout(10 * 1000);
final ProtoService helloService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
final int threads = 50;
final AtomicLong cnt = new AtomicLong(0);
final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
new NamedThreadFactory("client-"));
for (int i = 0; i < threads; i++) {
service1.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
EchoResponse s = helloService.echoObj(request);
cnt.incrementAndGet();
} catch (Throwable e) {
LOGGER.error("", e);
}
}
}
});
}
Thread thread = new Thread(new Runnable() {
private long last = 0;
@Override
public void run() {
while (true) {
long count = cnt.get();
long tps = count - last;
LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
last = count;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}, "Print-tps-THREAD");
thread.start();
}
use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.
the class AbstractHttpServer method initThreadPool.
protected ThreadPoolExecutor initThreadPool(ServerConfig serverConfig) {
ThreadPoolExecutor threadPool = BusinessPool.initPool(serverConfig);
threadPool.setThreadFactory(new NamedThreadFactory("SEV-" + serverConfig.getProtocol().toUpperCase() + "-BIZ-" + serverConfig.getPort(), serverConfig.isDaemon()));
threadPool.setRejectedExecutionHandler(new SofaRejectedExecutionHandler());
if (serverConfig.isPreStartCore()) {
// 初始化核心线程池
threadPool.prestartAllCoreThreads();
}
return threadPool;
}
use of com.alipay.sofa.rpc.common.struct.NamedThreadFactory in project sofa-rpc by sofastack.
the class AllConnectConnectionHolder method addNode.
protected void addNode(List<ProviderInfo> providerInfoList) {
// first update last all providers
lastAddresses.addAll(providerInfoList);
final String interfaceId = consumerConfig.getInterfaceId();
int providerSize = providerInfoList.size();
String appName = consumerConfig.getAppName();
if (LOGGER.isInfoEnabled(appName)) {
LOGGER.infoWithApp(appName, "Add provider of {}, size is : {}", interfaceId, providerSize);
}
if (providerSize > 0) {
// 多线程建立连接
// 最大10个
int threads = Math.min(10, providerSize);
final CountDownLatch latch = new CountDownLatch(providerSize);
ThreadPoolExecutor initPool = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(providerInfoList.size()), new NamedThreadFactory("CLI-CONN-" + interfaceId, true));
int connectTimeout = consumerConfig.getConnectTimeout();
for (final ProviderInfo providerInfo : providerInfoList) {
initClientRunnable(initPool, latch, providerInfo);
}
try {
int totalTimeout = ((providerSize % threads == 0) ? (providerSize / threads) : ((providerSize / threads) + 1)) * connectTimeout + 500;
// 一直等到子线程都结束
latch.await(totalTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.ERROR_UPDATE_PROVIDERS, consumerConfig.getInterfaceId(), ""), e);
} finally {
// 关闭线程池
initPool.shutdown();
}
}
}
Aggregations