Search in sources :

Example 21 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project voldemort by voldemort.

the class ConsistencyFixTest method badKeyReaderHelper.

/**
     * 
     * @param orphan true for testing orphan, false for testing normal...
     */
public void badKeyReaderHelper(boolean orphan) {
    String tmpDir = TestUtils.createTempDir().getAbsolutePath();
    String fileName = tmpDir + "BadKeyFile";
    if (orphan) {
        badKeyReaderWriteOrphanKeys(fileName, true);
    } else {
        badKeyReaderWriteBadKeys(fileName, true);
    }
    // Get cluster bootstrap url
    String url = setUpCluster();
    // Construct ConsistencyFix with parseOnly true
    ConsistencyFix consistencyFix = new ConsistencyFix(url, STORE_NAME, 100, 100, false, true);
    // Do set up for BadKeyReader akin to consistencyFix.execute...
    int parallelism = 1;
    BlockingQueue<Runnable> blockingQ = new ArrayBlockingQueue<Runnable>(parallelism);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    ExecutorService consistencyFixWorkers = new ThreadPoolExecutor(parallelism, parallelism, 0L, TimeUnit.MILLISECONDS, blockingQ, rejectedExecutionHandler);
    BlockingQueue<BadKeyStatus> badKeyQOut = new ArrayBlockingQueue<BadKeyStatus>(10000);
    ExecutorService badKeyReaderService = Executors.newSingleThreadExecutor();
    CountDownLatch allBadKeysReadLatch = new CountDownLatch(1);
    // Submit file of bad keys to (appropriate) BadKeyReader
    BadKeyReader bkr = null;
    if (orphan) {
        bkr = new BadKeyOrphanReader(allBadKeysReadLatch, fileName, consistencyFix, consistencyFixWorkers, badKeyQOut);
    } else {
        bkr = new BadKeyReader(allBadKeysReadLatch, fileName, consistencyFix, consistencyFixWorkers, badKeyQOut);
    }
    badKeyReaderService.submit(bkr);
    // Wait for file to be processed.
    try {
        allBadKeysReadLatch.await();
        badKeyReaderService.shutdown();
        consistencyFixWorkers.shutdown();
    } catch (InterruptedException e) {
        e.printStackTrace();
        fail("Unexpected exception");
    }
    consistencyFix.close();
    // Make sure everything worked as expected.
    assertFalse(bkr.hasException());
    assertEquals(0, badKeyQOut.size());
}
Also used : BadKeyStatus(voldemort.utils.ConsistencyFix.BadKeyStatus) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) BadKeyOrphanReader(voldemort.utils.ConsistencyFix.BadKeyOrphanReader) CountDownLatch(java.util.concurrent.CountDownLatch) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ExecutorService(java.util.concurrent.ExecutorService) BadKeyReader(voldemort.utils.ConsistencyFix.BadKeyReader) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 22 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project wildfly by wildfly.

the class ThreadPoolFactoryBuilder method build.

@Override
public ServiceBuilder<ThreadPoolFactory> build(ServiceTarget target) {
    int queueLength = this.getQueueLength();
    BlockingQueue<Runnable> queue = (queueLength > 0) ? new ArrayBlockingQueue<>(queueLength) : new SynchronousQueue<>();
    ClassLoader loader = JChannelFactory.class.getClassLoader();
    ThreadFactory threadFactory = new ClassLoaderThreadFactory(new DefaultThreadFactory(this.getThreadGroupPrefix(), false, true), loader);
    RejectedExecutionHandler handler = new ShutdownRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
    ThreadPoolFactory factory = () -> new ThreadPoolExecutor(this.getMinThreads(), this.getMaxThreads(), this.getKeepAliveTime(), TimeUnit.MILLISECONDS, queue, threadFactory, handler);
    return target.addService(this.getServiceName(), new ValueService<>(new ImmediateValue<>(factory)));
}
Also used : ClassLoaderThreadFactory(org.jboss.as.clustering.jgroups.ClassLoaderThreadFactory) DefaultThreadFactory(org.jgroups.util.DefaultThreadFactory) ThreadFactory(org.jgroups.util.ThreadFactory) ShutdownRejectedExecutionHandler(org.jgroups.util.ShutdownRejectedExecutionHandler) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ImmediateValue(org.jboss.msc.value.ImmediateValue) DefaultThreadFactory(org.jgroups.util.DefaultThreadFactory) ClassLoaderThreadFactory(org.jboss.as.clustering.jgroups.ClassLoaderThreadFactory) ShutdownRejectedExecutionHandler(org.jgroups.util.ShutdownRejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 23 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project geode by apache.

the class TCPConduit method startAcceptor.

/**
   * binds the server socket and gets threads going
   */
private void startAcceptor() throws ConnectionException {
    int localPort;
    int p = this.port;
    InetAddress ba = this.address;
    {
        ThreadPoolExecutor tmp_hsPool = null;
        String gName = "P2P-Handshaker " + ba + ":" + p;
        final ThreadGroup socketThreadGroup = LoggingThreadGroup.createThreadGroup(gName, logger);
        ThreadFactory socketThreadFactory = new ThreadFactory() {

            int connNum = -1;

            public Thread newThread(Runnable command) {
                int tnum;
                synchronized (this) {
                    tnum = ++connNum;
                }
                String tName = socketThreadGroup.getName() + " Thread " + tnum;
                return new Thread(socketThreadGroup, command, tName);
            }
        };
        try {
            final BlockingQueue bq = new SynchronousQueue();
            final RejectedExecutionHandler reh = new RejectedExecutionHandler() {

                public void rejectedExecution(Runnable r, ThreadPoolExecutor pool) {
                    try {
                        bq.put(r);
                    } catch (InterruptedException ex) {
                        // preserve the state
                        Thread.currentThread().interrupt();
                        throw new RejectedExecutionException(LocalizedStrings.TCPConduit_INTERRUPTED.toLocalizedString(), ex);
                    }
                }
            };
            tmp_hsPool = new ThreadPoolExecutor(1, HANDSHAKE_POOL_SIZE, HANDSHAKE_POOL_KEEP_ALIVE_TIME, TimeUnit.SECONDS, bq, socketThreadFactory, reh);
        } catch (IllegalArgumentException poolInitException) {
            throw new ConnectionException(LocalizedStrings.TCPConduit_WHILE_CREATING_HANDSHAKE_POOL.toLocalizedString(), poolInitException);
        }
        this.hsPool = tmp_hsPool;
    }
    createServerSocket();
    try {
        localPort = socket.getLocalPort();
        id = new InetSocketAddress(socket.getInetAddress(), localPort);
        stopped = false;
        ThreadGroup group = LoggingThreadGroup.createThreadGroup("P2P Listener Threads", logger);
        thread = new Thread(group, this, "P2P Listener Thread " + id);
        thread.setDaemon(true);
        try {
            thread.setPriority(thread.getThreadGroup().getMaxPriority());
        } catch (Exception e) {
            logger.info(LocalizedMessage.create(LocalizedStrings.TCPConduit_UNABLE_TO_SET_LISTENER_PRIORITY__0, e.getMessage()));
        }
        if (!Boolean.getBoolean("p2p.test.inhibitAcceptor")) {
            thread.start();
        } else {
            logger.fatal(LocalizedMessage.create(LocalizedStrings.TCPConduit_INHIBITACCEPTOR));
            socket.close();
            this.hsPool.shutdownNow();
        }
    } catch (IOException io) {
        String s = "While creating ServerSocket on port " + p;
        throw new ConnectionException(s, io);
    }
    this.port = localPort;
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) ThreadFactory(java.util.concurrent.ThreadFactory) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CancelException(org.apache.geode.CancelException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SSLException(javax.net.ssl.SSLException) SynchronousQueue(java.util.concurrent.SynchronousQueue) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) InetAddress(java.net.InetAddress)

Example 24 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project tomee by apache.

the class ExecutorBuilder method build.

public ThreadPoolExecutor build(final Options options) {
    int corePoolSize = options.get(prefix + ".CorePoolSize", size);
    if (corePoolSize < 1) {
        corePoolSize = 1;
    }
    // Default setting is for a fixed pool size, MaximumPoolSize==CorePoolSize
    int maximumPoolSize = Math.max(options.get(prefix + ".MaximumPoolSize", corePoolSize), corePoolSize);
    if (maximumPoolSize < corePoolSize) {
        maximumPoolSize = corePoolSize;
    }
    // Default QueueSize is bounded using the corePoolSize, else bounded pools will never grow
    final int qsize = options.get(prefix + ".QueueSize", corePoolSize);
    // Keep Threads inactive threads alive for 60 seconds by default
    final Duration keepAliveTime = options.get(prefix + ".KeepAliveTime", new Duration(60, TimeUnit.SECONDS));
    // All threads can be timed out by default
    final boolean allowCoreThreadTimeout = options.get(prefix + ".AllowCoreThreadTimeOut", true);
    // If the user explicitly set the QueueSize to 0, we default QueueType to SYNCHRONOUS
    final QueueType defaultQueueType = qsize < 1 ? QueueType.SYNCHRONOUS : QueueType.LINKED;
    final BlockingQueue<Runnable> queue = options.get(prefix + ".QueueType", defaultQueueType).create(options, prefix, qsize);
    ThreadFactory factory = this.threadFactory;
    if (factory == null) {
        factory = new DaemonThreadFactory(prefix);
    }
    RejectedExecutionHandler handler = this.rejectedExecutionHandler;
    if (handler == null) {
        final String rejectedExecutionHandlerClass = options.get(prefix + ".RejectedExecutionHandlerClass", (String) null);
        if (rejectedExecutionHandlerClass == null) {
            final Duration duration = options.get(prefix + ".OfferTimeout", new Duration(30, TimeUnit.SECONDS));
            handler = new OfferRejectedExecutionHandler(duration);
        } else {
            try {
                handler = RejectedExecutionHandler.class.cast(Thread.currentThread().getContextClassLoader().loadClass(rejectedExecutionHandlerClass).newInstance());
            } catch (final Exception e) {
                throw new OpenEJBRuntimeException(e);
            }
        }
    }
    final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime.getTime(), keepAliveTime.getUnit() != null ? keepAliveTime.getUnit() : TimeUnit.SECONDS, queue, factory, handler);
    threadPoolExecutor.allowCoreThreadTimeOut(allowCoreThreadTimeout);
    return threadPoolExecutor;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) OfferRejectedExecutionHandler(org.apache.openejb.util.executor.OfferRejectedExecutionHandler) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) OfferRejectedExecutionHandler(org.apache.openejb.util.executor.OfferRejectedExecutionHandler)

Example 25 with RejectedExecutionHandler

use of java.util.concurrent.RejectedExecutionHandler in project tomee by apache.

the class JNDIContext method newExecutor.

public static ThreadPoolExecutor newExecutor(final int threads, final BlockingQueue<Runnable> blockingQueue) {
    /**
         This thread pool starts with 3 core threads and can grow to the limit defined by 'threads'.
         If a pool thread is idle for more than 1 minute it will be discarded, unless the core size is reached.
         It can accept upto the number of processes defined by 'queue'.
         If the queue is full then an attempt is made to add the process to the queue for 10 seconds.
         Failure to add to the queue in this time will either result in a logged rejection, or if 'block'
         is true then a final attempt is made to run the process in the current thread (the service thread).
         */
    final ThreadPoolExecutor executorService = new ThreadPoolExecutor(3, (threads < 3 ? 3 : threads), 1, TimeUnit.MINUTES, blockingQueue == null ? new LinkedBlockingDeque<Runnable>(Integer.parseInt(getProperty(null, POOL_QUEUE_SIZE, "2"))) : blockingQueue);
    executorService.setThreadFactory(new ThreadFactory() {

        private final AtomicInteger i = new AtomicInteger(0);

        @Override
        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r, "OpenEJB.Client." + i.incrementAndGet());
            t.setDaemon(true);
            t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

                @Override
                public void uncaughtException(final Thread t, final Throwable e) {
                    Logger.getLogger(EJBObjectHandler.class.getName()).log(Level.SEVERE, "Uncaught error in: " + t.getName(), e);
                }
            });
            return t;
        }
    });
    executorService.setRejectedExecutionHandler(new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(final Runnable r, final ThreadPoolExecutor tpe) {
            if (null == r || null == tpe || tpe.isShutdown() || tpe.isTerminated() || tpe.isTerminating()) {
                return;
            }
            final Logger log = Logger.getLogger(EJBObjectHandler.class.getName());
            if (log.isLoggable(Level.WARNING)) {
                log.log(Level.WARNING, "EJBObjectHandler ExecutorService at capicity for process: " + r);
            }
            boolean offer = false;
            try {
                offer = tpe.getQueue().offer(r, 10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
            //Ignore
            }
            if (!offer) {
                log.log(Level.SEVERE, "EJBObjectHandler ExecutorService failed to run asynchronous process: " + r);
            }
        }
    });
    return executorService;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Logger(java.util.logging.Logger)

Aggregations

RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)27 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)23 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)6 ThreadFactory (java.util.concurrent.ThreadFactory)6 ExecutorService (java.util.concurrent.ExecutorService)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)3 Metacard (ddf.catalog.data.Metacard)2 SourceResponse (ddf.catalog.operation.SourceResponse)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 XRejectedExecutionHandler (org.elasticsearch.common.util.concurrent.XRejectedExecutionHandler)2 Test (org.junit.Test)2 NamedThreadFactory (com.alibaba.otter.shared.common.utils.thread.NamedThreadFactory)1