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:be.vlaanderen.sesam.proxy.internal.BundleActivator.java

private void startServer() {
    Executor executor = Executors.newCachedThreadPool();
    serverChannelFactory = new NioServerSocketChannelFactory(executor, executor);
    executionHandler = new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(32, 1048576, 33554432)); // TODO
    // configurable
    serverBootstrap = new ServerBootstrap(serverChannelFactory);
    pipelineFactory = new HttpServerPipelineFactory(executionHandler, central);

    serverBootstrap.setPipelineFactory(pipelineFactory);

    onConfigurationChanged(configService.getRules());
}

From source file:com.linkedin.pinot.integration.tests.OfflineClusterIntegrationTest.java

@BeforeClass
public void setUp() throws Exception {
    //Clean up//from ww w  .j a va  2  s.co m
    ensureDirectoryExistsAndIsEmpty(_tmpDir);
    ensureDirectoryExistsAndIsEmpty(_segmentDir);
    ensureDirectoryExistsAndIsEmpty(_tarDir);

    // Start the cluster
    startCluster();

    // Unpack the Avro files
    final List<File> avroFiles = unpackAvroData(_tmpDir, SEGMENT_COUNT);

    createTable();

    // Load data into H2
    ExecutorService executor = Executors.newCachedThreadPool();
    setupH2AndInsertAvro(avroFiles, executor);

    // Create segments from Avro data
    buildSegmentsFromAvro(avroFiles, executor, 0, _segmentDir, _tarDir, "mytable", false, null);

    // Initialize query generator
    setupQueryGenerator(avroFiles, executor);

    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.MINUTES);

    // Set up a Helix spectator to count the number of segments that are uploaded and unlock the latch once 12 segments are online
    final CountDownLatch latch = setupSegmentCountCountDownLatch("mytable", SEGMENT_COUNT);

    // Upload the segments
    int i = 0;
    for (String segmentName : _tarDir.list()) {
        System.out.println("Uploading segment " + (i++) + " : " + segmentName);
        File file = new File(_tarDir, segmentName);
        FileUploadUtils.sendSegmentFile("localhost", "8998", segmentName, new FileInputStream(file),
                file.length());
    }

    // Wait for all segments to be online
    latch.await();
    TOTAL_DOCS = 115545;
    long timeInTwoMinutes = System.currentTimeMillis() + 2 * 60 * 1000L;
    long numDocs;
    while ((numDocs = getCurrentServingNumDocs()) < TOTAL_DOCS) {
        System.out.println("Current number of documents: " + numDocs);
        if (System.currentTimeMillis() < timeInTwoMinutes) {
            Thread.sleep(1000);
        } else {
            Assert.fail("Segments were not completely loaded within two minutes");
        }
    }
}

From source file:org.ow2.chameleon.fuchsia.importer.jsonrpc.it.JSONRPCImporterTest.java

@Before
public void setUpFinal() {
    // instantiate the importer
    Dictionary<String, String> conf = new Hashtable<String, String>();
    conf.put(INSTANCE_NAME_PROPERTY, IMPORTER_NAME);
    conf.put(TARGET_FILTER_PROPERTY, "(" + CONFIGS + "=jsonrpc)");
    ComponentInstance importer = ipojoHelper
            .createComponentInstance("org.ow2.chameleon.fuchsia.importer.jsonrpc.JSONRPCImporter", conf, 20000);
    if (importer == null) {
        fail("Fail to create the JSONRPC Importer.");
    }/*from  w  ww .java 2  s  . co m*/

    // create HttpServer
    try {
        httpServer = HttpServer.create(new InetSocketAddress(HTTP_PORT), 0);
    } catch (IOException e) {
        fail("Creation of httpServer fail", e);
    }
    httpServer.setExecutor(Executors.newCachedThreadPool());
    httpServer.start();
}

From source file:org.osgp.adapter.protocol.dlms.application.config.DlmsConfig.java

/**
 * Returns a ServerBootstrap setting up a server pipeline listening for
 * incoming DLMS alarm notifications./*from   w  w  w .j a va  2 s. c o  m*/
 *
 * @return a DLMS alarm server bootstrap.
 */
@Bean(destroyMethod = "releaseExternalResources")
public ServerBootstrap serverBootstrap() {
    final ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool());

    final ServerBootstrap bootstrap = new ServerBootstrap(factory);

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws ProtocolAdapterException {
            final ChannelPipeline pipeline = DlmsConfig.this
                    .createChannelPipeline(DlmsConfig.this.dlmsChannelHandlerServer());

            LOGGER.info("Created new DLMS handler pipeline for server");

            return pipeline;
        }
    });

    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", false);

    bootstrap.bind(new InetSocketAddress(this.dlmsPortServer()));

    return bootstrap;
}

From source file:jp.realglobe.util.uploader.DirectoryUploaderWithServerTest.java

/**
 * ??/*  www  .  j  av  a  2 s .c  o  m*/
 * @throws IOException 
 * @throws InterruptedException ????
 */
@Before
public void setUp() throws IOException, InterruptedException {
    this.requestQueue = new LinkedBlockingQueue<>();
    this.executor = Executors.newCachedThreadPool();
    this.uploaderId = String.valueOf(Math.abs(System.nanoTime()));

    this.targetDirectory = Files.createTempDirectory("");
    try {
        this.server = HttpServer.create(new InetSocketAddress(0), 0);
        this.server.createContext("/", this::handle);
        this.server.start();
    } catch (final IOException e) {
        FileUtils.deleteDirectory(this.targetDirectory.toFile());
        throw e;
    }
    // ??
    Thread.sleep(1_000);
}

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

/**
 * Initializes the manager using the provided input
 * @param componentRepository/*from   w w  w  .j  av a  2 s.  c  om*/
 * @param pipelineThreadPool
 */
public MicroPipelineManager(final ComponentRepository componentRepository,
        final ExecutorService pipelineThreadPool) {
    this.factory = new MicroPipelineFactory(componentRepository);
    if (pipelineThreadPool != null)
        this.pipelineThreadPool = pipelineThreadPool;
    else
        this.pipelineThreadPool = Executors.newCachedThreadPool();
}

From source file:jenkins.plugins.elanceodesk.workplace.notifier.HttpWorkerTest.java

@Test
public void testMutipleTriesWorker() throws InterruptedException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    HttpWorker worker = new HttpWorker("http://localhost:8000/retry-test", "test1body", 30000, retries,
            Mockito.mock(PrintStream.class));
    executorService.submit(worker);/*from w  w  w . j  a v a2  s  .  c  o m*/
    executorService.shutdown();
    executorService.awaitTermination(5, TimeUnit.SECONDS);
    Assert.assertTrue(MyHandler.getRetryTestResult());
}

From source file:com.informatica.surf.Node.java

@Override
public void run() {
    _logger.info("Node run starting...");
    Executor executor = Executors.newCachedThreadPool();
    Disruptor<SurfEvent> disruptor = new Disruptor<>(SurfEvent.EVENT_FACTORY, 128, executor);
    disruptor.handleEventsWith(this);
    RingBuffer<SurfEvent> buffer = disruptor.start();

    VDSEventListImpl events = null;/*w w  w.j a  v a 2s .  c  om*/
    while (!_shutdown) {
        try {
            events = _eventListPool.borrowObject(0);
            while (events.getEventsList().isEmpty()) {
                _source.read(events);
            }
            if (_needsAck) {
                Object obj = _acksource.getInputObject();
                // Hacking this in for now
                // Need to actually ack the object after Kinesis consumes it: TODO
                _acksource.updateInputObject(obj);
            }
            long seq = buffer.next();
            SurfEvent event = buffer.get(seq);
            event.setEventlist(events);
            buffer.publish(seq);
        } catch (Exception ex) {
            _logger.error("Exception while reading data:", ex);
            ex.printStackTrace();
        }
    }
}

From source file:org.openrdf.http.client.SesameClientImpl.java

/**
 * (re)initializes the connection manager and HttpClient (if not already
 * done), for example after a shutdown has been invoked earlier. Invoking
 * this method multiple times will have no effect.
 *//*ww w.  j  a  v a  2 s . c  o m*/
public synchronized void initialize() {
    if (executor == null) {
        executor = Executors.newCachedThreadPool();
    }
}

From source file:com.yahoo.omid.tso.TSOServer.java

@Override
public void run() {
    // *** Start the Netty configuration ***
    // Start server with Nb of active threads = 2*NB CPU + 1 as maximum.
    ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2);

    ServerBootstrap bootstrap = new ServerBootstrap(factory);
    // Create the global ChannelGroup
    ChannelGroup channelGroup = new DefaultChannelGroup(TSOServer.class.getName());
    // threads max
    // int maxThreads = Runtime.getRuntime().availableProcessors() *2 + 1;
    int maxThreads = 5;
    // Memory limitation: 1MB by channel, 1GB global, 100 ms of timeout
    ThreadPoolExecutor pipelineExecutor = new OrderedMemoryAwareThreadPoolExecutor(maxThreads, 1048576,
            1073741824, 100, TimeUnit.MILLISECONDS, new ObjectSizeEstimator() {
                @Override//from  w ww .  ja va 2 s . com
                public int estimateSize(Object o) {
                    return 1000;
                }
            }, Executors.defaultThreadFactory());

    // This is the only object of timestamp oracle
    // TODO: make it singleton
    //TimestampOracle timestampOracle = new TimestampOracle();
    // The wrapper for the shared state of TSO
    state = BookKeeperStateBuilder.getState(this.config);

    if (state == null) {
        LOG.error("Couldn't build state");
        return;
    }

    state.addRecord(new byte[] { LoggerProtocol.LOGSTART }, new AddRecordCallback() {
        @Override
        public void addRecordComplete(int rc, Object ctx) {
        }
    }, null);

    TSOState.BATCH_SIZE = config.getBatchSize();
    System.out.println("PARAM MAX_ITEMS: " + TSOState.MAX_ITEMS);
    System.out.println("PARAM BATCH_SIZE: " + TSOState.BATCH_SIZE);
    System.out.println("PARAM LOAD_FACTOR: " + TSOState.LOAD_FACTOR);
    System.out.println("PARAM MAX_THREADS: " + maxThreads);

    final TSOHandler handler = new TSOHandler(channelGroup, state);
    handler.start();

    bootstrap.setPipelineFactory(new TSOPipelineFactory(pipelineExecutor, handler));
    bootstrap.setOption("tcpNoDelay", false);
    //setting buffer size can improve I/O
    bootstrap.setOption("child.sendBufferSize", 1048576);
    bootstrap.setOption("child.receiveBufferSize", 1048576);
    // better to have an receive buffer predictor
    bootstrap.setOption("receiveBufferSizePredictorFactory", new AdaptiveReceiveBufferSizePredictorFactory());
    //if the server is sending 1000 messages per sec, optimum write buffer water marks will
    //prevent unnecessary throttling, Check NioSocketChannelConfig doc
    bootstrap.setOption("writeBufferLowWaterMark", 32 * 1024);
    bootstrap.setOption("writeBufferHighWaterMark", 64 * 1024);

    bootstrap.setOption("child.tcpNoDelay", false);
    bootstrap.setOption("child.keepAlive", true);
    bootstrap.setOption("child.reuseAddress", true);
    bootstrap.setOption("child.connectTimeoutMillis", 60000);

    // *** Start the Netty running ***

    // Create the monitor
    ThroughputMonitor monitor = new ThroughputMonitor(state);
    // Add the parent channel to the group
    Channel channel = bootstrap.bind(new InetSocketAddress(config.getPort()));
    channelGroup.add(channel);

    // Compacter handler
    ChannelFactory comFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
            Executors.newCachedThreadPool(), (Runtime.getRuntime().availableProcessors() * 2 + 1) * 2);
    ServerBootstrap comBootstrap = new ServerBootstrap(comFactory);
    ChannelGroup comGroup = new DefaultChannelGroup("compacter");
    final CompacterHandler comHandler = new CompacterHandler(comGroup, state);
    comBootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new ObjectDecoder());
            pipeline.addLast("encoder", new ObjectEncoder());
            pipeline.addLast("handler", comHandler);
            return pipeline;
        }
    });
    comBootstrap.setOption("tcpNoDelay", false);
    comBootstrap.setOption("child.tcpNoDelay", false);
    comBootstrap.setOption("child.keepAlive", true);
    comBootstrap.setOption("child.reuseAddress", true);
    comBootstrap.setOption("child.connectTimeoutMillis", 100);
    comBootstrap.setOption("readWriteFair", true);
    channel = comBootstrap.bind(new InetSocketAddress(config.getPort() + 1));

    // Starts the monitor
    monitor.start();
    synchronized (lock) {
        while (!finish) {
            try {
                lock.wait();
            } catch (InterruptedException e) {
                break;
            }
        }
    }

    //timestampOracle.stop();
    handler.stop();
    comHandler.stop();
    state.stop();

    // *** Start the Netty shutdown ***

    // End the monitor
    System.out.println("End of monitor");
    monitor.interrupt();
    // Now close all channels
    System.out.println("End of channel group");
    channelGroup.close().awaitUninterruptibly();
    comGroup.close().awaitUninterruptibly();
    // Close the executor for Pipeline
    System.out.println("End of pipeline executor");
    pipelineExecutor.shutdownNow();
    // Now release resources
    System.out.println("End of resources");
    factory.releaseExternalResources();
    comFactory.releaseExternalResources();
}