List of usage examples for java.util.concurrent ScheduledExecutorService execute
void execute(Runnable command);
From source file:com.google.pubsub.flic.controllers.GCEController.java
/** * Instantiates the load test on Google Compute Engine. *//*from ww w. jav a 2 s.c o m*/ private GCEController(String projectName, Map<String, Map<ClientParams, Integer>> types, ScheduledExecutorService executor, Storage storage, Compute compute, Pubsub pubsub) throws Throwable { super(executor); this.projectName = projectName; this.types = types; this.storage = storage; this.compute = compute; // For each unique type of CPS Publisher, create a Topic if it does not already exist, and then // delete and recreate any subscriptions attached to it so that we do not have backlog from // previous runs. List<SettableFuture<Void>> pubsubFutures = new ArrayList<>(); types.values().forEach(paramsMap -> { paramsMap.keySet().stream().map(p -> p.getClientType()).distinct().filter(ClientType::isCpsPublisher) .forEach(clientType -> { SettableFuture<Void> pubsubFuture = SettableFuture.create(); pubsubFutures.add(pubsubFuture); executor.execute(() -> { String topic = Client.TOPIC_PREFIX + Client.getTopicSuffix(clientType); try { pubsub.projects().topics() .create("projects/" + projectName + "/topics/" + topic, new Topic()) .execute(); } catch (GoogleJsonResponseException e) { if (e.getStatusCode() != ALREADY_EXISTS) { pubsubFuture.setException(e); return; } log.info("Topic already exists, reusing."); } catch (IOException e) { pubsubFuture.setException(e); return; } // Recreate each subscription attached to the topic. paramsMap.keySet().stream() .filter(p -> p.getClientType() == clientType.getSubscriberType()) .map(p -> p.subscription).forEach(subscription -> { try { pubsub.projects().subscriptions().delete( "projects/" + projectName + "/subscriptions/" + subscription) .execute(); } catch (IOException e) { log.debug( "Error deleting subscription, assuming it has not yet been created.", e); } try { pubsub.projects().subscriptions().create( "projects/" + projectName + "/subscriptions/" + subscription, new Subscription() .setTopic( "projects/" + projectName + "/topics/" + topic) .setAckDeadlineSeconds(10)) .execute(); } catch (IOException e) { pubsubFuture.setException(e); } }); pubsubFuture.set(null); }); }); }); try { createStorageBucket(); createFirewall(); List<SettableFuture<Void>> filesRemaining = new ArrayList<>(); Files.walk(Paths.get(resourceDirectory)).filter(Files::isRegularFile).forEach(filePath -> { SettableFuture<Void> fileRemaining = SettableFuture.create(); filesRemaining.add(fileRemaining); executor.execute(() -> { try { uploadFile(filePath); fileRemaining.set(null); } catch (Exception e) { fileRemaining.setException(e); } }); }); List<SettableFuture<Void>> createGroupFutures = new ArrayList<>(); types.forEach((zone, paramsMap) -> paramsMap.forEach((param, n) -> { SettableFuture<Void> createGroupFuture = SettableFuture.create(); createGroupFutures.add(createGroupFuture); executor.execute(() -> { try { createManagedInstanceGroup(zone, param.getClientType()); createGroupFuture.set(null); } catch (Exception e) { createGroupFuture.setException(e); } }); })); // Wait for files and instance groups to be created. Futures.allAsList(pubsubFutures).get(); log.info("Pub/Sub actions completed."); Futures.allAsList(filesRemaining).get(); log.info("File uploads completed."); Futures.allAsList(createGroupFutures).get(); log.info("Instance group creation completed."); // Everything is set up, let's start our instances log.info("Starting instances."); List<SettableFuture<Void>> resizingFutures = new ArrayList<>(); types.forEach((zone, paramsMap) -> paramsMap.forEach((type, n) -> { SettableFuture<Void> resizingFuture = SettableFuture.create(); resizingFutures.add(resizingFuture); executor.execute(() -> { try { startInstances(zone, type.getClientType(), n); resizingFuture.set(null); } catch (Exception e) { resizingFuture.setException(e); } }); })); Futures.allAsList(resizingFutures).get(); // We wait for all instances to finish starting, and get the external network address of each // newly created instance. List<SettableFuture<Void>> startFutures = new ArrayList<>(); for (String zone : types.keySet()) { Map<ClientParams, Integer> paramsMap = types.get(zone); for (ClientParams type : paramsMap.keySet()) { SettableFuture<Void> startFuture = SettableFuture.create(); startFutures.add(startFuture); executor.execute(() -> { int numErrors = 0; while (true) { try { addInstanceGroupInfo(zone, type); startFuture.set(null); return; } catch (IOException e) { numErrors++; if (numErrors > 3) { startFuture.setException(new Exception("Failed to get instance information.")); return; } log.error("Transient error getting status for instance group, continuing", e); } } }); } } Futures.allAsList(startFutures).get(); log.info("Successfully started all instances."); } catch (ExecutionException e) { shutdown(e.getCause()); throw e.getCause(); } catch (Exception e) { shutdown(e); throw e; } }
From source file:ome.formats.importer.gui.ImportHandler.java
/** * @param ex -scheduled executor service for background processing * @param viewer - parent/*from www . jav a 2 s .c o m*/ * @param qTable - fileQueueTable to receive notifications * @param config - passed in config * @param library - passed in library * @param containers - importContainers */ public ImportHandler(final ScheduledExecutorService ex, GuiImporter viewer, FileQueueTable qTable, ImportConfig config, ImportLibrary library, ImportContainer[] containers) { this.config = config; this.library = library; this.importContainer = containers; if (viewer.getHistoryTable() != null) { this.db = viewer.getHistoryTable().db; } else { this.db = null; } if (runState == true) { log.error("ImportHandler running twice"); throw new RuntimeException("ImportHandler running twice"); } runState = true; try { this.viewer = viewer; this.qTable = qTable; library.addObserver(qTable); library.addObserver(viewer); library.addObserver(viewer.getErrorHandler()); library.addObserver(new IObserver() { public void update(IObservable importLibrary, ImportEvent event) { if (ex.isShutdown()) { log.info("Cancelling import"); throw new RuntimeException("CLIENT SHUTDOWN"); } } }); Runnable run = new Runnable() { public void run() { log.info("Background: Importing images"); importImages(); } }; ex.execute(run); } finally { runState = false; // FIXME this should be set from inside the thread, right? } }
From source file:org.apache.nifi.controller.service.StandardControllerServiceNode.java
/** * Will atomically enable this service by invoking its @OnEnabled operation. * It uses CAS operation on {@link #stateRef} to transition this service * from DISABLED to ENABLING state. If such transition succeeds the service * will be marked as 'active' (see {@link ControllerServiceNode#isActive()}). * If such transition doesn't succeed then no enabling logic will be * performed and the method will exit. In other words it is safe to invoke * this operation multiple times and from multiple threads. * <br>/*w w w . j a va2 s. c om*/ * This operation will also perform re-try of service enabling in the event * of exception being thrown by previous invocation of @OnEnabled. * <br> * Upon successful invocation of @OnEnabled this service will be transitioned to * ENABLED state. * <br> * In the event where enabling took longer then expected by the user and such user * initiated disable operation, this service will be automatically disabled as soon * as it reached ENABLED state. */ @Override public void enable(final ScheduledExecutorService scheduler, final long administrativeYieldMillis) { if (this.stateRef.compareAndSet(ControllerServiceState.DISABLED, ControllerServiceState.ENABLING)) { this.active.set(true); final ConfigurationContext configContext = new StandardConfigurationContext(this, this.serviceProvider, null, getVariableRegistry()); scheduler.execute(new Runnable() { @Override public void run() { try { try (final NarCloseable nc = NarCloseable.withComponentNarLoader( getControllerServiceImplementation().getClass(), getIdentifier())) { ReflectionUtils.invokeMethodsWithAnnotation(OnEnabled.class, getControllerServiceImplementation(), configContext); } boolean shouldEnable = false; synchronized (active) { shouldEnable = active.get() && stateRef.compareAndSet(ControllerServiceState.ENABLING, ControllerServiceState.ENABLED); } if (!shouldEnable) { LOG.debug("Disabling service " + this + " after it has been enabled due to disable action being initiated."); // Can only happen if user initiated DISABLE operation before service finished enabling. It's state will be // set to DISABLING (see disable() operation) invokeDisable(configContext); stateRef.set(ControllerServiceState.DISABLED); } } catch (Exception e) { final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e; final ComponentLog componentLog = new SimpleProcessLogger(getIdentifier(), StandardControllerServiceNode.this); componentLog.error("Failed to invoke @OnEnabled method due to {}", cause); LOG.error("Failed to invoke @OnEnabled method of {} due to {}", getControllerServiceImplementation(), cause.toString()); invokeDisable(configContext); if (isActive()) { scheduler.schedule(this, administrativeYieldMillis, TimeUnit.MILLISECONDS); } else { try (final NarCloseable nc = NarCloseable.withComponentNarLoader( getControllerServiceImplementation().getClass(), getIdentifier())) { ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnDisabled.class, getControllerServiceImplementation(), configContext); } stateRef.set(ControllerServiceState.DISABLED); } } } }); } }
From source file:org.apache.nifi.controller.service.StandardControllerServiceNode.java
/** * Will atomically disable this service by invoking its @OnDisabled operation. * It uses CAS operation on {@link #stateRef} to transition this service * from ENABLED to DISABLING state. If such transition succeeds the service * will be de-activated (see {@link ControllerServiceNode#isActive()}). * If such transition doesn't succeed (the service is still in ENABLING state) * then the service will still be transitioned to DISABLING state to ensure that * no other transition could happen on this service. However in such event * (e.g., its @OnEnabled finally succeeded), the {@link #enable(ScheduledExecutorService, long)} * operation will initiate service disabling javadoc for (see {@link #enable(ScheduledExecutorService, long)} * <br>/*from w w w.j av a 2 s . c om*/ * Upon successful invocation of @OnDisabled this service will be transitioned to * DISABLED state. */ @Override public void disable(ScheduledExecutorService scheduler) { /* * The reason for synchronization is to ensure consistency of the * service state when another thread is in the middle of enabling this * service since it will attempt to transition service state from * ENABLING to ENABLED but only if it's active. */ synchronized (this.active) { this.active.set(false); } if (this.stateRef.compareAndSet(ControllerServiceState.ENABLED, ControllerServiceState.DISABLING)) { final ConfigurationContext configContext = new StandardConfigurationContext(this, this.serviceProvider, null, getVariableRegistry()); scheduler.execute(new Runnable() { @Override public void run() { try { invokeDisable(configContext); } finally { stateRef.set(ControllerServiceState.DISABLED); } } }); } else { this.stateRef.compareAndSet(ControllerServiceState.ENABLING, ControllerServiceState.DISABLING); } }
From source file:org.apache.nifi.controller.StandardProcessorNode.java
/** * Will idempotently start the processor using the following sequence: <i> * <ul>/*from ww w.j a v a 2s. co m*/ * <li>Validate Processor's state (e.g., PropertyDescriptors, * ControllerServices etc.)</li> * <li>Transition (atomically) Processor's scheduled state form STOPPED to * STARTING. If the above state transition succeeds, then execute the start * task (asynchronously) which will be re-tried until @OnScheduled is * executed successfully and "schedulingAgentCallback' is invoked, or until * STOP operation is initiated on this processor. If state transition fails * it means processor is already being started and WARN message will be * logged explaining it.</li> * </ul> * </i> * <p> * Any exception thrown while invoking operations annotated with @OnSchedule * will be caught and logged after which @OnUnscheduled operation will be * invoked (quietly) and the start sequence will be repeated (re-try) after * delay provided by 'administrativeYieldMillis'. * </p> * <p> * Upon successful completion of start sequence (@OnScheduled -> * 'schedulingAgentCallback') the attempt will be made to transition * processor's scheduling state to RUNNING at which point processor is * considered to be fully started and functioning. If upon successful * invocation of @OnScheduled operation the processor can not be * transitioned to RUNNING state (e.g., STOP operation was invoked on the * processor while it's @OnScheduled operation was executing), the * processor's @OnUnscheduled operation will be invoked and its scheduling * state will be set to STOPPED at which point the processor is considered * to be fully stopped. * </p> */ @Override public <T extends ProcessContext & ControllerServiceLookup> void start( final ScheduledExecutorService taskScheduler, final long administrativeYieldMillis, final T processContext, final SchedulingAgentCallback schedulingAgentCallback) { if (!this.isValid()) { throw new IllegalStateException("Processor " + this.getName() + " is not in a valid state due to " + this.getValidationErrors()); } final ComponentLog procLog = new SimpleProcessLogger(StandardProcessorNode.this.getIdentifier(), processor); if (this.scheduledState.compareAndSet(ScheduledState.STOPPED, ScheduledState.STARTING)) { // will ensure that the Processor represented by this node can only be started once final Runnable startProcRunnable = new Runnable() { @Override public void run() { try { invokeTaskAsCancelableFuture(schedulingAgentCallback, new Callable<Void>() { @Override public Void call() throws Exception { try (final NarCloseable nc = NarCloseable .withComponentNarLoader(processor.getClass(), processor.getIdentifier())) { ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, processor, processContext); return null; } } }); if (scheduledState.compareAndSet(ScheduledState.STARTING, ScheduledState.RUNNING)) { schedulingAgentCallback.trigger(); // callback provided by StandardProcessScheduler to essentially initiate component's onTrigger() cycle } else { // can only happen if stopProcessor was called before service was transitioned to RUNNING state try (final NarCloseable nc = NarCloseable.withComponentNarLoader(processor.getClass(), processor.getIdentifier())) { ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, processor, processContext); } scheduledState.set(ScheduledState.STOPPED); } } catch (final Exception e) { final Throwable cause = e instanceof InvocationTargetException ? e.getCause() : e; procLog.error( "{} failed to invoke @OnScheduled method due to {}; processor will not be scheduled to run for {} seconds", new Object[] { StandardProcessorNode.this.getProcessor(), cause, administrativeYieldMillis / 1000L }, cause); LOG.error("Failed to invoke @OnScheduled method due to {}", cause.toString(), cause); ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, processor, processContext); ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, processor, processContext); if (scheduledState.get() != ScheduledState.STOPPING) { // make sure we only continue retry loop if STOP action wasn't initiated taskScheduler.schedule(this, administrativeYieldMillis, TimeUnit.MILLISECONDS); } else { scheduledState.set(ScheduledState.STOPPED); } } } }; taskScheduler.execute(startProcRunnable); } else { final String procName = this.processor.getClass().getSimpleName(); LOG.warn("Can not start '" + procName + "' since it's already in the process of being started or it is DISABLED - " + scheduledState.get()); procLog.warn("Can not start '" + procName + "' since it's already in the process of being started or it is DISABLED - " + scheduledState.get()); } }
From source file:org.apache.nifi.controller.StandardProcessorNode.java
/** * Will idempotently stop the processor using the following sequence: <i> * <ul>/*from w ww . java2s. c o m*/ * <li>Transition (atomically) Processor's scheduled state from RUNNING to * STOPPING. If the above state transition succeeds, then invoke any method * on the Processor with the {@link OnUnscheduled} annotation. Once those methods * have been called and returned (either normally or exceptionally), start checking * to see if all of the Processor's active threads have finished. If not, check again * every 100 milliseconds until they have. * Once all after threads have completed, the processor's @OnStopped operation will be invoked * and its scheduled state is set to STOPPED which completes processor stop * sequence.</li> * </ul> * </i> * * <p> * If for some reason processor's scheduled state can not be transitioned to * STOPPING (e.g., the processor didn't finish @OnScheduled operation when * stop was called), the attempt will be made to transition processor's * scheduled state from STARTING to STOPPING which will allow * {@link #start(ScheduledExecutorService, long, ProcessContext, Runnable)} * method to initiate processor's shutdown upon exiting @OnScheduled * operation, otherwise the processor's scheduled state will remain * unchanged ensuring that multiple calls to this method are idempotent. * </p> */ @Override public <T extends ProcessContext & ControllerServiceLookup> void stop(final ScheduledExecutorService scheduler, final T processContext, final SchedulingAgent schedulingAgent, final ScheduleState scheduleState) { LOG.info("Stopping processor: " + this.processor.getClass()); if (this.scheduledState.compareAndSet(ScheduledState.RUNNING, ScheduledState.STOPPING)) { // will ensure that the Processor represented by this node can only be stopped once scheduleState.incrementActiveThreadCount(); // will continue to monitor active threads, invoking OnStopped once there are no // active threads (with the exception of the thread performing shutdown operations) scheduler.execute(new Runnable() { @Override public void run() { try { if (scheduleState.isScheduled()) { schedulingAgent.unschedule(StandardProcessorNode.this, scheduleState); try (final NarCloseable nc = NarCloseable.withComponentNarLoader(processor.getClass(), processor.getIdentifier())) { ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnUnscheduled.class, processor, processContext); } } // all threads are complete if the active thread count is 1. This is because this thread that is // performing the lifecycle actions counts as 1 thread. final boolean allThreadsComplete = scheduleState.getActiveThreadCount() == 1; if (allThreadsComplete) { try (final NarCloseable nc = NarCloseable.withComponentNarLoader(processor.getClass(), processor.getIdentifier())) { ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, processor, processContext); } scheduleState.decrementActiveThreadCount(); scheduledState.set(ScheduledState.STOPPED); } else { // Not all of the active threads have finished. Try again in 100 milliseconds. scheduler.schedule(this, 100, TimeUnit.MILLISECONDS); } } catch (final Exception e) { LOG.warn("Failed while shutting down processor " + processor, e); } } }); } else { /* * We do compareAndSet() instead of set() to ensure that Processor * stoppage is handled consistently including a condition where * Processor never got a chance to transition to RUNNING state * before stop() was called. If that happens the stop processor * routine will be initiated in start() method, otherwise the IF * part will handle the stop processor routine. */ this.scheduledState.compareAndSet(ScheduledState.STARTING, ScheduledState.STOPPING); } }
From source file:org.openhab.binding.logreader.internal.filereader.FileTailer.java
@Override public void start(String filePath, long refreshRate, ScheduledExecutorService scheduler) throws FileReaderException { tailer = new Tailer(new File(filePath), logListener, refreshRate, true, false, true); try {/*from www . j a va 2 s .co m*/ logger.debug("Start executor"); scheduler.execute(tailer); } catch (Exception e) { throw new FileReaderException(e); } }
From source file:org.ros.concurrent.RetryingExecutorService.java
/** * @param scheduledExecutorService// w w w .j a v a2 s . c om * the {@link ExecutorService} to wrap */ public RetryingExecutorService(ScheduledExecutorService scheduledExecutorService) { this.scheduledExecutorService = scheduledExecutorService; retryLoop = new RetryLoop(); latches = Maps.newConcurrentMap(); callables = Maps.newConcurrentMap(); completionService = new ExecutorCompletionService<Boolean>(scheduledExecutorService); mutex = new Object(); retryDelay = DEFAULT_RETRY_DELAY; retryTimeUnit = DEFAULT_RETRY_TIME_UNIT; running = true; // TODO(damonkohler): Unify this with the passed in ExecutorService. scheduledExecutorService.execute(retryLoop); }
From source file:org.ros.internal.node.DefaultNode.java
/** * {@link DefaultNode}s should only be constructed using the * {@link DefaultNodeFactory}./*from w ww.ja v a 2 s .c om*/ * * @param nodeConfiguration * the {@link NodeConfiguration} for this {@link Node} * @param nodeListeners * a {@link Collection} of {@link NodeListener}s that will be added * to this {@link Node} before it starts */ public DefaultNode(NodeConfiguration nodeConfiguration, Collection<NodeListener> nodeListeners, ScheduledExecutorService scheduledExecutorService) { this.nodeConfiguration = NodeConfiguration.copyOf(nodeConfiguration); this.nodeListeners = new ListenerGroup<NodeListener>(scheduledExecutorService); this.nodeListeners.addAll(nodeListeners); this.scheduledExecutorService = scheduledExecutorService; masterUri = nodeConfiguration.getMasterUri(); masterClient = new MasterClient(masterUri); topicParticipantManager = new TopicParticipantManager(); serviceManager = new ServiceManager(); parameterManager = new ParameterManager(scheduledExecutorService); GraphName basename = nodeConfiguration.getNodeName(); NameResolver parentResolver = nodeConfiguration.getParentResolver(); nodeName = parentResolver.getNamespace().join(basename); resolver = new NodeNameResolver(nodeName, parentResolver); slaveServer = new SlaveServer(nodeName, nodeConfiguration.getTcpRosBindAddress(), nodeConfiguration.getTcpRosAdvertiseAddress(), nodeConfiguration.getXmlRpcBindAddress(), nodeConfiguration.getXmlRpcAdvertiseAddress(), masterClient, topicParticipantManager, serviceManager, parameterManager, scheduledExecutorService); slaveServer.start(); NodeIdentifier nodeIdentifier = slaveServer.toNodeIdentifier(); parameterTree = DefaultParameterTree.newFromNodeIdentifier(nodeIdentifier, masterClient.getRemoteUri(), resolver, parameterManager); publisherFactory = new PublisherFactory(nodeIdentifier, topicParticipantManager, nodeConfiguration.getTopicMessageFactory(), scheduledExecutorService); subscriberFactory = new SubscriberFactory(nodeIdentifier, topicParticipantManager, scheduledExecutorService); serviceFactory = new ServiceFactory(nodeName, slaveServer, serviceManager, scheduledExecutorService); registrar = new Registrar(masterClient, scheduledExecutorService); topicParticipantManager.setListener(registrar); serviceManager.setListener(registrar); scheduledExecutorService.execute(new Runnable() { @Override public void run() { start(); } }); }
From source file:org.ros.internal.transport.IncomingMessageQueue.java
public IncomingMessageQueue(MessageDeserializer<MessageType> deserializer, ScheduledExecutorService executorService) { this.deserializer = deserializer; this.executorService = executorService; messages = new CircularBlockingQueue<MessageType>(MESSAGE_BUFFER_CAPACITY); listeners = new ListenerCollection<MessageListener<MessageType>>(executorService, true); dispatcher = new Dispatcher(); latchMode = false;/*w w w . j a v a 2 s . com*/ latchedMessage = null; executorService.execute(dispatcher); }