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.vaushell.superpipes.tools.http.ImageExtractor.java

/**
 * Return the biggest image URI of this webpage.
 *
 * @param rootURI Webpage URI/*from  w  w w.j  a va  2 s  .  com*/
 * @return Biggest image
 * @throws IOException
 */
public BufferedImage extractBiggest(final URI rootURI) throws IOException {
    final List<URI> imagesURIs = new ArrayList<>();
    HttpEntity responseEntity = null;
    try {
        // Exec request
        final HttpGet get = new HttpGet(rootURI);

        try (final CloseableHttpResponse response = client.execute(get)) {
            final StatusLine sl = response.getStatusLine();
            if (sl.getStatusCode() != 200) {
                throw new IOException(sl.getReasonPhrase());
            }

            responseEntity = response.getEntity();

            try (final InputStream is = responseEntity.getContent()) {
                final Document doc = Jsoup.parse(is, "UTF-8", rootURI.toString());

                final Elements elts = doc.select("img");
                if (elts != null) {
                    for (final Element elt : elts) {
                        final String src = elt.attr("src");
                        if (src != null && !src.isEmpty()) {
                            try {
                                imagesURIs.add(rootURI.resolve(src));
                            } catch (final IllegalArgumentException ex) {
                                // Ignore wrong encoded URI
                            }
                        }
                    }
                }
            }
        }
    } finally {
        if (responseEntity != null) {
            EntityUtils.consume(responseEntity);
        }
    }

    final BufferedImage[] images = new BufferedImage[imagesURIs.size()];
    final ExecutorService service = Executors.newCachedThreadPool();
    for (int i = 0; i < imagesURIs.size(); ++i) {
        final int num = i;

        service.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    images[num] = HTTPhelper.loadPicture(client, imagesURIs.get(num));
                } catch (final IOException ex) {
                    images[num] = null;
                }
            }
        });
    }

    service.shutdown();

    try {
        service.awaitTermination(1L, TimeUnit.DAYS);
    } catch (final InterruptedException ex) {
        // Ignore
    }

    BufferedImage biggest = null;
    int biggestSize = Integer.MIN_VALUE;
    for (int i = 0; i < imagesURIs.size(); ++i) {
        if (images[i] != null) {
            final int actualSize = images[i].getWidth() * images[i].getHeight();
            if (actualSize > biggestSize) {
                biggest = images[i];

                biggestSize = actualSize;
            }
        }
    }

    return biggest;
}

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.application.config.OslpConfig.java

@Bean(destroyMethod = "releaseExternalResources")
public ClientBootstrap clientBootstrap() {
    InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    final ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());

    final ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
        @Override//from ww w. ja  va2  s . co m
        public ChannelPipeline getPipeline() throws ProtocolAdapterException {
            final ChannelPipeline pipeline = OslpConfig.this
                    .createChannelPipeline(OslpConfig.this.oslpChannelHandlerClient());

            LOGGER.info("Created client new pipeline");

            return pipeline;
        }
    };

    final ClientBootstrap bootstrap = new ClientBootstrap(factory);

    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", false);
    bootstrap.setOption("connectTimeoutMillis", this.connectionTimeout());

    bootstrap.setPipelineFactory(pipelineFactory);

    return bootstrap;
}

From source file:andromache.hadoop.CassandraInputFormat.java

public List<InputSplit> getSplits(JobContext context) throws IOException {
    Configuration conf = context.getConfiguration();
    validateConfiguration(conf);//from  w  ww.j  a v a 2 s.co m

    // cannonical ranges and nodes holding replicas
    List<TokenRange> masterRangeNodes = getRangeMap(conf);

    keyspace = CassandraConfigHelper.getInputKeyspace(context.getConfiguration());

    cfNames = CassandraConfigHelper.getInputColumnFamilies(context.getConfiguration());

    // TODO: [IS] make sure this partitioner matches to what is set on each keyspace participating
    partitioner = CassandraConfigHelper.getInputPartitioner(context.getConfiguration());
    logger.debug("partitioner is " + partitioner);

    // cannonical ranges, split into pieces, fetching the splits in parallel

    ExecutorService executor = Executors.newCachedThreadPool();
    List<InputSplit> splits = new ArrayList<InputSplit>();

    try {
        List<Future<List<CassandraSplit>>> splitfutures = new ArrayList<Future<List<CassandraSplit>>>();
        KeyRange jobKeyRange = CassandraConfigHelper.getInputKeyRange(conf);
        Range<Token> jobRange = null;
        if (jobKeyRange != null && jobKeyRange.start_token != null) {
            assert partitioner
                    .preservesOrder() : "ConfigHelper.setInputKeyRange(..) can only be used with a order preserving paritioner";
            assert jobKeyRange.start_key == null : "only start_token supported";
            assert jobKeyRange.end_key == null : "only end_token supported";
            jobRange = new Range<Token>(partitioner.getTokenFactory().fromString(jobKeyRange.start_token),
                    partitioner.getTokenFactory().fromString(jobKeyRange.end_token), partitioner);
        }

        for (TokenRange range : masterRangeNodes) {
            if (jobRange == null) {
                // for each range, pick a live owner and ask it to compute bite-sized splits

                splitfutures.add(executor.submit(new SplitCallable(range, conf)));
            } else {
                Range<Token> dhtRange = new Range<Token>(
                        partitioner.getTokenFactory().fromString(range.start_token),
                        partitioner.getTokenFactory().fromString(range.end_token), partitioner);

                if (dhtRange.intersects(jobRange)) {
                    for (Range<Token> intersection : dhtRange.intersectionWith(jobRange)) {
                        range.start_token = partitioner.getTokenFactory().toString(intersection.left);
                        range.end_token = partitioner.getTokenFactory().toString(intersection.right);
                        // for each range, pick a live owner and ask it to compute bite-sized splits
                        splitfutures.add(executor.submit(new SplitCallable(range, conf)));
                    }
                }
            }
        }

        // wait until we have all the results back
        for (Future<List<CassandraSplit>> futureInputSplits : splitfutures) {
            try {
                splits.addAll(futureInputSplits.get());
            } catch (Exception e) {
                throw new IOException("Could not get input splits", e);
            }
        }
    } finally {
        executor.shutdownNow();
    }

    assert splits.size() > 0;
    Collections.shuffle(splits, new Random(System.nanoTime()));
    return splits;
}

From source file:com.easarrive.quartz.aws.service.impl.PythonWordSegmenterService.java

/**
 * {@inheritDoc}/*w  w w .ja  v a2  s. com*/
 */
@Override
public void teachFromDatabase(final String wordSegmenterURL, String dbName, String tableName, String taskName) {

    //?
    List<CronTask> cronTaskList = cronTaskReadMapper.find(dbName, tableName, taskName);
    CronTask dbCronTask = null;

    //?????
    if (cronTaskList != null && cronTaskList.size() > 0) {
        dbCronTask = cronTaskList.get(0);
        if (cronTaskList.size() > 1) {
            for (int index = 1; index < cronTaskList.size(); index++) {
                CronTask deleteCronTask = cronTaskList.get(index);
                Long count = cronTaskWriteMapper.delete(deleteCronTask.getId());
                if (logger.isDebugEnabled()) {
                    logger.debug("Python ????  {}", count);
                }
            }
        }
    }

    //?
    Long tableUpdateTime = null;
    if (dbCronTask != null) {
        tableUpdateTime = dbCronTask.getTableUpdateTime();
        dbCronTask.setLastExecTime(dbCronTask.getExecTime());
        dbCronTask.setExecTime(System.currentTimeMillis());
        dbCronTask.setUpdateTime(System.currentTimeMillis());
    }
    if (tableUpdateTime == null) {
        tableUpdateTime = -1L;
    }

    //
    List<GoodsTags> goodsTagsList = goodsTagsReadMapper.getAll(tableUpdateTime);
    if (goodsTagsList == null || goodsTagsList.size() < 1) {
        if (logger.isInfoEnabled()) {
            logger.info("Python ?");
        }
        return;
    }

    //
    ExecutorService executorService = Executors.newCachedThreadPool();
    List<Future<Map<Integer, Boolean>>> futureList = new ArrayList<Future<Map<Integer, Boolean>>>();
    for (final GoodsTags tags : goodsTagsList) {
        if (tags == null) {
            continue;
        }
        Future<Map<Integer, Boolean>> future = executorService.submit(new Callable<Map<Integer, Boolean>>() {
            @Override
            public Map<Integer, Boolean> call() throws Exception {
                Map<Integer, Boolean> result = new HashMap<Integer, Boolean>();
                result.put(tags.getId(), null);
                if (StringUtil.isEmpty(tags.getTag())) {
                    return result;
                }
                try {
                    String wordSegmenterURLFinal = String.format("%s%s", wordSegmenterURL,
                            URLEncoder.encode(tags.getTag(), Constant.Charset.UTF8));
                    HttpResponse httpResponse = HttpClientUtil.get(wordSegmenterURLFinal, null);
                    int statusCode = httpResponse.getStatusLine().getStatusCode();
                    result.put(tags.getId(), statusCode == 200);
                } catch (Exception e) {
                    result.put(tags.getId(), false);
                }
                return result;
            }
        });
        futureList.add(future);
        if (tableUpdateTime < tags.getAddTime()) {
            tableUpdateTime = tags.getAddTime();
        }
    }

    //?
    for (Future<Map<Integer, Boolean>> future : futureList) {
        try {
            Map<Integer, Boolean> result = future.get();
            if (logger.isInfoEnabled()) {
                logger.info("Python ??  {}", result);
            }
        } catch (InterruptedException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getMessage(), e);
            }
        } catch (ExecutionException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    //
    if (dbCronTask == null) {
        dbCronTask = new CronTask(0L, dbName, tableName, tableUpdateTime, taskName, System.currentTimeMillis(),
                System.currentTimeMillis(), System.currentTimeMillis(), System.currentTimeMillis(), 0);
        Long id = cronTaskWriteMapper.insert(dbCronTask);
        if (logger.isDebugEnabled()) {
            logger.debug(" Python ?  {}", id);
        }
    } else {
        Long count = cronTaskWriteMapper.update(dbCronTask);
        if (logger.isDebugEnabled()) {
            logger.debug(" Python ??  {}", count);
        }
    }
}

From source file:org.sakuli.javaDSL.AbstractSakuliTest.java

/**
 * Initialize the Spring context of the Sakuli test suite and invokes all configured Initializing Services.
 *
 * @throws FileNotFoundException/*from w ww  . j  ava2 s.co m*/
 */
protected void initTestSuiteParameter() throws FileNotFoundException {
    LOGGER.info("............................INITIALIZE SAKULI-CONTEXT");
    executorService = Executors.newCachedThreadPool();
    BeanLoader.CONTEXT_PATH = "java-dsl-beanRefFactory.xml";
    SakuliJavaPropertyPlaceholderConfigurer.TEST_SUITE_FOLDER_VALUE = getTestSuiteFolder();
    SakuliJavaPropertyPlaceholderConfigurer.SAKULI_HOME_FOLDER_VALUE = getSakuliHomeFolder();
    SakuliJavaPropertyPlaceholderConfigurer.SAHI_HOME_VALUE = getSahiFolder();
    InitializingServiceHelper.invokeInitializingServcies();
}

From source file:com.mnxfst.testing.server.TSMain.java

public void execute(String[] args) {

    Options commandLineOptions = getTSClientCommandLineOptions();
    CommandLine commandLine = null;/*from w  w w .  j  a va2 s  . co m*/
    try {
        commandLine = parseCommandline(commandLineOptions, args);
    } catch (ParseException e) {
        System.out.println("Failed to parse command-line");
    }

    String hostname = null;
    try {
        hostname = extractStringValue(commandLine, CMD_OPT_HOSTNAME, CMD_OPT_HOSTNAME_SHORT);
    } catch (TSClientConfigurationException e) {
        printHelp(commandLineOptions, "Failed to parse host name from command-line");
        return;
    }

    int port = -1;
    try {
        port = extractIntValue(commandLine, CMD_OPT_PORT, CMD_OPT_PORT_SHORT);
    } catch (TSClientConfigurationException e) {
        printHelp(commandLineOptions, "Failed to parse port from command-line");
        return;
    }

    if (port < 1) {
        printHelp(commandLineOptions, "Failed to parse port from command-line");
        return;
    }

    int threadPoolSize = -1;
    try {
        threadPoolSize = extractIntValue(commandLine, CMD_OPT_THREAD_POOL_SIZE, CMD_OPT_THREAD_POOL_SIZE_SHORT);
    } catch (TSClientConfigurationException e) {
        threadPoolSize = -1;
    }

    System.out.println("ptest-server");
    System.out.println("hostname: " + hostname);
    System.out.println("port: " + port);
    System.out.println("server socket thread pool size: " + threadPoolSize);

    ChannelFactory channelFactory = null;
    if (threadPoolSize > 0)
        channelFactory = new NioServerSocketChannelFactory(Executors.newFixedThreadPool(threadPoolSize),
                Executors.newFixedThreadPool(threadPoolSize));
    else
        channelFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool());

    ServerBootstrap serverBootstrap = new ServerBootstrap(channelFactory);
    serverBootstrap.setPipelineFactory(new TSPipelineFactory(hostname, port));
    serverBootstrap.setOption("child.tcpNoDelay", true);
    serverBootstrap.setOption("child.keepAlive", true);

    serverBootstrap.bind(new InetSocketAddress(port));

}

From source file:net.formicary.remoterun.examples.Server.java

public void run() {
    remoteRunMaster = new RemoteRunMaster(Executors.newCachedThreadPool(), Executors.newCachedThreadPool(),
            null);//from   w  w w  .  j a v a 2s. c o m
    InetSocketAddress bindAddress = new InetSocketAddress(1081);
    remoteRunMaster.bind(bindAddress);

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, CharsetUtil.UTF_8));
        String line;
        while ((line = reader.readLine()) != null) {
            try {
                if (line.startsWith("l")) {
                    listClientConnections();
                } else if (line.startsWith("r")) {
                    runCommand(line);
                } else if (line.startsWith("i")) {
                    sendInput(line);
                } else if (line.startsWith("c")) {
                    closeInput(line);
                } else {
                    log.warn("Unhandled command: " + line);
                }
            } catch (Exception e) {
                log.error("Failed to process command: " + line, e);
            }
        }
    } catch (Exception e) {
        throw new RemoteRunException("Failed whilst processing user input, shutting down", e);
    } finally {
        remoteRunMaster.shutdown();
    }
}

From source file:com.flipkart.poseidon.Poseidon.java

@Autowired
public Poseidon(Configuration configuration, Application application) {
    this.configuration = configuration;
    this.application = application;

    dataSourceES = Executors.newCachedThreadPool();
    filterES = Executors.newCachedThreadPool();
}

From source file:com.alliander.osgp.adapter.protocol.oslp.application.config.OslpConfig.java

/**
 * @return//from  w w w . ja  va2  s.  c  o m
 */
@Bean(destroyMethod = "releaseExternalResources")
public ClientBootstrap clientBootstrap() {
    InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
    final ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());

    final ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws ProtocolAdapterException {
            final ChannelPipeline pipeline = OslpConfig.this
                    .createChannelPipeline(OslpConfig.this.oslpChannelHandlerClient());

            LOGGER.info("Created client new pipeline");

            return pipeline;
        }
    };

    final ClientBootstrap bootstrap = new ClientBootstrap(factory);

    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("keepAlive", false);
    bootstrap.setOption("connectTimeoutMillis", this.connectionTimeout());

    bootstrap.setPipelineFactory(pipelineFactory);

    return bootstrap;
}

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

/**
 * Initializes the micro pipeline manager
 * @param processingNodeId identifier of node this manager lives on
 * @param componentRepository reference to {@link ComponentRepository} which provides access to all {@link MicroPipelineComponent}
 * @param maxNumberOfThreads max. number of threads assigned to {@link ExecutorService} (1 = single threaded, n = fixed number of threads, other = cached thread pool)
 * @throws RequiredInputMissingException   
 *///from  www.  j  a  va2s.co  m
public MicroPipelineManager(final String processingNodeId, final ComponentRepository componentRepository,
        final int maxNumberOfThreads) throws RequiredInputMissingException {

    //////////////////////////////////////////////////////////////////////////////
    // validate provided input
    if (componentRepository == null)
        throw new RequiredInputMissingException("Missing required component repository");
    if (StringUtils.isBlank(processingNodeId))
        throw new RequiredInputMissingException("Missing required processing node identifier");
    //
    //////////////////////////////////////////////////////////////////////////////

    this.processingNodeId = StringUtils.lowerCase(StringUtils.trim(processingNodeId));
    this.microPipelineFactory = new MicroPipelineFactory(this.processingNodeId, componentRepository);

    if (maxNumberOfThreads == 1)
        this.executorService = Executors.newSingleThreadExecutor();
    else if (maxNumberOfThreads > 1)
        this.executorService = Executors.newFixedThreadPool(maxNumberOfThreads);
    else
        this.executorService = Executors.newCachedThreadPool();

}