Example usage for java.util.concurrent ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor

List of usage examples for java.util.concurrent ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor

Introduction

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

Prototype

public ScheduledThreadPoolExecutor(int corePoolSize) 

Source Link

Document

Creates a new ScheduledThreadPoolExecutor with the given core pool size.

Usage

From source file:com.linkedin.pinot.broker.broker.BrokerServerBuilder.java

public void buildNetwork() throws ConfigurationException {
    // build transport
    Configuration transportConfigs = _config.subset(TRANSPORT_CONFIG_PREFIX);
    TransportClientConf conf = new TransportClientConf();
    conf.init(transportConfigs);/*from  w ww  .ja v  a 2 s . c om*/

    _registry = new MetricsRegistry();
    MetricsHelper.initializeMetrics(_config.subset(METRICS_CONFIG_PREFIX));
    MetricsHelper.registerMetricsRegistry(_registry);
    _brokerMetrics = new BrokerMetrics(_registry);
    _brokerMetrics.initializeGlobalMeters();
    _state.set(State.INIT);
    _eventLoopGroup = new NioEventLoopGroup();
    /**
     * Some of the client metrics uses histogram which is doing synchronous operation.
     * These are fixed overhead per request/response.
     * TODO: Measure the overhead of this.
     */
    final NettyClientMetrics clientMetrics = new NettyClientMetrics(_registry, "client_");

    // Setup Netty Connection Pool
    _resourceManager = new PooledNettyClientResourceManager(_eventLoopGroup, new HashedWheelTimer(),
            clientMetrics);
    _poolTimeoutExecutor = new ScheduledThreadPoolExecutor(50);
    // _requestSenderPool = MoreExecutors.sameThreadExecutor();
    final ConnectionPoolConfig cfg = conf.getConnPool();

    _requestSenderPool = Executors.newCachedThreadPool();

    ConnectionPoolConfig connPoolCfg = conf.getConnPool();

    _connPool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(
            connPoolCfg.getMinConnectionsPerServer(), connPoolCfg.getMaxConnectionsPerServer(),
            connPoolCfg.getIdleTimeoutMs(), connPoolCfg.getMaxBacklogPerServer(), _resourceManager,
            _poolTimeoutExecutor, _requestSenderPool, _registry);
    // MoreExecutors.sameThreadExecutor(), _registry);
    _resourceManager.setPool(_connPool);

    // Setup Routing Table
    if (conf.getRoutingMode() == RoutingMode.CONFIG) {
        final CfgBasedRouting rt = new CfgBasedRouting();
        rt.init(conf.getCfgBasedRouting());
        _routingTable = rt;
    } else {
        // Helix based routing is already initialized.
    }

    // Setup ScatterGather
    _scatterGather = new ScatterGatherImpl(_connPool, _requestSenderPool);

    // Setup Broker Request Handler

    ReduceServiceRegistry reduceServiceRegistry = buildReduceServiceRegistry();
    _requestHandler = new BrokerRequestHandler(_routingTable, _timeBoundaryService, _scatterGather,
            reduceServiceRegistry, _brokerMetrics, _config);

    // Register SerDe for data-table.
    DataTableSerDeRegistry.getInstance().register(new DataTableCustomSerDe(_brokerMetrics));

    LOGGER.info("Network initialized !!");
}

From source file:edu.umich.robot.HeadlessApplication.java

/**
 * <p>// ww  w .  j  ava2  s  .  c o m
 * Start Soar, wait for timeout or Soar to stop.
 * 
 * @param controller
 *            Simulation controller initialized.
 * @throws InterruptedException
 *             Thrown on thread interrupt.
 */
private void run(Controller controller) throws InterruptedException {
    final CountDownLatch doneSignal = new CountDownLatch(1);

    Thread shutdownHook = new Thread() {
        @Override
        public void run() {
            logger.warn("Shutdown detected.");
            shutdown.set(true);
            doneSignal.countDown();
        }
    };

    Runtime.getRuntime().addShutdownHook(shutdownHook);

    try {
        controller.addListener(SoarStoppedEvent.class, new RobotEventListener() {
            public void onEvent(RobotEvent event) {
                logger.info("Soar stop detected.");
                doneSignal.countDown();
            }
        });

        ScheduledExecutorService schexec = MoreExecutors
                .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
        ScheduledFuture<?> task = null;
        if (seconds > 0) {
            task = schexec.schedule(new Runnable() {
                public void run() {
                    logger.info("Time up.");
                    doneSignal.countDown();
                }
            }, seconds, TimeUnit.SECONDS);
        }

        controller.startSoar(cycles);

        doneSignal.await();

        if (task != null)
            task.cancel(true);
        schexec.shutdown();
    } finally {
        if (!shutdown.get())
            Runtime.getRuntime().removeShutdownHook(shutdownHook);
    }
}

From source file:com.linkedin.pinot.transport.perf.ScatterGatherPerfClient.java

private void setup() {
    MetricsRegistry registry = new MetricsRegistry();
    _timedExecutor = new ScheduledThreadPoolExecutor(1);
    _service = new ThreadPoolExecutor(10, 10, 10, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
    _eventLoopGroup = new NioEventLoopGroup(10);
    _timer = new HashedWheelTimer();

    NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_");
    PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(_eventLoopGroup, _timer,
            clientMetrics);/*from  w ww.ja va 2 s . com*/
    _pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>(1, _maxActiveConnections, 300000, 10, rm,
            _timedExecutor, MoreExecutors.sameThreadExecutor(), registry);
    rm.setPool(_pool);
    _scatterGather = new ScatterGatherImpl(_pool, _service);
    for (AsyncReader r : _readerThreads) {
        r.start();
    }
}

From source file:org.holodeckb2b.common.workerpool.WorkerPool.java

/**
 * Performs the initial setup of the worker pool.
 * //from   w w w . ja v a 2  s. com
 * @param newConfig The initial configuration to use
 */
protected void setup(IWorkerPoolConfiguration newConfig) {
    log.debug("Starting initial configuration");

    pool = new ScheduledThreadPoolExecutor(newConfig.getWorkers().size());
    workers = new ArrayList<RunningWorkerInstance>();

    for (IWorkerConfiguration newWorker : newConfig.getWorkers()) {
        addWorker(newWorker);
    }

    // Check if pool size is still sufficient
    int s = workers.size();
    if (s > pool.getCorePoolSize())
        pool.setCorePoolSize(s);
}

From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java

@Test
public void testValidatePool() throws Exception {
    PooledNettyClientResourceManager resourceManager = new PooledNettyClientResourceManager(
            new NioEventLoopGroup(), new HashedWheelTimer(), new NettyClientMetrics(null, "abc"));
    ExecutorService executorService = Executors.newCachedThreadPool();
    ScheduledExecutorService timeoutExecutor = new ScheduledThreadPoolExecutor(5);

    String serverName = "server";
    MetricsRegistry metricsRegistry = new MetricsRegistry();
    AsyncPoolResourceManagerAdapter<PooledNettyClientResourceManager.PooledClientConnection> rmAdapter = new AsyncPoolResourceManagerAdapter<>(
            _clientServer, resourceManager, executorService, metricsRegistry);
    AsyncPool<PooledNettyClientResourceManager.PooledClientConnection> pool = new AsyncPoolImpl<>(serverName,
            rmAdapter, /*maxSize=*/5, /*idleTimeoutMs=*/100000L, timeoutExecutor, executorService,
            /*maxWaiters=*/10, AsyncPoolImpl.Strategy.LRU, /*minSize=*/2, metricsRegistry);

    try {//from  w ww.  ja  v a  2 s .  c om
        pool.start();
        Uninterruptibles.sleepUninterruptibly(1L, TimeUnit.SECONDS);

        // Test no connection in pool
        Assert.assertTrue(pool.validate(false));
        PoolStats stats = pool.getStats();
        Assert.assertEquals(stats.getPoolSize(), 2);
        Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
        Assert.assertEquals(stats.getCheckedOut(), 0);

        // Test one connection, it should not destroy anything
        AsyncResponseFuture<PooledNettyClientResourceManager.PooledClientConnection> responseFuture = new AsyncResponseFuture<>(
                _clientServer, null);
        pool.get(responseFuture);
        Assert.assertNotNull(responseFuture.getOne());
        Assert.assertTrue(pool.validate(false));
        stats = pool.getStats();
        Assert.assertEquals(stats.getPoolSize(), 2);
        Assert.assertEquals(stats.getTotalBadDestroyed(), 0);
        Assert.assertEquals(stats.getCheckedOut(), 1);

        // Now stop the server, so that the checked out connection is invalidated
        closeServerConnection();
        Assert.assertTrue(pool.validate(false));
        stats = pool.getStats();
        Assert.assertEquals(stats.getPoolSize(), 2);
        Assert.assertEquals(stats.getTotalBadDestroyed(), 1);
        Assert.assertEquals(stats.getCheckedOut(), 1);
    } finally {
        pool.shutdown(new Callback<NoneType>() {
            @Override
            public void onSuccess(NoneType arg0) {
            }

            @Override
            public void onError(Throwable arg0) {
                Assert.fail("Shutdown error");
            }
        });
        executorService.shutdown();
        timeoutExecutor.shutdown();
    }
}

From source file:eu.stratosphere.sopremo.server.SopremoServer.java

private ScheduledThreadPoolExecutor createExecutor() {
    final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
    executor.setMaximumPoolSize(1);/* www .  j  av a  2s.co  m*/
    return executor;
}

From source file:org.wso2.andes.server.virtualhost.VirtualHostImpl.java

private VirtualHostImpl(IApplicationRegistry appRegistry, VirtualHostConfiguration hostConfig,
        MessageStore store) throws Exception {
    if (hostConfig == null) {
        throw new IllegalAccessException("HostConfig and MessageStore cannot be null");
    }//from  w w w  .  j  a  v  a2 s. c  o m

    this.appRegistry = appRegistry;
    broker = this.appRegistry.getBroker();
    configuration = hostConfig;
    name = configuration.getName();

    id = this.appRegistry.getConfigStore().createId();

    CurrentActor.get().message(VirtualHostMessages.CREATED(name));

    if (name == null || name.length() == 0) {
        throw new IllegalArgumentException("Illegal name (" + name + ") for virtualhost.");
    }

    securityManager = new SecurityManager(this.appRegistry.getSecurityManager());
    securityManager.configureHostPlugins(configuration);

    virtualHostMBean = new VirtualHostMBean();

    connectionRegistry = new ConnectionRegistry();

    houseKeepingTasks = new ScheduledThreadPoolExecutor(configuration.getHouseKeepingThreadCount());

    queueRegistry = new DefaultQueueRegistry(this);

    exchangeFactory = new DefaultExchangeFactory(this);
    exchangeFactory.initialise(configuration);

    StartupRoutingTable configFileRT = new StartupRoutingTable();

    durableConfigurationStore = configFileRT;

    if (store != null) {
        messageStore = store;
        durableConfigurationStore = store;
    } else {
        initialiseAndesStores(hostConfig);
    }

    AndesKernelBoot.startAndesCluster();
    exchangeRegistry = new DefaultExchangeRegistry(this);
    bindingFactory = new BindingFactory(this);

    // This needs to be after the RT has been defined as it creates the default durable exchanges.
    initialiseModel(configuration);
    exchangeRegistry.initialise();

    authenticationManager = ApplicationRegistry.getInstance().getAuthenticationManager();

    brokerMBean = new AMQBrokerManagerMBean(virtualHostMBean);
    brokerMBean.register();

    queueManagementInformationMBean = new QueueManagementInformationMBean(virtualHostMBean);
    queueManagementInformationMBean.register();

    initialiseHouseKeeping(hostConfig.getHousekeepingExpiredMessageCheckPeriod());

    initialiseStatistics();

}

From source file:com.facebook.share.internal.DeviceShareDialogFragment.java

private static synchronized ScheduledThreadPoolExecutor getBackgroundExecutor() {
    if (backgroundExecutor == null) {
        backgroundExecutor = new ScheduledThreadPoolExecutor(1);
    }/*from w  w  w.j  a  v  a  2s  . c  o  m*/
    return backgroundExecutor;
}

From source file:org.rifidi.edge.core.sensors.sessions.AbstractIPSensorSession.java

@Override
protected void _connect() throws IOException {
    if ((getStatus() == SessionStatus.PROCESSING || getStatus() == SessionStatus.CREATED
            || getStatus() == SessionStatus.LOGGINGIN || getStatus() == SessionStatus.CLOSED)
            && connecting.compareAndSet(false, true)) {
        try {// ww  w  .ja  v  a  2 s .  c  om
            setStatus(SessionStatus.CONNECTING);
            socket = null;
            // if an executor exists, execute it (delete the executor :))
            if (processing.get()) {
                if (!processing.compareAndSet(true, false)) {
                    logger.warn("Killed a non active executor. That should not happen. ");
                }
                // TODO: better would be to have a method in
                // AbstractSensorSession that handles the shutdown of the
                // executor
                executor.shutdownNow();
                executor = null;
                resetCommands();
            }
            // check if somebody is currently reading
            if (readThread != null) {
                readThread.interrupt();
                try {
                    logger.debug("Killing read thread.");
                    readThread.join();
                    readThread = null;
                } catch (InterruptedException e1) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
            // check if somebody is currently writing
            if (writeThread != null) {
                writeThread.interrupt();
                try {
                    logger.debug("Killing write thread.");
                    writeThread.join();
                    writeThread = null;
                } catch (InterruptedException e1) {
                    Thread.currentThread().interrupt();
                    return;
                }
            }
            executor = new ScheduledThreadPoolExecutor(1);
            // try to open the socket
            logger.info("Attempting to connect to : " + host + ":" + port);

            // begin reconnect loop. We have an atomic boolean that should
            // be true as long as we are in the loop
            if (!reconnectLoop.compareAndSet(false, true)) {
                logger.warn("atomic boolean for recconnect " + "loop was already true.");
            }
            try {
                // reconnect loop. try to connect maxConAteempts number of
                // times, unless maxConAttempts is -1, in which case try
                // forever.
                for (int connCount = 0; connCount < maxConAttempts || maxConAttempts == -1; connCount++) {

                    try {
                        socket = new Socket(host, port);
                        if (logger.isDebugEnabled()) {
                            logger.info("Socket connection successful to " + this.host + ":" + this.port);
                        }
                        break;
                    } catch (IOException e) {
                        // do nothing
                    }

                    // print info message the first time
                    if (logger.isInfoEnabled() && connCount == 0) {
                        logger.info("Session " + this.getID() + " on " + this.getSensor().getID()
                                + " Starting reconnect attempts to " + this.host + ":" + this.port);
                    }

                    // wait for a specified number of ms or for a notify
                    // call
                    try {
                        synchronized (reconnectLoop) {
                            reconnectLoop.wait(this.reconnectionInterval);
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                    if (!reconnectLoop.get())
                        break;
                }
            } finally {
                reconnectLoop.compareAndSet(true, false);
            }

            // no socket, we are screwed
            if (socket == null) {
                // revert
                setStatus(SessionStatus.CREATED);
                connecting.compareAndSet(true, false);
                throw new IOException("Unable to reach reader.");
            }

            readThread = new Thread(new ReadThread(socket.getInputStream(), getMessageParsingStrategyFactory(),
                    getMessageProcessingStrategyFactory()));
            writeThread = new Thread(new WriteThread(socket.getOutputStream(), writeQueue));
            readThread.start();
            writeThread.start();
            setStatus(SessionStatus.LOGGINGIN);
            // do the logical connect
            try {
                if (!onConnect()) {
                    onConnectFailed();
                    return;
                }
            } catch (IOException e) {
                logger.warn("Unable to connect during login: " + e);
                connecting.compareAndSet(true, false);
                connect();
            }
            logger.info("Session " + this.getID() + " on " + this.getSensor().getID()
                    + " connected successfully to  " + this.host + ":" + this.port);

            // TODO: paramaterize the keepalive frequency
            submit(getKeepAliveCommand(), 10L, TimeUnit.SECONDS);

            // create thread that checks if the write thread dies and
            // restart it
            connectionGuardian = new ConnectionGaurdian();
            connectionGuardian.start();
            setStatus(SessionStatus.PROCESSING);
            if (!processing.compareAndSet(false, true)) {
                logger.warn("Executor was already active! ");
            }
        } finally {
            connecting.compareAndSet(true, false);
        }
    }
}

From source file:tayler.TailerTest.java

@Test
public void testStopWithNoFileUsingExecutor() throws Exception {
    final File file = new File(getTestDirectory(), "nosuchfile");
    assertFalse("nosuchfile should not exist", file.exists());
    TestTailerListener listener = new TestTailerListener();
    int delay = 100;
    int idle = 50; // allow time for thread to work
    tailer = new Tailer(file, listener, delay, false);
    Executor exec = new ScheduledThreadPoolExecutor(1);
    exec.execute(tailer);//from   www  .j  a v a 2s  . c o  m
    Thread.sleep(idle);
    tailer.stop();
    tailer = null;
    Thread.sleep(delay + idle);
    assertNull("Should not generate Exception", listener.exception);
    assertEquals("Expected init to be called", 1, listener.initialised.get());
    assertTrue("fileNotFound should be called", listener.notFound.get() > 0);
    assertEquals("fileRotated should be not be called", 0, listener.rotated.get());
}