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:org.apache.storm.grouping.LoadAwareShuffleGroupingTest.java

private void runSimpleBenchmark(LoadAwareCustomStreamGrouping grouper, List<Integer> availableTaskIds,
        LoadMapping loadMapping) {//  ww w. j a  v a 2s.c  om
    // Task Id not used, so just pick a static value
    final int inputTaskId = 100;

    WorkerTopologyContext context = mockContext(availableTaskIds);
    grouper.prepare(context, null, availableTaskIds);

    // periodically calls refreshLoad in 1 sec to simulate worker load update timer
    ScheduledExecutorService refreshService = MoreExecutors
            .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
    refreshService.scheduleAtFixedRate(() -> grouper.refreshLoad(loadMapping), 1, 1, TimeUnit.SECONDS);

    long current = System.currentTimeMillis();
    int idx = 0;
    while (true) {
        grouper.chooseTasks(inputTaskId, Lists.newArrayList());

        idx++;
        if (idx % 100000 == 0) {
            // warm up 60 seconds
            if (System.currentTimeMillis() - current >= 60_000) {
                break;
            }
        }
    }

    current = System.currentTimeMillis();
    for (int i = 1; i <= 2_000_000_000; i++) {
        grouper.chooseTasks(inputTaskId, Lists.newArrayList());
    }

    LOG.info("Duration: {} ms", (System.currentTimeMillis() - current));

    refreshService.shutdownNow();
}

From source file:org.apache.storm.grouping.LoadAwareShuffleGroupingTest.java

private void runMultithreadedBenchmark(LoadAwareCustomStreamGrouping grouper, List<Integer> availableTaskIds,
        LoadMapping loadMapping, int numThreads) throws InterruptedException, ExecutionException {
    // Task Id not used, so just pick a static value
    final int inputTaskId = 100;

    final WorkerTopologyContext context = mockContext(availableTaskIds);

    // Call prepare with our available taskIds
    grouper.prepare(context, null, availableTaskIds);

    // periodically calls refreshLoad in 1 sec to simulate worker load update timer
    ScheduledExecutorService refreshService = MoreExecutors
            .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
    refreshService.scheduleAtFixedRate(() -> grouper.refreshLoad(loadMapping), 1, 1, TimeUnit.SECONDS);

    long current = System.currentTimeMillis();
    int idx = 0;/*from w  ww . j  av a  2  s .c o m*/
    while (true) {
        grouper.chooseTasks(inputTaskId, Lists.newArrayList());

        idx++;
        if (idx % 100000 == 0) {
            // warm up 60 seconds
            if (System.currentTimeMillis() - current >= 60_000) {
                break;
            }
        }
    }

    final int groupingExecutionsPerThread = 2_000_000_000;

    List<Callable<Long>> threadTasks = Lists.newArrayList();
    for (int x = 0; x < numThreads; x++) {
        Callable<Long> threadTask = new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                long current = System.currentTimeMillis();
                for (int i = 1; i <= groupingExecutionsPerThread; i++) {
                    grouper.chooseTasks(inputTaskId, Lists.newArrayList());
                }
                return System.currentTimeMillis() - current;
            }
        };

        // Add to our collection.
        threadTasks.add(threadTask);
    }

    ExecutorService executor = Executors.newFixedThreadPool(threadTasks.size());
    List<Future<Long>> taskResults = executor.invokeAll(threadTasks);

    // Wait for all tasks to complete
    Long maxDurationMillis = 0L;
    for (Future taskResult : taskResults) {
        while (!taskResult.isDone()) {
            Thread.sleep(100);
        }
        Long durationMillis = (Long) taskResult.get();
        if (maxDurationMillis < durationMillis) {
            maxDurationMillis = durationMillis;
        }
    }

    LOG.info("Max duration among threads is : {} ms", maxDurationMillis);

    refreshService.shutdownNow();
}

From source file:ddf.metrics.collector.rrd4j.RrdJmxCollector.java

/**
 * Configures a scheduled threaded executor to poll the metric's MBean periodically and add a
 * sample to the RRD file with the metric's current value.
 *
 * @throws CollectorException//from   ww w.  ja va 2  s  .  com
 */
public void updateSamples() throws CollectorException {
    LOGGER.trace("ENTERING: updateSamples");

    if (executor == null) {
        executor = new ScheduledThreadPoolExecutor(1);
    }

    final Runnable updater = new Runnable() {
        public void run() {
            Object attr = null;
            try {
                attr = localMBeanServer.getAttribute(new ObjectName(mbeanName), mbeanAttributeName);

                LOGGER.trace("Sampling attribute {} from MBean {}", mbeanAttributeName, mbeanName);

                // Cast the metric's sampled value to the appropriate data type
                double val = 0;
                if (attr instanceof Integer) {
                    val = (Integer) attr;
                } else if (attr instanceof Long) {
                    val = ((Long) attr).intValue();
                } else if (attr instanceof Float) {
                    val = ((Float) attr);
                } else if (attr instanceof Double) {
                    val = ((Double) attr);
                } else {
                    throw new IllegalArgumentException(
                            "Unsupported type " + attr + " for attribute " + mbeanAttributeName);
                }

                LOGGER.trace("MBean attribute {} has value = {}", mbeanAttributeName, val);

                // If first time this metric has been sampled, then need to create a
                // sample in the RRD file
                if (sample == null) {
                    sample = rrdDb.createSample();
                }

                try {
                    long now = System.currentTimeMillis() / MILLIS_PER_SECOND;
                    long lastUpdateTime = rrdDb.getLastUpdateTime();

                    // Add metric's sample to RRD file with current timestamp
                    if (now - rrdDb.getLastUpdateTime() >= minimumUpdateTimeDelta) {
                        updateSample(now, val);
                    } else {
                        LOGGER.debug(
                                "Skipping sample update because time between updates is less than {} seconds",
                                minimumUpdateTimeDelta);

                        sampleSkipCount++;

                        LOGGER.debug("now = {},   lastUpdateTime = {}   (sampleSkipCount = {})", now,
                                lastUpdateTime, sampleSkipCount);
                    }
                } catch (IllegalArgumentException iae) {
                    LOGGER.error("Dropping sample of datasource {}", rrdDataSourceName, iae);
                }
            } catch (MalformedObjectNameException e) {
                LOGGER.warn("Problems getting MBean attribute {}", mbeanAttributeName, e);
            } catch (AttributeNotFoundException e) {
                LOGGER.warn("Problems getting MBean attribute {}", mbeanAttributeName, e);
            } catch (InstanceNotFoundException e) {
                LOGGER.warn("Problems getting MBean attribute {}", mbeanAttributeName, e);
            } catch (MBeanException e) {
                LOGGER.warn("Problems getting MBean attribute {}", mbeanAttributeName, e);
            } catch (ReflectionException e) {
                LOGGER.warn("Problems getting MBean attribute {}", mbeanAttributeName, e);
            } catch (IOException e) {
                LOGGER.warn("Error updating RRD", e);
            }
        }
    };

    // Setup threaded scheduler to retrieve this MBean attribute's value
    // at the specified sample rate
    LOGGER.debug("Setup ScheduledThreadPoolExecutor for MBean {}", mbeanName);
    executor.scheduleWithFixedDelay(updater, 0, sampleRate, TimeUnit.SECONDS);

    LOGGER.trace("EXITING: updateSamples");
}

From source file:ome.formats.OMEROMetadataStoreClient.java

/**
 * Initialize all services needed//from   ww w .  j  a  v a2  s  .co m
 *
 * @param manageLifecylce
 *
 *            Whether or not to call the {@link Thread#start()} method on
 *            the {@link #keepAlive} instance. This will be set to false
 *            when an {@link omero.client} or a {@link ServiceFactoryPrx}
 *            instance is provided to {@link #initialize(client)} since the
 *            assumption is that the consumer will take care of the keep
 *            alive. In that case, {@link #closeServices()} should be called
 *            when importing is finished.
 *
 *  @param group
 *
 *            Value to pass to {@link #setCurrentGroup(long)} if not null.
 *
 * @throws ServerError
 */
private void initializeServices(boolean manageLifecycle, Long group) throws ServerError {
    // Blitz services
    iAdmin = serviceFactory.getAdminService();
    iQuery = serviceFactory.getQueryService();
    eventContext = iAdmin.getEventContext();
    if (group != null) {
        setCurrentGroup(group);
    }
    iUpdate = serviceFactory.getUpdateService();
    rawFileStore = serviceFactory.createRawFileStore();
    rawPixelStore = serviceFactory.createRawPixelsStore();
    thumbnailStore = serviceFactory.createThumbnailStore();
    iRepoInfo = serviceFactory.getRepositoryInfoService();
    iContainer = serviceFactory.getContainerService();
    iSettings = serviceFactory.getRenderingSettingsService();
    delegate = MetadataStorePrxHelper.checkedCast(serviceFactory.getByName(METADATASTORE.value));

    // Client side services
    enumProvider = new IQueryEnumProvider(iQuery);
    instanceProvider = new BlitzInstanceProvider(enumProvider);

    // Default model processors
    modelProcessors.add(new PixelsProcessor());
    modelProcessors.add(new ChannelProcessor());
    modelProcessors.add(new InstrumentProcessor());
    modelProcessors.add(new PlaneInfoProcessor());
    modelProcessors.add(new WellProcessor());
    modelProcessors.add(new ShapeProcessor());
    modelProcessors.add(new TargetProcessor()); // Should be second last
    modelProcessors.add(new ReferenceProcessor()); // Should be last

    // Fix check for broken 4.0 immersions table
    //checkImmersions();

    // Start our keep alive executor
    if (manageLifecycle) {
        if (executor == null) {
            executor = new ScheduledThreadPoolExecutor(1);
            executor.scheduleWithFixedDelay(keepAlive, 60, 60, TimeUnit.SECONDS);
        }
    }
    keepAlive.setClient(this); // This is used elsewhere.
}

From source file:org.rifidi.edge.adapter.llrp.LLRPReaderSession.java

/**
 * This logic executes as soon as a socket is established to initialize the
 * connection. It occurs before any commands are scheduled
 */// w ww  . j av a2s  . c  o m
private void onConnect() {
    logger.info("LLRP Session " + this.getID() + " on sensor " + this.getSensor().getID()
            + " attempting to log in to " + host + ":" + port);
    setStatus(SessionStatus.LOGGINGIN);
    executor = new ScheduledThreadPoolExecutor(1);

    try {
        SET_READER_CONFIG config = createSetReaderConfig();
        config.setMessageID(new UnsignedInteger(messageID++));

        SET_READER_CONFIG_RESPONSE config_response = (SET_READER_CONFIG_RESPONSE) connection.transact(config);

        StatusCode sc = config_response.getLLRPStatus().getStatusCode();
        if (sc.intValue() != StatusCode.M_Success) {
            if (config_response.getLLRPStatus().getStatusCode().toInteger() != 0) {
                try {
                    logger.error("Problem with SET_READER_CONFIG: \n" + config_response.toXMLString());
                } catch (InvalidLLRPMessageException e) {
                    logger.warn("Cannot print XML for " + "SET_READER_CONFIG_RESPONSE");
                }
            }
        }

        // BytesToEnd_HEX data = new BytesToEnd_HEX();
        // CUSTOM_MESSAGE msg = new CUSTOM_MESSAGE();
        // msg.setVendorIdentifier(new UnsignedInteger(25882));
        // msg.setMessageSubtype(new UnsignedByte(21));
        // data.add(new SignedByte(0));
        // data.add(new SignedByte(0));
        // data.add(new SignedByte(0));
        // data.add(new SignedByte(0));
        // msg.setData(data);
        // connection.transact(msg);

        if (!processing.compareAndSet(false, true)) {
            logger.warn("Executor was already active! ");
        }
        this.lastTagTimestamp = System.currentTimeMillis();
        submit(getTimeoutCommand(this), 10, TimeUnit.SECONDS);
        setStatus(SessionStatus.PROCESSING);

    } catch (TimeoutException e) {
        logger.error(e.getMessage());
        disconnect();
    } catch (ClassCastException ex) {
        logger.error(ex.getMessage());
        disconnect();
    }

}

From source file:com.facebook.appevents.AppEventsLogger.java

private static void initializeTimersIfNeeded() {
    synchronized (staticLock) {
        if (backgroundExecutor != null) {
            return;
        }//w ww.ja  va2 s.  c  om
        backgroundExecutor = new ScheduledThreadPoolExecutor(1);
    }

    final Runnable flushRunnable = new Runnable() {
        @Override
        public void run() {
            if (getFlushBehavior() != FlushBehavior.EXPLICIT_ONLY) {
                flushAndWait(FlushReason.TIMER);
            }
        }
    };

    backgroundExecutor.scheduleAtFixedRate(flushRunnable, 0, FLUSH_PERIOD_IN_SECONDS, TimeUnit.SECONDS);

    final Runnable attributionRecheckRunnable = new Runnable() {
        @Override
        public void run() {
            Set<String> applicationIds = new HashSet<String>();
            synchronized (staticLock) {
                for (AccessTokenAppIdPair accessTokenAppId : stateMap.keySet()) {
                    applicationIds.add(accessTokenAppId.getApplicationId());
                }
            }
            for (String applicationId : applicationIds) {
                Utility.queryAppSettings(applicationId, true);
            }
        }
    };

    backgroundExecutor.scheduleAtFixedRate(attributionRecheckRunnable, 0,
            APP_SUPPORTS_ATTRIBUTION_ID_RECHECK_PERIOD_IN_SECONDS, TimeUnit.SECONDS);
}

From source file:com.att.nsa.mr.client.impl.MRSimplerBatchPublisher.java

private MRSimplerBatchPublisher(Collection<String> hosts, String topic, int maxBatchSize, long maxBatchAgeMs,
        boolean compress) throws MalformedURLException {
    super(hosts);

    if (topic == null || topic.length() < 1) {
        throw new IllegalArgumentException("A topic must be provided.");
    }//from w  w  w  .  ja v a  2  s .c  om

    fHostSelector = new HostSelector(hosts, null);
    fClosed = false;
    fTopic = topic;
    fMaxBatchSize = maxBatchSize;
    fMaxBatchAgeMs = maxBatchAgeMs;
    fCompress = compress;

    fPending = new LinkedBlockingQueue<TimestampedMessage>();
    fDontSendUntilMs = 0;
    fExec = new ScheduledThreadPoolExecutor(1);
    pubResponse = new MRPublisherResponse();

}

From source file:omero.gateway.Gateway.java

/**
 * Initiates a session/*  ww  w . ja  va2 s  . com*/
 * 
 * @param c
 *            The login credentials
 * @return The client
 * @throws DSOutOfServiceException
 */
private client createSession(LoginCredentials c) throws DSOutOfServiceException {
    client secureClient = null;

    try {
        // client must be cleaned up by caller.
        List<String> args = c.getArguments();
        String username;
        if (args != null) {
            secureClient = new client(args.toArray(new String[args.size()]));
            username = secureClient.getProperty("omero.user");
        } else {
            username = c.getUser().getUsername();
            if (c.getServer().getPort() > 0)
                secureClient = new client(c.getServer().getHostname(), c.getServer().getPort());
            else
                secureClient = new client(c.getServer().getHostname());
        }
        secureClient.setAgent(c.getApplicationName());
        ServiceFactoryPrx entryEncrypted;
        boolean session = true;
        ServiceFactoryPrx guestSession = null;
        try {
            // Check if it is a session first
            guestSession = secureClient.createSession("guest", "guest");
            this.pcs.firePropertyChange(PROP_SESSION_CREATED, null, secureClient.getSessionId());
            guestSession.getSessionService().getSession(username);
        } catch (Exception e) {
            // thrown if it is not a session or session has expired.
            session = false;
        } finally {
            String id = secureClient.getSessionId();
            secureClient.closeSession();
            this.pcs.firePropertyChange(PROP_SESSION_CLOSED, null, id);
        }
        if (session) {
            entryEncrypted = secureClient.joinSession(username);
        } else {
            if (args != null) {
                entryEncrypted = secureClient.createSession();
            } else {
                entryEncrypted = secureClient.createSession(c.getUser().getUsername(),
                        c.getUser().getPassword());
            }
        }
        this.pcs.firePropertyChange(PROP_SESSION_CREATED, null, secureClient.getSessionId());
        serverVersion = entryEncrypted.getConfigService().getVersion();

        if (c.isCheckNetwork()) {
            try {
                String ip = InetAddress.getByName(c.getServer().getHostname()).getHostAddress();
                networkChecker = new NetworkChecker(ip, log);
            } catch (Exception e) {
                if (log != null)
                    log.warn(this,
                            new LogMessage("Failed to get inet address: " + c.getServer().getHostname(), e));
            }
        }

        Runnable r = new Runnable() {
            public void run() {
                try {
                    keepSessionAlive();
                } catch (Throwable t) {
                    if (log != null)
                        log.warn(this, new LogMessage("Exception while keeping the services alive", t));
                }
            }
        };
        keepAliveExecutor = new ScheduledThreadPoolExecutor(1);
        keepAliveExecutor.scheduleWithFixedDelay(r, 60, 60, TimeUnit.SECONDS);
    } catch (Throwable e) {
        if (secureClient != null) {
            secureClient.__del__();
        }
        throw new DSOutOfServiceException("Can't connect to OMERO. OMERO info not valid", e);
    }
    return secureClient;
}

From source file:com.att.nsa.mr.client.impl.MRSimplerBatchPublisher.java

private MRSimplerBatchPublisher(Collection<String> hosts, String topic, int maxBatchSize, long maxBatchAgeMs,
        boolean compress, boolean allowSelfSignedCerts, int httpThreadOccurnace) throws MalformedURLException {
    super(hosts);

    if (topic == null || topic.length() < 1) {
        throw new IllegalArgumentException("A topic must be provided.");
    }/*  ww  w .ja  v  a 2s .c om*/

    fHostSelector = new HostSelector(hosts, null);
    fClosed = false;
    fTopic = topic;
    fMaxBatchSize = maxBatchSize;
    fMaxBatchAgeMs = maxBatchAgeMs;
    fCompress = compress;
    threadOccuranceTime = httpThreadOccurnace;
    fPending = new LinkedBlockingQueue<TimestampedMessage>();
    fDontSendUntilMs = 0;
    fExec = new ScheduledThreadPoolExecutor(1);
    fExec.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            send(false);
        }
    }, 100, threadOccuranceTime, TimeUnit.MILLISECONDS);
}

From source file:com.f8full.casserolesencours.CasserolesEnCoursActivity.java

/**
 * On service click.//from   w w w .  j  a v  a 2s  .  c o m
 *
 * @param view the view
 */
public void onServiceClick(View view) {
    boolean isChecked = ((ToggleButton) view).isChecked();
    if (isChecked) {
        //turn on
        mAlohar.startServices();
        //StatusView.setText("Service is running!");
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setText(getString(R.string.geolocationStatusOn));
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setTextColor(getResources().getColor(R.color.text_red));
        ((TextView) findViewById(R.id.distanceFilterNA)).setVisibility(View.INVISIBLE);
        findViewById(R.id.FrequencyGroupLayout).setVisibility(View.VISIBLE);
        mDistanceFilterSpinner.setEnabled(true);

        //String freq = ((RadioButton)findViewById( ((RadioGroup)findViewById(R.id.pollFrequencyRadioGroup)).getCheckedRadioButtonId() )).getText().toString();
        String text = getString(R.string.geolocationStatusOnNotification);//String.format(getString(R.string.geolocationOnNotification), freq);
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = text;

        long when = System.currentTimeMillis();

        mNotification = new Notification(icon, tickerText, when);
        mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
        CharSequence contentText = getString(R.string.geolocationOnNotificationNoFreq);
        CharSequence contentTitle = getString(R.string.app_name);
        Intent notificationIntent = new Intent(this, CasserolesEnCoursActivity.class);
        mNotification.contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mNotification.setLatestEventInfo(this, contentTitle, contentText, mNotification.contentIntent);
        mNotificationManager.notify(R.layout.main, mNotification);

    } else {
        //turn off
        mAlohar.stopServices();
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setText(getString(R.string.geolocationStatusOff));
        ((TextView) findViewById(R.id.geolocationServiceStatus))
                .setTextColor(getResources().getColor(R.color.text_green));
        ((TextView) findViewById(R.id.distanceFilterNA)).setVisibility(View.VISIBLE);
        findViewById(R.id.FrequencyGroupLayout).setVisibility(View.INVISIBLE);
        mDistanceFilterSpinner.setSelection(0);
        mDistanceFilterSpinner.setEnabled(false);

        //((RadioGroup) findViewById(R.id.pollFrequencyRadioGroup)).check(-1);
        cancelActiveTasks();
        mLocationPollThreadExecutor.purge();

        //That is a hack, some code path must be missed, I'm tracking a thread leak bug when app is in background for a long time
        mLocationPollThreadExecutor.shutdown();
        mLocationPollThreadExecutor = new ScheduledThreadPoolExecutor(20);

        mIsStationary = true;
        mNotificationManager.cancel(R.layout.main);
    }
}