Example usage for java.util.concurrent RejectedExecutionHandler RejectedExecutionHandler

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

Introduction

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

Prototype

RejectedExecutionHandler

Source Link

Usage

From source file:com.netflix.suro.client.async.AsyncSuroClient.java

@Inject
public AsyncSuroClient(ClientConfig config, Queue4Client messageQueue, ConnectionPool connectionPool) {
    this.config = config;
    this.messageQueue = messageQueue;

    this.connectionPool = connectionPool;
    this.builder = new MessageSetBuilder(config).withCompression(Compression.create(config.getCompression()));

    poller.execute(createPoller());/*from  w  ww . j a  v a2  s.co m*/

    jobQueue = new ArrayBlockingQueue<Runnable>(config.getAsyncJobQueueCapacity()) {
        @Override
        public boolean offer(Runnable runnable) {
            try {
                put(runnable); // not to reject the task, slowing down
            } catch (InterruptedException e) {
                // do nothing
            }
            return true;
        }
    };

    senders = new ThreadPoolExecutor(config.getAsyncSenderThreads(), config.getAsyncSenderThreads(), 10,
            TimeUnit.SECONDS, jobQueue, new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    TMessageSet messageSet = ((AsyncSuroSender) r).getMessageSet();
                    for (Message m : new MessageSetReader(messageSet)) {
                        restore(m);
                    }
                }
            });

    rateLimiter = new RateLimiter(rateLimitMsgPerSec.get());
    tags = GraphitePublisher.createSimpleTagList(config.getApp() + "_" + config.getClientType() + "_client");
    Monitors.registerObject(this);
}

From source file:org.wso2.carbon.analytics.iots.smarthomeanalytics.SensorAgentService.java

public static void initialization() {
    try {// ww  w . ja  va2 s  .c  om
        dataPublisher = new DataPublisher(
                Constants.TRASPORT_LEVEL_PROTOCOL + Constants.SERVER_IP_ADDRESS + ":" + Constants.SERVER_PORT,
                Constants.USERNAME, Constants.PASSWORD);
        if (executorService == null) {
            RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        executor.getQueue().put(r);
                    } catch (InterruptedException e) {
                        log.error("Exception while adding event to executor queue : " + e.getMessage(), e);
                    }
                }
            };
            executorService = new ThreadPoolExecutor(Constants.ADAPTER_MIN_THREAD_POOL_SIZE,
                    Constants.ADAPTER_MAX_THREAD_POOL_SIZE, Constants.DEFAULT_KEEP_ALIVE_TIME_IN_MILLS,
                    TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>(Constants.ADAPTER_EXECUTOR_JOB_QUEUE_SIZE),
                    rejectedExecutionHandler);
        }
    } catch (DataEndpointConfigurationException e) {
        log.error("Required fields are missing in Data endpoint configuration file ", e);
    } catch (DataEndpointException e) {
        log.error("Error while connecting to configured endpoint ", e);
    } catch (DataEndpointAgentConfigurationException e) {
        log.error("Required fields are missing in Data endpoint agent configuration file ", e);
    } catch (TransportException e) {
        log.error("Error while connecting to server through Thrift", e);
    } catch (DataEndpointAuthenticationException e) {
        log.error("Please check whether user name and password is correct,", e);
    }
}

From source file:org.deeplearning4j.models.word2vec.Word2Vec.java

/**
 * Train the model/*ww w  . ja  va2  s .  c  o  m*/
 */
public void fit() throws IOException {
    boolean loaded = buildVocab();
    //save vocab after building
    if (!loaded && saveVocab)
        vocab().saveVocab();
    if (stopWords == null)
        readStopWords();

    log.info("Training word2vec multithreaded");

    if (sentenceIter != null)
        sentenceIter.reset();
    if (docIter != null)
        docIter.reset();

    int[] docs = vectorizer.index().allDocs();

    if (docs.length < 1) {
        vectorizer.fit();
    }

    docs = vectorizer.index().allDocs();
    if (docs.length < 1) {
        throw new IllegalStateException("No documents found");
    }

    totalWords = vectorizer.numWordsEncountered();
    if (totalWords < 1)
        throw new IllegalStateException("Unable to train, total words less than 1");

    totalWords *= numIterations;

    log.info("Processing sentences...");

    AtomicLong numWordsSoFar = new AtomicLong(0);
    final AtomicLong nextRandom = new AtomicLong(5);
    ExecutorService exec = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
            Runtime.getRuntime().availableProcessors(), 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(), new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                    executor.submit(r);
                }
            });

    final Queue<List<VocabWord>> batch2 = new ConcurrentLinkedDeque<>();
    vectorizer.index().eachDoc(new Function<List<VocabWord>, Void>() {
        @Override
        public Void apply(List<VocabWord> input) {
            List<VocabWord> batch = new ArrayList<>();
            addWords(input, nextRandom, batch);
            if (!batch.isEmpty()) {
                batch2.add(batch);
            }

            return null;
        }
    }, exec);

    exec.shutdown();
    try {
        exec.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ActorSystem actorSystem = ActorSystem.create();

    for (int i = 0; i < numIterations; i++)
        doIteration(batch2, numWordsSoFar, nextRandom, actorSystem);
    actorSystem.shutdown();

}

From source file:com.jkoolcloud.tnt4j.streams.custom.dirStream.DirStreamingManager.java

private void initialize() {
    executorService = new ThreadPoolExecutor(CORE_TREAD_POOL_SIZE, MAX_TREAD_POOL_SIZE, KEEP_ALIVE_TIME,
            TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(MAX_TREAD_POOL_SIZE * 2),
            new TNTInputStream.StreamsThreadFactory("DirStreamingManagerExecutorThread-")); // NON-NLS

    executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override//from  w  ww  .ja v a 2 s .  c o m
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                boolean added = executor.getQueue().offer(r, offerTimeout, TimeUnit.SECONDS);
                if (!added) {
                    LOGGER.log(OpLevel.WARNING,
                            StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                                    "TNTInputStream.tasks.buffer.limit"),
                            offerTimeout);
                    notifyStreamingJobRejected(r);
                }
            } catch (InterruptedException exc) {
                LOGGER.log(OpLevel.WARNING,
                        StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
                                "DirStreamingManager.job.offer.interrupted"),
                        ((StreamingJob) r).getJobId(), exc);
            }
        }
    });

    dirWatchdog = new DirWatchdog(dirPath, DirWatchdog.getDefaultFilter(fileWildcardName));
    dirWatchdog.addObserverListener(new FileAlterationListenerAdaptor() {
        @Override
        public void onFileCreate(File file) {
            handleJobConfigCreate(file);
        }

        @Override
        public void onFileChange(File file) {
            handleJobConfigChange(file);
        }

        @Override
        public void onFileDelete(File file) {
            handleJobConfigRemoval(file);
        }
    });

    LOGGER.log(OpLevel.DEBUG, StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME,
            "DirStreamingManager.dir.monitoring.started"), dirPath, fileWildcardName);
}

From source file:com.nextgis.maplib.display.TMSRenderer.java

@Override
public void runDraw(final GISDisplay display) throws NullPointerException {
    final double zoom = display.getZoomLevel();

    GeoEnvelope env = display.getBounds();
    //get tiled for zoom and bounds
    final TMSLayer tmsLayer = (TMSLayer) mLayer;
    final List<TileItem> tiles = tmsLayer.getTielsForBounds(display, env, zoom);

    int threadCount = tmsLayer.getMaxThreadCount();
    if (threadCount > 0)
        mDrawThreadPool = new ThreadPoolExecutor(threadCount, threadCount, KEEP_ALIVE_TIME,
                KEEP_ALIVE_TIME_UNIT, new LinkedBlockingQueue<Runnable>(), new RejectedExecutionHandler() {
                    @Override/*from   www.  ja  va2  s .co m*/
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        try {
                            executor.getQueue().put(r);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            //throw new RuntimeException("Interrupted while submitting task", e);
                        }
                    }
                });

    if (null == mDrawThreadPool) {
        tmsLayer.onDrawFinished(tmsLayer.getId(), 1);
        return;
    }

    if (tiles.size() == 0) {
        tmsLayer.onDrawFinished(tmsLayer.getId(), 1);
        return;
    }

    for (int i = 0; i < tiles.size(); ++i) {
        final TileItem tile = tiles.get(i);
        mDrawThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);

                final Bitmap bmp = tmsLayer.getBitmap(tile);
                if (bmp != null) {
                    display.drawTile(bmp, tile.getPoint(), mRasterPaint);
                }

                float percent = 1;
                synchronized (mLayer) {
                    if (mDrawThreadPool.getTaskCount() > 1)
                        percent = (float) (mDrawThreadPool.getCompletedTaskCount() + 1)
                                / mDrawThreadPool.getTaskCount();
                    tmsLayer.onDrawFinished(tmsLayer.getId(), percent);
                }
                //Log.d(TAG, "percent: " + percent + " complete: " + mDrawThreadPool.getCompletedTaskCount() + " task count: " + mDrawThreadPool.getTaskCount());
            }
        });
    }
}

From source file:com.attribyte.essem.ApplicationCache.java

ApplicationCache(final AsyncClient client, final RequestOptions requestOptions, final ESEndpoint esEndpoint,
        final Logger logger) {

    this.client = client;
    this.requestOptions = requestOptions;
    this.esEndpoint = esEndpoint;
    this.logger = logger;

    final BlockingQueue<Runnable> requestQueue = new ArrayBlockingQueue<>(4096);
    final Gauge<Integer> requestQueueSize = new Gauge<Integer>() {
        @Override//from w  ww . j a v a 2  s. c  om
        public Integer getValue() {
            return requestQueue.size();
        }
    };

    final ThreadPoolExecutor requestExecutor = new ThreadPoolExecutor(2, 8, 5L, TimeUnit.MINUTES, requestQueue,
            new ThreadFactoryBuilder().setNameFormat("application-cache-%d").build());
    requestExecutor.prestartAllCoreThreads();

    final Counter rejectedRequests = new Counter();
    requestExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
            rejectedRequests.inc();
        }
    });
    this.requestExecutor = MoreExecutors
            .listeningDecorator(MoreExecutors.getExitingExecutorService(requestExecutor));

    this.appRequestTimer = new Timer();
    this.appRequestErrors = new Counter();

    this.nameRequestTimer = new Timer();
    this.nameRequestErrors = new Counter();

    this.statsRequestTimer = new Timer();
    this.statsRequestErrors = new Counter();

    Gauge<Integer> appCacheSize = new Gauge<Integer>() {
        @Override
        public Integer getValue() {
            return appCache.size();
        }
    };

    this.metrics = ImmutableMap.<String, com.codahale.metrics.Metric>builder()
            .put("request-queue-size", requestQueueSize).put("rejected-background-requests", rejectedRequests)
            .put("app-requests", appRequestTimer).put("app-request-errors", appRequestErrors)
            .put("name-requests", nameRequestTimer).put("name-request-errors", nameRequestErrors)
            .put("app-cache-size", appCacheSize).put("stats-requests", statsRequestTimer)
            .put("stats-request-errors", statsRequestErrors).build();
}

From source file:com.nextgis.maplibui.service.TileDownloadService.java

private void download(DownloadTask task) {
    MapBase map = MapBase.getInstance();
    if (null == map)
        return;//from   www. j a v a  2  s  .co m

    ILayer layer = map.getLayerByPathName(task.getLayerPathName());
    if (null != layer && layer instanceof RemoteTMSLayer) { // process only tms layers
        final RemoteTMSLayer tmsLayer = (RemoteTMSLayer) layer;
        String notifyTitle = getString(R.string.download_tiles);

        mBuilder.setWhen(System.currentTimeMillis()).setContentTitle(notifyTitle);
        mNotifyManager.notify(TILE_DOWNLOAD_NOTIFICATION_ID, mBuilder.build());

        final List<TileItem> tiles = new LinkedList<>();
        int zoomCount = task.getZoomTo() + 1 - task.getZoomFrom();
        for (int zoom = task.getZoomFrom(); zoom < task.getZoomTo() + 1; zoom++) {
            tiles.addAll(MapUtil.getTileItems(task.getEnvelope(), zoom, tmsLayer.getTMSType()));

            if (mCanceled)
                break;

            mBuilder.setProgress(zoomCount, zoom, false).setContentText(getString(R.string.form_tiles_list));
            mNotifyManager.notify(TILE_DOWNLOAD_NOTIFICATION_ID, mBuilder.build());

            if (tiles.size() > Constants.MAX_TILES_COUNT)
                break;
        }

        int threadCount = DRAWING_SEPARATE_THREADS;
        int coreCount = Runtime.getRuntime().availableProcessors();

        // FIXME more than 1 pool size causing strange behaviour on 6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            coreCount = 1;

        mThreadPool = new ThreadPoolExecutor(coreCount, threadCount, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT,
                new LinkedBlockingQueue<Runnable>(), new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        try {
                            executor.getQueue().put(r);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            //throw new RuntimeException("Interrupted while submitting task", e);
                        }
                    }
                });

        int tilesSize = tiles.size();
        List<Future> futures = new ArrayList<>(tilesSize);

        for (int i = 0; i < tilesSize; ++i) {
            if (mCanceled)
                break;

            final TileItem tile = tiles.get(i);
            futures.add(mThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    android.os.Process.setThreadPriority(Constants.DEFAULT_DRAW_THREAD_PRIORITY);
                    tmsLayer.downloadTile(tile);
                }
            }));
        }

        //in separate thread

        // wait for download ending
        int nStep = futures.size() / Constants.DRAW_NOTIFY_STEP_PERCENT;
        if (nStep == 0)
            nStep = 1;

        for (int i = 0, futuresSize = futures.size(); i < futuresSize; i++) {
            if (mCanceled)
                break;

            try {
                Future future = futures.get(i);
                future.get(); // wait for task ending

                if (i % nStep == 0) {
                    mBuilder.setProgress(futuresSize, i, false)
                            .setContentText(getString(R.string.processing) + " " + tmsLayer.getName());
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(TILE_DOWNLOAD_NOTIFICATION_ID, mBuilder.build());
                }

            } catch (CancellationException | InterruptedException e) {
                //e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.apache.hadoop.hbase.client.crosssite.CrossSiteHTable.java

public static ThreadPoolExecutor getDefaultExecutor(Configuration conf) {
    int maxThreads = conf.getInt("hbase.crosssite.table.threads.max", Integer.MAX_VALUE);
    if (maxThreads <= 0) {
        maxThreads = Integer.MAX_VALUE;
    }/*from  w  w w  .j  a  v a 2s .  c om*/
    final SynchronousQueue<Runnable> blockingQueue = new SynchronousQueue<Runnable>();
    RejectedExecutionHandler rejectHandler = new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                blockingQueue.put(r);
            } catch (InterruptedException e) {
                throw new RejectedExecutionException(e);
            }
        }
    };
    long keepAliveTime = conf.getLong("hbase.table.threads.keepalivetime", 60);
    ThreadPoolExecutor pool = new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS,
            blockingQueue, Threads.newDaemonThreadFactory("crosssite-hbase-table"), rejectHandler);
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
    return pool;
}

From source file:org.apache.hadoop.hbase.client.crosssite.CrossSiteHBaseAdmin.java

public CrossSiteHBaseAdmin(Configuration conf) throws IOException, KeeperException {
    //    super();
    // create the connection to the global zk of the CrossSiteHBaseAdmin
    Configuration crossSiteZKConf = new Configuration(conf);
    ZKUtil.applyClusterKeyToConf(crossSiteZKConf, conf.get(CrossSiteConstants.CROSS_SITE_ZOOKEEPER));
    this.conf = crossSiteZKConf;
    zkw = new ZooKeeperWatcher(this.conf, "connection to global zookeeper", this, false);
    znodes = new CrossSiteZNodes(zkw);
    this.numRetries = this.conf.getInt("hbase.crosssite.client.retries.number", 5);
    this.retryLongerMultiplier = this.conf.getInt("hbase.crosssite.client.retries.longer.multiplier", 2);
    this.pause = this.conf.getLong("hbase.crosssite.client.pause", 1000);

    int poolSize = this.conf.getInt("hbase.crosssite.admin.pool.size", Integer.MAX_VALUE);
    if (poolSize <= 0) {
        poolSize = Integer.MAX_VALUE;
    }//  w w w. j  a  v a 2 s .c  o m
    final SynchronousQueue<Runnable> blockingQueue = new SynchronousQueue<Runnable>();
    RejectedExecutionHandler rejectHandler = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                blockingQueue.put(r);
            } catch (InterruptedException e) {
                throw new RejectedExecutionException(e);
            }
        }
    };
    pool = new ThreadPoolExecutor(1, poolSize, 60, TimeUnit.SECONDS, blockingQueue,
            Threads.newDaemonThreadFactory("crosssite-hbase-admin-"), rejectHandler);
    ((ThreadPoolExecutor) pool).allowCoreThreadTimeOut(true);
}

From source file:com.amazon.mws.shared.MwsConnection.java

/**
 * Get the shared executor service that is used by async calls if no
 * executor is supplied.//w  w w .  j  a v  a2  s. c o m
 * 
 * @return The shared executor service.
 */
private ExecutorService getSharedES() {
    synchronized (this.getClass()) {
        if (sharedES != null) {
            return sharedES;
        }
        sharedES = new ThreadPoolExecutor(maxAsyncThreads / 10, maxAsyncThreads, 60L, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(maxAsyncQueueSize), new ThreadFactory() {
                    private final AtomicInteger threadNumber = new AtomicInteger(1);

                    public Thread newThread(Runnable task) {
                        Thread thread = new Thread(task, "MWSClient-" + threadNumber.getAndIncrement());
                        thread.setDaemon(true);
                        thread.setPriority(Thread.NORM_PRIORITY);
                        return thread;
                    }
                }, new RejectedExecutionHandler() {
                    public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
                        if (!executor.isShutdown()) {
                            log.warn("MWSClient async queue full, running on calling thread.");
                            task.run();
                        } else {
                            throw new RejectedExecutionException();
                        }
                    }
                });
        return sharedES;
    }
}