Example usage for java.util.concurrent Executors newCachedThreadPool

List of usage examples for java.util.concurrent Executors newCachedThreadPool

Introduction

In this page you can find the example usage for java.util.concurrent Executors newCachedThreadPool.

Prototype

public static ExecutorService newCachedThreadPool() 

Source Link

Document

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.

Usage

From source file:com.qwazr.server.GenericServer.java

GenericServer(final GenericServerBuilder builder) throws IOException {

    this.configuration = builder.configuration;
    this.executorService = builder.executorService == null ? Executors.newCachedThreadPool()
            : builder.executorService;//ww  w  . j a  va  2  s  .  c o  m
    this.servletContainer = Servlets.newContainer();
    this.webAppContext = builder.webAppContext;
    this.webServiceContext = builder.webServiceContext;
    this.webAppEndPoints = webAppContext == null ? null : Collections.unmodifiableSet(webAppContext.endPoints);
    this.webServiceEndPoints = webServiceContext == null ? null
            : Collections.unmodifiableSet(webServiceContext.endPoints);
    builder.contextAttribute(this);
    this.contextAttributes = new LinkedHashMap<>(builder.contextAttributes);
    this.undertows = new ArrayList<>();
    this.deploymentManagers = new ArrayList<>();
    this.identityManagerProvider = builder.identityManagerProvider;
    this.hostnamePrincipalResolver = builder.hostnamePrincipalResolver;
    this.webAppAccessLogger = builder.webAppAccessLogger;
    this.webServiceAccessLogger = builder.webServiceAccessLogger;
    this.udpServer = buildUdpServer(builder, configuration);
    this.startedListeners = CollectionsUtils.copyIfNotEmpty(builder.startedListeners, ArrayList::new);
    this.shutdownListeners = CollectionsUtils.copyIfNotEmpty(builder.shutdownListeners, ArrayList::new);
    this.connectorsStatistics = new ArrayList<>();
    this.registeredObjectNames = new LinkedHashSet<>();
}

From source file:com.cloudera.livy.client.local.driver.RemoteDriver.java

private RemoteDriver(String[] args) throws Exception {
    this.activeJobs = Maps.newConcurrentMap();
    this.jcLock = new Object();
    this.shutdownLock = new Object();
    localTmpDir = Files.createTempDir();

    SparkConf conf = new SparkConf();
    String serverAddress = null;/*from  w w w  .  ja va 2  s.co m*/
    int serverPort = -1;
    for (int idx = 0; idx < args.length; idx += 2) {
        String key = args[idx];
        if (key.equals("--remote-host")) {
            serverAddress = getArg(args, idx);
        } else if (key.equals("--remote-port")) {
            serverPort = Integer.parseInt(getArg(args, idx));
        } else if (key.equals("--client-id")) {
            conf.set(LocalConf.SPARK_CONF_PREFIX + CLIENT_ID.key, getArg(args, idx));
        } else if (key.equals("--secret")) {
            conf.set(LocalConf.SPARK_CONF_PREFIX + CLIENT_SECRET.key, getArg(args, idx));
        } else if (key.equals("--conf")) {
            String[] val = getArg(args, idx).split("[=]", 2);
            conf.set(val[0], val[1]);
        } else {
            throw new IllegalArgumentException("Invalid command line: " + Joiner.on(" ").join(args));
        }
    }

    executor = Executors.newCachedThreadPool();

    LOG.info("Connecting to: {}:{}", serverAddress, serverPort);

    LocalConf livyConf = new LocalConf(null);
    for (Tuple2<String, String> e : conf.getAll()) {
        if (e._1().startsWith(LocalConf.SPARK_CONF_PREFIX)) {
            String key = e._1().substring(LocalConf.SPARK_CONF_PREFIX.length());
            livyConf.set(key, e._2());
            LOG.debug("Remote Driver config: {} = {}", key, e._2());
        }
    }

    String clientId = livyConf.get(CLIENT_ID);
    Preconditions.checkArgument(clientId != null, "No client ID provided.");
    String secret = livyConf.get(CLIENT_SECRET);
    Preconditions.checkArgument(secret != null, "No secret provided.");

    System.out.println("MAPCONF-->");
    System.out.println(livyConf);
    this.egroup = new NioEventLoopGroup(livyConf.getInt(RPC_MAX_THREADS),
            new ThreadFactoryBuilder().setNameFormat("Driver-RPC-Handler-%d").setDaemon(true).build());
    this.serializer = new Serializer();
    this.protocol = new DriverProtocol(this, jcLock);

    // The RPC library takes care of timing out this.
    this.clientRpc = Rpc.createClient(livyConf, egroup, serverAddress, serverPort, clientId, secret, protocol)
            .get();
    this.running = true;

    this.clientRpc.addListener(new Rpc.Listener() {
        @Override
        public void rpcClosed(Rpc rpc) {
            LOG.warn("Shutting down driver because RPC channel was closed.");
            shutdown(null);
        }
    });

    try {
        long t1 = System.currentTimeMillis();
        LOG.info("Starting Spark context at {}", t1);
        JavaSparkContext sc = new JavaSparkContext(conf);
        LOG.info("Spark context finished initialization in {}ms", System.currentTimeMillis() - t1);
        sc.sc().addSparkListener(new DriverSparkListener(this));
        synchronized (jcLock) {
            jc = new JobContextImpl(sc, localTmpDir);
            jcLock.notifyAll();
        }
    } catch (Exception e) {
        LOG.error("Failed to start SparkContext: " + e, e);
        shutdown(e);
        synchronized (jcLock) {
            jcLock.notifyAll();
        }
        throw e;
    }

    synchronized (jcLock) {
        for (JobWrapper<?> job : jobQueue) {
            job.submit(executor);
        }
        jobQueue.clear();
    }
}

From source file:com.amazon.alexa.avs.NotificationManager.java

NotificationManager(NotificationIndicator indicator, SimpleAudioPlayer audioPlayer, BasicHttpClient httpClient,
        FileDataStore<NotificationsStatePayload> dataStore) {
    this.notificationIndicator = indicator;
    this.player = audioPlayer;
    this.assets = Collections.synchronizedMap(new HashMap<String, File>());
    this.indicatorExecutor = Executors.newSingleThreadExecutor();
    this.assetDownloader = Executors.newCachedThreadPool();
    this.allowPersistentIndicator = new AtomicBoolean(true);
    this.httpClient = httpClient;
    this.isSetIndicatorPersisted = new AtomicBoolean(false);
    this.indicatorStatus = Status.NONE;
    this.dataStore = dataStore;
    this.indicatorFutures = Collections.synchronizedSet(new HashSet<Future<?>>());
    this.activeAudioAsset = new AtomicReference<String>("");
    this.resLoader = Thread.currentThread().getContextClassLoader();
}

From source file:com.ottogroup.bi.asap.pipeline.MicroPipelineManager.java

/**
 * Initializes the manager using the provided input
 * @param microPipelineFactory//from  www . j  av  a2  s.  c  o m
 * @param pipelineThreadPool
 * 
 */
public MicroPipelineManager(final MicroPipelineFactory microPipelineFactory,
        final ExecutorService pipelineThreadPool) {
    this.factory = microPipelineFactory;
    if (pipelineThreadPool != null)
        this.pipelineThreadPool = pipelineThreadPool;
    else
        this.pipelineThreadPool = Executors.newCachedThreadPool();
}

From source file:backup.datanode.DataNodeRestoreProcessor.java

public DataNodeRestoreProcessor(Configuration conf, DataNode datanode) throws Exception {
    _closer = Closer.create();/*w  w w  .  j a  v a2s . co  m*/
    _datanode = datanode;
    _restoreThroughput = Metrics.METRICS.meter(RESTORE_THROUGHPUT);
    _bytesPerChecksum = conf.getInt(DFSConfigKeys.DFS_BYTES_PER_CHECKSUM_KEY,
            DFSConfigKeys.DFS_BYTES_PER_CHECKSUM_DEFAULT);
    _checksumType = Type
            .valueOf(conf.get(DFSConfigKeys.DFS_CHECKSUM_TYPE_KEY, DFSConfigKeys.DFS_CHECKSUM_TYPE_DEFAULT));
    int threads = conf.getInt(DFS_BACKUP_DATANODE_RESTORE_BLOCK_HANDLER_COUNT_KEY,
            DFS_BACKUP_DATANODE_RESTORE_BLOCK_HANDLER_COUNT_DEFAULT);
    long pauseOnError = conf.getLong(DFS_BACKUP_DATANODE_RESTORE_ERROR_PAUSE_KEY,
            DFS_BACKUP_DATANODE_RESTORE_ERROR_PAUSE_DEFAULT);
    _backupStore = _closer.register(BackupStore.create(BackupUtil.convert(conf)));
    _restoreBlocks = new ArrayBlockingQueue<>(threads);
    _executorService = Executors.newCachedThreadPool();
    _closer.register((Closeable) () -> _executorService.shutdownNow());
    for (int t = 0; t < threads; t++) {
        _executorService.submit(Executable.createDaemon(LOG, pauseOnError, _running, () -> restoreBlocks()));
    }
}

From source file:com.cloudera.livy.rsc.driver.RSCDriver.java

public RSCDriver(SparkConf conf, RSCConf livyConf) throws Exception {
    Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwx------");
    this.localTmpDir = Files.createTempDirectory("rsc-tmp", PosixFilePermissions.asFileAttribute(perms))
            .toFile();//from  w w w . j a  va2  s.c om
    this.executor = Executors.newCachedThreadPool();
    this.jobQueue = new LinkedList<>();
    this.clients = new ConcurrentLinkedDeque<>();
    this.serializer = new Serializer();

    this.conf = conf;
    this.livyConf = livyConf;
    this.jcLock = new Object();
    this.shutdownLock = new Object();

    this.activeJobs = new ConcurrentHashMap<>();
    this.bypassJobs = new ConcurrentLinkedDeque<>();
    this.idleTimeout = new AtomicReference<>();
}

From source file:com.dtstack.jlogstash.inputs.Netty.java

@Override
public void emit() {
    // TODO Auto-generated method stub
    // Server??/*from  www. j a va 2s.  c  om*/
    try {
        bossExecutor = Executors.newCachedThreadPool();
        workerExecutor = Executors.newCachedThreadPool();
        bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(bossExecutor, workerExecutor));
        final NettyServerHandler nettyServerHandler = new NettyServerHandler(this);
        // ?????(Handler)
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();
                if (isExtract) {
                    pipeline.addLast("zlibDecoder", new ZlibDecoder(ZlibWrapper.GZIP));
                }
                pipeline.addLast("decoder", new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, false, true,
                        ChannelBuffers.copiedBuffer(delimiter, Charset.forName(encoding))));
                pipeline.addLast("handler", nettyServerHandler);
                return pipeline;
            }
        });
        bootstrap.setOption("child.receiveBufferSize", receiveBufferSize);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.bind(new InetSocketAddress(InetAddress.getByName(host), port));
    } catch (Exception e) {
        logger.error(e.getMessage());
        System.exit(1);
    }
}

From source file:com.netflix.curator.framework.recipes.queue.TestDistributedQueue.java

@Test
public void testCustomExecutor() throws Exception {
    final int ITERATIONS = 1000;

    Timing timing = new Timing();
    DistributedQueue<String> queue = null;
    final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(),
            timing.session(), timing.connection(), new RetryOneTime(1));
    client.start();/*w  w w.  j a  v a2  s .  co  m*/
    try {
        final CountDownLatch latch = new CountDownLatch(ITERATIONS);
        QueueConsumer<String> consumer = new QueueConsumer<String>() {
            @Override
            public void consumeMessage(String message) throws Exception {
                latch.countDown();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        QueueSerializer<String> serializer = new QueueSerializer<String>() {
            @Override
            public byte[] serialize(String item) {
                return item.getBytes();
            }

            @Override
            public String deserialize(byte[] bytes) {
                return new String(bytes);
            }
        };

        Executor executor = Executors.newCachedThreadPool();

        final Set<String> used = Sets.newHashSet();
        final Set<String> doubleUsed = Sets.newHashSet();
        queue = new DistributedQueue<String>(client, consumer, serializer, QUEUE_PATH,
                QueueBuilder.defaultThreadFactory, executor, Integer.MAX_VALUE, false, "/lock",
                QueueBuilder.NOT_SET, true, 5000) {
            @SuppressWarnings("SimplifiableConditionalExpression")
            @Override
            protected boolean processWithLockSafety(String itemNode, DistributedQueue.ProcessType type)
                    throws Exception {
                if (used.contains(itemNode)) {
                    doubleUsed.add(itemNode);
                } else {
                    used.add(itemNode);
                }
                return (client.getState() == CuratorFrameworkState.STARTED)
                        ? super.processWithLockSafety(itemNode, type)
                        : false;
            }
        };
        queue.start();

        for (int i = 0; i < ITERATIONS; ++i) {
            queue.put(Integer.toString(i));
        }

        Assert.assertTrue(timing.awaitLatch(latch));

        Assert.assertTrue(doubleUsed.size() == 0, doubleUsed.toString());
    } finally {
        IOUtils.closeQuietly(queue);
        IOUtils.closeQuietly(client);
    }
}

From source file:net.pms.network.HTTPServer.java

public boolean start() throws IOException {
    hostname = configuration.getServerHostname();
    InetSocketAddress address;//from w  ww  .ja  v a 2 s .  c  o  m

    if (StringUtils.isNotBlank(hostname)) {
        logger.info("Using forced address " + hostname);
        InetAddress tempIA = InetAddress.getByName(hostname);

        if (tempIA != null && networkInterface != null
                && networkInterface.equals(NetworkInterface.getByInetAddress(tempIA))) {
            address = new InetSocketAddress(tempIA, port);
        } else {
            address = new InetSocketAddress(hostname, port);
        }
    } else if (isAddressFromInterfaceFound(configuration.getNetworkInterface())) { // XXX sets iafinal and networkInterface
        logger.info("Using address {} found on network interface: {}", iafinal,
                networkInterface.toString().trim().replace('\n', ' '));
        address = new InetSocketAddress(iafinal, port);
    } else {
        logger.info("Using localhost address");
        address = new InetSocketAddress(port);
    }

    logger.info("Created socket: " + address);

    if (configuration.isHTTPEngineV2()) { // HTTP Engine V2
        group = new DefaultChannelGroup("myServer");
        factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool());

        ServerBootstrap bootstrap = new ServerBootstrap(factory);
        HttpServerPipelineFactory pipeline = new HttpServerPipelineFactory(group);
        bootstrap.setPipelineFactory(pipeline);
        bootstrap.setOption("child.tcpNoDelay", true);
        bootstrap.setOption("child.keepAlive", true);
        bootstrap.setOption("reuseAddress", true);
        bootstrap.setOption("child.reuseAddress", true);
        bootstrap.setOption("child.sendBufferSize", 65536);
        bootstrap.setOption("child.receiveBufferSize", 65536);
        channel = bootstrap.bind(address);
        group.add(channel);

        if (hostname == null && iafinal != null) {
            hostname = iafinal.getHostAddress();
        } else if (hostname == null) {
            hostname = InetAddress.getLocalHost().getHostAddress();
        }
    } else { // HTTP Engine V1
        serverSocketChannel = ServerSocketChannel.open();

        serverSocket = serverSocketChannel.socket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(address);

        if (hostname == null && iafinal != null) {
            hostname = iafinal.getHostAddress();
        } else if (hostname == null) {
            hostname = InetAddress.getLocalHost().getHostAddress();
        }

        runnable = new Thread(this, "HTTP Server");
        runnable.setDaemon(false);
        runnable.start();
    }

    return true;
}

From source file:org.elasticsoftware.sip.SipChannelFactoryImpl.java

/**
 * Creates a new channel to given host and port.<br>
 *
 * @param host//from   www .  j ava 2 s  .co m
 * @param port
 * @return
 * @throws Exception
 */
private Channel createChannel(String host, int port) throws Exception {
    // Important notice; use NioClientSocketChannelFactory instead
    // of NioServerSocketChannelFactory
    ChannelFactory channelFactory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());
    ClientBootstrap bootstrap = new ClientBootstrap(channelFactory);
    //bootstrap.setPipelineFactory(new SipClientPipelineFactory(false,false));
    bootstrap.setPipelineFactory(new SipPipelineFactory(sipServerHandler));
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));

    // open / connect to channel
    Channel c = future.await().getChannel();
    if (!future.isSuccess()) {
        log.warn(String.format("createChannel. Establishing connection failed[%s]",
                future.getCause().getMessage()));
        bootstrap.releaseExternalResources();
    }
    return c;
}