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();
}
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;
}
Aggregations