Search in sources :

Example 6 with AbortPolicy

use of java.util.concurrent.ThreadPoolExecutor.AbortPolicy in project ACS by ACS-Community.

the class CorbaNotifySupplierImpl method sendEvents.

@Override
public int sendEvents(NcEventSpec[] ncEventSpecs, int eventPeriodMillis, int numberOfEvents) throws CouldntPerformActionEx {
    if (cancel) {
        AcsJCouldntPerformActionEx ex = new AcsJCouldntPerformActionEx("Method sendEvents cannot be called after interrupt / ncDisconnect.");
        throw ex.toCouldntPerformActionEx();
    }
    StopWatch sw = null;
    ScheduledThreadPoolExecutor runner = null;
    // Set up the runnables for all NCs
    try {
        for (NcEventSpec ncEventSpec : ncEventSpecs) {
            PublishEventRunnable runnable = new PublishEventRunnable(ncEventSpec, this.subsOrPubs.get(ncEventSpec.ncName), numberOfEvents);
            runnables.add(runnable);
        }
        // multithreaded executor
        runner = new ScheduledThreadPoolExecutor(// thread per NC
        ncEventSpecs.length, m_containerServices.getThreadFactory(), //RejectedExecutionException
        new AbortPolicy());
        sw = new StopWatch();
        // run the NC suppliers
        for (PublishEventRunnable runnable : runnables) {
            ScheduledFuture<?> future = null;
            if (eventPeriodMillis > 0) {
                // publish at fixed rate
                future = runner.scheduleAtFixedRate(runnable, 0, eventPeriodMillis, TimeUnit.MILLISECONDS);
            } else {
                // run continuously 
                // delay must be > 0, otherwise IllegalArgumentException
                future = runner.scheduleWithFixedDelay(runnable, 0, 1, TimeUnit.NANOSECONDS);
            }
            runnable.setScheduledFuture(future);
        }
    } catch (Exception ex) {
        m_logger.log(AcsLogLevel.SEVERE, "sendEvents call failed", ex);
        throw new AcsJCouldntPerformActionEx(ex).toCouldntPerformActionEx();
    }
    String msgBase = "Started publishing events on " + ncEventSpecs.length + " NC(s), sending events " + (eventPeriodMillis > 0 ? "every " + eventPeriodMillis + " ms. " : "as fast as possible. ");
    if (numberOfEvents > 0) {
        m_logger.info(msgBase + "Will now wait until " + numberOfEvents + " have been published on every NC...");
        // block until all events are sent
        runner.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
        runner.setExecuteExistingDelayedTasksAfterShutdownPolicy(true);
        runner.shutdown();
        try {
            // 10 min timeout, just to clean up resources eventually.
            // TODO: Offer a workaround for special long-running tests
            boolean cleanTermination = runner.awaitTermination(10, TimeUnit.MINUTES);
            if (!cleanTermination) {
                m_logger.warning("Unforeseen termination of event suppliers after 10 min (timeout).");
                cancel = true;
            }
        } catch (InterruptedException ex) {
            cancel = true;
        }
    } else {
        runnersToInterrupt.add(runner);
        m_logger.info(msgBase + "Will return and asynchronously continue publishing events, until interrupt() gets called.");
    }
    if (cancel) {
        throw new AcsJCouldntPerformActionEx("Event sending was interrupted or failed otherwise.").toCouldntPerformActionEx();
    }
    return (int) sw.getLapTimeMillis();
}
Also used : AbortPolicy(java.util.concurrent.ThreadPoolExecutor.AbortPolicy) AcsJCouldntPerformActionEx(alma.ACSErrTypeCommon.wrappers.AcsJCouldntPerformActionEx) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) NcEventSpec(alma.benchmark.NcEventSpec) AcsJException(alma.acs.exceptions.AcsJException) StopWatch(alma.acs.util.StopWatch)

Example 7 with AbortPolicy

use of java.util.concurrent.ThreadPoolExecutor.AbortPolicy in project mule by mulesoft.

the class SimpleUnitTestSupportSchedulerService method customScheduler.

@Override
public Scheduler customScheduler(SchedulerConfig config, int queueSize) {
    final SimpleUnitTestSupportScheduler customScheduler = new SimpleUnitTestSupportCustomScheduler(config.getMaxConcurrentTasks(), buildThreadFactory(config), new AbortPolicy());
    customSchedulers.add(customScheduler);
    final SimpleUnitTestSupportLifecycleSchedulerDecorator decorator = decorateScheduler(customScheduler);
    decorators.add(decorator);
    return decorator;
}
Also used : AbortPolicy(java.util.concurrent.ThreadPoolExecutor.AbortPolicy)

Aggregations

AbortPolicy (java.util.concurrent.ThreadPoolExecutor.AbortPolicy)7 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 Disabled (org.junit.jupiter.api.Disabled)2 Test (org.junit.jupiter.api.Test)2 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)2 Schedulers.fromExecutorService (reactor.core.scheduler.Schedulers.fromExecutorService)2 AcsJCouldntPerformActionEx (alma.ACSErrTypeCommon.wrappers.AcsJCouldntPerformActionEx)1 AcsJException (alma.acs.exceptions.AcsJException)1 StopWatch (alma.acs.util.StopWatch)1 NcEventSpec (alma.benchmark.NcEventSpec)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 NotNull (org.jetbrains.annotations.NotNull)1 CompositeExecutor (org.springframework.integration.util.CompositeExecutor)1