Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright 2013 Netflix, Inc.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
An object that periodically updates a DataAccumulator, swapping between the buffers.

netlixoss $
$Revision: $
 public class DataPublisher {
     private static final String THREAD_NAME = "DataPublisher";
     private static final boolean DAEMON_THREADS = true;
     private static ScheduledExecutorService sharedExecutor = null;
     private final DataAccumulator accumulator;
     private final long delayMillis;
     private Future<?> future = null;

Creates a new DataPublisher. When created it is not running; it is up to the caller to call start().

accumulator the DataAccumulator to periodically publish
delayMillis the number of milliseconds between publish events
     public DataPublisher(DataAccumulator accumulator,
                          long delayMillis) {
         this. = accumulator;
         this. = delayMillis;

Gets the DataAccumulator that is managed by this publisher.
     public DataAccumulator getDataAccumulator() {
         return ;

Is the DataPublisher scheduled to run?
     public synchronized boolean isRunning() {
         return ( != null);
      * Scheduling data publication

Starts the DataPublisher. The method DataAccumulator.publish() will be called approximately every delayMillis milliseconds. If the publisher has already been started, does nothing.

See also:
     public synchronized void start() {
         if ( == null) {
             Runnable task = new Runnable() {
                                 public void run() {
                                     try {
                                     } catch (Exception e) {
              = getExecutor().scheduleWithFixedDelay(task,

Gets the java.util.concurrent.ScheduledExecutorService to use to run the task to periodically update the DataAccumulator. The default uses a global executor pool for all DataPublishers. Subclasses are free to override this if desired, for example to use a per-publisher executor pool.
    protected synchronized ScheduledExecutorService getExecutor() {
        if ( == null) {
             = Executors.newScheduledThreadPool(1, new PublishThreadFactory());
        return ;
    private static final class PublishThreadFactory implements ThreadFactory {
        PublishThreadFactory() { }
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            return t;

Stops publishing new data.

See also:
    public synchronized void stop() {
        if ( != null) {
             = null;

Called if an attempt to publish data throws an exception. The default does nothing. Subclasses are free to override this.
    protected void handleException(Exception e) {
        // Do nothing, for now
// DataPublisher
New to GrepCode? Check out our FAQ X