use of com.google.common.util.concurrent.ThreadFactoryBuilder in project pulsar by yahoo.
the class MockBrokerService method startMockBrokerService.
public void startMockBrokerService() throws Exception {
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build();
final int numThreads = 2;
final int MaxMessageSize = 5 * 1024 * 1024;
EventLoopGroup eventLoopGroup;
try {
if (SystemUtils.IS_OS_LINUX) {
try {
eventLoopGroup = new EpollEventLoopGroup(numThreads, threadFactory);
} catch (UnsatisfiedLinkError e) {
eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
}
} else {
eventLoopGroup = new NioEventLoopGroup(numThreads, threadFactory);
}
workerGroup = eventLoopGroup;
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(workerGroup, workerGroup);
if (workerGroup instanceof EpollEventLoopGroup) {
bootstrap.channel(EpollServerSocketChannel.class);
} else {
bootstrap.channel(NioServerSocketChannel.class);
}
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
ch.pipeline().addLast("handler", new MockServerCnx());
}
});
// Bind and start to accept incoming connections.
bootstrap.bind(brokerServicePort).sync();
} catch (Exception e) {
throw e;
}
}
use of com.google.common.util.concurrent.ThreadFactoryBuilder in project distributedlog by twitter.
the class DistributedLogServer method runServer.
public void runServer() throws ConfigurationException, IllegalArgumentException, IOException {
if (!uri.isPresent()) {
throw new IllegalArgumentException("No distributedlog uri provided.");
}
URI dlUri = URI.create(uri.get());
DistributedLogConfiguration dlConf = new DistributedLogConfiguration();
if (conf.isPresent()) {
String configFile = conf.get();
try {
dlConf.loadConf(new File(configFile).toURI().toURL());
} catch (ConfigurationException e) {
throw new IllegalArgumentException("Failed to load distributedlog configuration from " + configFile + ".");
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Failed to load distributedlog configuration from malformed " + configFile + ".");
}
}
this.configExecutorService = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder().setNameFormat("DistributedLogService-Dyncfg-%d").setDaemon(true).build());
// server configuration and dynamic configuration
ServerConfiguration serverConf = new ServerConfiguration();
serverConf.loadConf(dlConf);
// overwrite the shard id if it is provided in the args
if (shardId.isPresent()) {
serverConf.setServerShardId(shardId.get());
}
serverConf.validate();
DynamicDistributedLogConfiguration dynDlConf = getServiceDynConf(dlConf);
logger.info("Starting stats provider : {}", statsProvider.getClass());
statsProvider.start(dlConf);
if (announceServerSet.isPresent() && announceServerSet.get()) {
announcer = new ServerSetAnnouncer(dlUri, port.or(0), statsPort.or(0), shardId.or(0));
} else {
announcer = new NOPAnnouncer();
}
// Build the stream partition converter
StreamPartitionConverter converter;
try {
converter = ReflectionUtils.newInstance(serverConf.getStreamPartitionConverterClass());
} catch (ConfigurationException e) {
logger.warn("Failed to load configured stream-to-partition converter. Fallback to use {}", IdentityStreamPartitionConverter.class.getName());
converter = new IdentityStreamPartitionConverter();
}
StreamConfigProvider streamConfProvider = getStreamConfigProvider(dlConf, converter);
// pre-run
preRun(dlConf, serverConf);
Pair<DistributedLogServiceImpl, Server> serverPair = runServer(serverConf, dlConf, dynDlConf, dlUri, converter, statsProvider, port.or(0), keepAliveLatch, statsReceiver, thriftmux.isPresent(), streamConfProvider);
this.dlService = serverPair.getLeft();
this.server = serverPair.getRight();
// announce the service
announcer.announce();
}
use of com.google.common.util.concurrent.ThreadFactoryBuilder in project helios by spotify.
the class FastForwardReporterTest method setUp.
@Before
public void setUp() throws Exception {
final ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).build();
this.executor = Executors.newSingleThreadScheduledExecutor(threadFactory);
this.reporter = new FastForwardReporter(ffwd, metricRegistry, executor, "helios.test", // these interval values do not matter for this test:
30, TimeUnit.SECONDS, Collections::emptyMap);
}
use of com.google.common.util.concurrent.ThreadFactoryBuilder in project helios by spotify.
the class FastForwardReporter method create.
/**
* Overload of {@link #create(MetricRegistry, Optional, String, int)} which allows for setting
* additional attributes in each reported metric.
*
* <p>The additional attributes are modeled as a Supplier to allow for attributes that might
* change values at runtime.
*/
public static FastForwardReporter create(MetricRegistry registry, Optional<HostAndPort> address, String metricKey, int intervalSeconds, Supplier<Map<String, String>> additionalAttributes) throws SocketException, UnknownHostException {
final FastForward ff;
if (address.isPresent()) {
ff = FastForward.setup(address.get().getHostText(), address.get().getPort());
} else {
ff = FastForward.setup();
}
final ThreadFactory threadFactory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("fast-forward-reporter-%d").build();
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(threadFactory);
return new FastForwardReporter(ff, registry, executorService, metricKey, intervalSeconds, TimeUnit.SECONDS, additionalAttributes);
}
use of com.google.common.util.concurrent.ThreadFactoryBuilder in project reflections by ronmamo.
the class ConfigurationBuilder method useParallelExecutor.
/** sets the executor service used for scanning to ThreadPoolExecutor with core size as the given availableProcessors parameter.
* the executor service spawns daemon threads by default.
* <p>default is ThreadPoolExecutor with a single core */
public ConfigurationBuilder useParallelExecutor(final int availableProcessors) {
ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("org.reflections-scanner-%d").build();
setExecutorService(Executors.newFixedThreadPool(availableProcessors, factory));
return this;
}
Aggregations