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.amazonservices.mws.client.MwsConnection.java

/**
 * Get the shared executor service that is used by async calls if no
 * executor is supplied.//from  w  ww  .  j a v  a 2s .  com
 * 
 * @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);

                    //@Override
                    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() {
                    //@Override
                    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;
    }
}

From source file:com.neusou.bioroid.restful.RestfulClient.java

/**
 * Creates a <b>RestfulClient</b><br/><br/>
 * The intent actions will generated with the following rule: <br/><br/>
 * &lt;the package name of the supplied context&gt;.&lt;the supplied name&gt;.restful.&lt;the action name&gt;
 * //ww  w.j a va2 s. c  o m
 * <br/><br/>Example: with context has package name com.neusou.facegraph and FB as the restful client name:<br/><br/>
 * com.neusou.facegraph.FB.restful.PROCESS_RESPONSE<br/>
 * com.neusou.facegraph.FB.restful.EXECUTE_REQUEST<br/>
 * com.neusou.facegraph.FB.restful.EXECUTE_REQUEST
 * <br/>
 * @param context context
 * @param name the unique name of the restful client
 */
public RestfulClient(Context context, String name) {
    if (name == null) {
        throw new IllegalArgumentException("name can not be null");
    }

    if (context == null) {
        Logger.l(Logger.WARN, LOG_TAG, "Required Context argument is null.");
    }

    mContext = context;
    mName = name;

    HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
    httpParams.setBooleanParameter("http.protocol.expect-continue", false);

    SchemeRegistry scheme = new SchemeRegistry();
    scheme.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
    sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    scheme.register(new Scheme("https", sslSocketFactory, 443));

    ThreadSafeClientConnManager tscm = new ThreadSafeClientConnManager(httpParams, scheme);

    httpClient = new DefaultHttpClient(tscm, httpParams);
    httpClient.setReuseStrategy(new ConnectionReuseStrategy() {
        @Override
        public boolean keepAlive(HttpResponse response, HttpContext context) {
            return false;
        }
    });

    mExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            //   Logger.l(Logger.DEBUG, LOG_TAG, "rejectedExecution. #activethread:"+executor.getActiveCount()+", queue.size:"+executor.getQueue().size());            
        }
    });

    if (context != null) {
        setContext(context);
    }
}

From source file:org.openmrs.module.openconceptlab.updater.Updater.java

private ThreadPoolExecutor newRunner() {
    return new ThreadPoolExecutor(0, THREAD_POOL_SIZE, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(THREAD_POOL_SIZE / 2), new RejectedExecutionHandler() {

                @Override//w w w .  jav  a2  s.c o  m
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        executor.getQueue().put(r);
                    } catch (InterruptedException e) {
                        throw new RejectedExecutionException("Work discarded", e);
                    }
                }
            });
}

From source file:com.indeed.lsmtree.recordcache.PersistentRecordCache.java

/**
 * Performs lookup for multiple keys and returns a streaming iterator to results.
 * Each element in the iterator is one of
 *  (1) an exception associated with a single lookup
 *  (2) a key value tuple//from w  w  w .  j ava  2  s.  co  m
 *
 * @param keys      lookup keys
 * @param progress  (optional) an AtomicInteger for tracking progress
 * @param skipped   (optional) an AtomicInteger for tracking missing keys
 * @return          iterator of lookup results
 */
public Iterator<Either<Exception, P2<K, V>>> getStreaming(final @Nonnull Iterator<K> keys,
        final @Nullable AtomicInteger progress, final @Nullable AtomicInteger skipped) {
    log.info("starting store lookups");
    LongArrayList addressList = new LongArrayList();
    int notFound = 0;
    while (keys.hasNext()) {
        final K key = keys.next();
        final Long address;
        try {
            address = index.get(key);
        } catch (IOException e) {
            log.error("error", e);
            return Iterators.singletonIterator(Left.<Exception, P2<K, V>>of(new IndexReadException(e)));
        }
        if (address != null) {
            addressList.add(address);
        } else {
            notFound++;
        }
    }
    if (progress != null)
        progress.addAndGet(notFound);
    if (skipped != null)
        skipped.addAndGet(notFound);
    log.info("store lookups complete, sorting addresses");

    final long[] addresses = addressList.elements();
    Arrays.sort(addresses, 0, addressList.size());

    log.info("initializing store lookup iterator");
    final BlockingQueue<Runnable> taskQueue = new ArrayBlockingQueue<Runnable>(100);
    final Iterator<List<Long>> iterable = Iterators.partition(addressList.iterator(), 1000);
    final ExecutorService primerThreads = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, taskQueue,
            new NamedThreadFactory("store priming thread", true, log), new RejectedExecutionHandler() {
                @Override
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    try {
                        taskQueue.put(r);
                    } catch (InterruptedException e) {
                        log.error("error", e);
                        throw new RuntimeException(e);
                    }
                }
            });
    final BlockingQueue<List<Either<Exception, P2<K, V>>>> completionQueue = new ArrayBlockingQueue<List<Either<Exception, P2<K, V>>>>(
            10);
    final AtomicLong runningTasks = new AtomicLong(0);
    final AtomicBoolean taskSubmitterRunning = new AtomicBoolean(true);

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (iterable.hasNext()) {
                runningTasks.incrementAndGet();
                final List<Long> addressesSublist = iterable.next();
                primerThreads.submit(new FutureTask<List<Either<Exception, P2<K, V>>>>(
                        new RecordLookupTask(addressesSublist)) {
                    @Override
                    protected void done() {
                        try {
                            final List<Either<Exception, P2<K, V>>> results = get();
                            if (progress != null) {
                                progress.addAndGet(results.size());
                            }
                            completionQueue.put(results);
                        } catch (InterruptedException e) {
                            log.error("error", e);
                            throw new RuntimeException(e);
                        } catch (ExecutionException e) {
                            log.error("error", e);
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
            taskSubmitterRunning.set(false);
        }
    }, "RecordLookupTaskSubmitterThread").start();

    return new Iterator<Either<Exception, P2<K, V>>>() {

        Iterator<Either<Exception, P2<K, V>>> currentIterator;

        @Override
        public boolean hasNext() {
            if (currentIterator != null && currentIterator.hasNext())
                return true;
            while (taskSubmitterRunning.get() || runningTasks.get() > 0) {
                try {
                    final List<Either<Exception, P2<K, V>>> list = completionQueue.poll(1, TimeUnit.SECONDS);
                    if (list != null) {
                        log.debug("remaining: " + runningTasks.decrementAndGet());
                        currentIterator = list.iterator();
                        if (currentIterator.hasNext())
                            return true;
                    }
                } catch (InterruptedException e) {
                    log.error("error", e);
                    throw new RuntimeException(e);
                }
            }
            primerThreads.shutdown();
            return false;
        }

        @Override
        public Either<Exception, P2<K, V>> next() {
            return currentIterator.next();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:API.amazon.mws.feeds.service.MarketplaceWebServiceClient.java

/**
 * Constructs MarketplaceWebServiceClient with AWS Access Key ID, AWS Secret Key
 * and MarketplaceWebServiceConfig. Use MarketplaceWebServiceConfig to pass additional
 * configuration that affects how service is being called.
 *
 * @param awsAccessKeyId//from  w  w w  .  ja  va  2 s .co  m
 *          AWS Access Key ID
 * @param awsSecretAccessKey
 *          AWS Secret Access Key
 * @param config
 *          Additional configuration options
 */
@SuppressWarnings("serial")
public MarketplaceWebServiceClient(String awsAccessKeyId, String awsSecretAccessKey, String applicationName,
        String applicationVersion, MarketplaceWebServiceConfig config) {
    this.awsAccessKeyId = awsAccessKeyId;
    this.awsSecretAccessKey = awsSecretAccessKey;
    this.config = config;
    this.httpClient = configureHttpClient(applicationName, applicationVersion);
    this.asyncExecutor = new ThreadPoolExecutor(config.getMaxAsyncThreads(), config.getMaxAsyncThreads(), 60L,
            TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(config.getMaxAsyncQueueSize()) {

                @Override
                public boolean offer(Runnable task) {
                    log.debug("Maximum number of concurrent threads reached, queuing task...");
                    return super.offer(task);
                }
            }, new ThreadFactory() {

                private final AtomicInteger threadNumber = new AtomicInteger(1);

                public Thread newThread(Runnable task) {
                    Thread thread = new Thread(task,
                            "MarketplaceWebServiceClient-Thread-" + threadNumber.getAndIncrement());
                    thread.setDaemon(true);
                    if (thread.getPriority() != Thread.NORM_PRIORITY) {
                        thread.setPriority(Thread.NORM_PRIORITY);
                    }
                    log.debug("ThreadFactory created new thread: " + thread.getName());
                    return thread;
                }
            }, new RejectedExecutionHandler() {

                public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
                    log.debug("Maximum number of concurrent threads reached, and queue is full. "
                            + "Running task in the calling thread..." + Thread.currentThread().getName());
                    if (!executor.isShutdown()) {
                        task.run();
                    }
                }
            });
}

From source file:com.jkoolcloud.tnt4j.streams.inputs.TNTInputStream.java

/**
 * Creates thread pool executor service for a given number of threads with bounded tasks queue - queue size is
 * 2x{@code threadsQty}. When queue size is reached, new tasks are offered to queue using defined offer timeout. If
 * task can't be put into queue over this time, task is skipped with making warning log entry. Thus memory use does
 * not grow drastically if consumers can't keep up the pace of producers filling in the queue, making producers
 * synchronize with consumers.//ww w.ja v  a  2s  .c om
 *
 * @param threadsQty
 *            the number of threads in the pool
 * @param offerTimeout
 *            how long to wait before giving up on offering task to queue
 *
 * @return the newly created thread pool executor
 *
 * @see ThreadPoolExecutor#ThreadPoolExecutor(int, int, long, TimeUnit, BlockingQueue, ThreadFactory)
 */
private ExecutorService getBoundedExecutorService(int threadsQty, final int offerTimeout) {
    StreamsThreadFactory stf = new StreamsThreadFactory("StreamBoundedExecutorThread-"); // NON-NLS
    stf.addThreadFactoryListener(new StreamsThreadFactoryListener());

    ThreadPoolExecutor tpe = new ThreadPoolExecutor(threadsQty, threadsQty, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(threadsQty * 2), stf);

    tpe.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        @Override
        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);
                    notifyStreamTaskRejected(r);
                }
            } catch (InterruptedException exc) {
                halt(true);
            }
        }
    });

    return tpe;
}

From source file:com.bosscs.spark.commons.utils.Utils.java

/**
 * Returns an instance of ThreadPoolExecutor using an bounded queue and blocking when the worker queue is full.
 * @param nThreads thread pool size//from   w  ww.  java 2 s  .  co  m
 * @param queueSize workers queue size
 * @return thread pool executor
 */
public static ExecutorService newBlockingFixedThreadPoolExecutor(int nThreads, int queueSize) {
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(queueSize);
    RejectedExecutionHandler blockingRejectedExecutionHandler = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(task);
            } catch (InterruptedException e) {
            }
        }

    };

    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, blockingQueue,
            blockingRejectedExecutionHandler);
}

From source file:com.sentaroh.android.TaskAutomation.TaskManager.java

static final public void buildTaskExecThreadPool(final EnvironmentParms envParms,
        final TaskManagerParms taskMgrParms, final CommonUtilities util) {
    if (taskMgrParms.taskExecutorThreadPool != null)
        removeTaskExecThreadPool(envParms, taskMgrParms, util);
    SynchronousQueue<Runnable> slq = new SynchronousQueue<Runnable>();
    RejectedExecutionHandler rh = new RejectedExecutionHandler() {
        @Override//ww  w.j av  a 2  s. c o  m
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            util.addDebugMsg(1, "W", "Task executor reject handler entered.");
            startTaskOutsideThreadPool(taskMgrParms, envParms, util, (TaskExecutor) r);
        }
    };
    taskMgrParms.taskExecutorThreadPool = new ThreadPoolExecutor(envParms.settingTaskExecThreadPoolCount + 2,
            envParms.settingTaskExecThreadPoolCount + 2, 10, TimeUnit.SECONDS, slq, rh);
    for (int i = 0; i < envParms.settingTaskExecThreadPoolCount + 2; i++) {
        final int num = i + 1;
        Runnable rt = new Runnable() {
            @Override
            public void run() {
                Thread.currentThread().setPriority(THREAD_PRIORITY_TASK_EXEC);
                Thread.currentThread().setName("TaskExec-" + num);
            }
        };
        taskMgrParms.taskExecutorThreadPool.execute(rt);
    }
    taskMgrParms.taskExecutorThreadPool.prestartAllCoreThreads();
    util.addDebugMsg(1, "I", "Task executor thread pool was created.");
}

From source file:com.dumontierlab.pdb2rdf.Pdb2Rdf.java

private static ExecutorService getThreadPool(CommandLine cmd) {
    // twice the number of PU
    final Object monitor = new Object();
    int numberOfThreads = getNumberOfThreads(cmd);
    LOG.info("Using " + numberOfThreads + " threads.");
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, 10,
            TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(1), new RejectedExecutionHandler() {
                @Override/*from ww  w  .  j  a v a2s .  com*/
                public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                    synchronized (monitor) {
                        try {
                            monitor.wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    executor.execute(r);
                }
            }) {
        @Override
        protected void afterExecute(Runnable r, Throwable t) {
            synchronized (monitor) {
                monitor.notify();
            }
            super.afterExecute(r, t);
        }
    };

    return threadPool;
}