public static void startSystem() throws ExecutionException, InterruptedException {

    // Create the entry gates and link them to the parking lot
    for (int i = 0; i < numOfEntries; i++) {
      entries.add(new Entry("entry" + i, parkingLot));
    }
    parkingLot.setEntries(entries);

    // Create the exit gates and link them to the parking lot
    for (int k = 0; k < numOfExits; k++) {
      exits.add(new Exit("exit" + k, parkingLot));
    }
    parkingLot.setExits(exits);

    // Create a thread pool based on the number of entries and exits for this lot
    ScheduledExecutorService scheduledExecutorService1 =
        Executors.newScheduledThreadPool(numOfEntries);
    ScheduledExecutorService scheduledExecutorService2 =
        Executors.newScheduledThreadPool(numOfExits);

    // Schedule the threads, which will act as Car objects entering and exiting the parking lot
    ScheduledFuture<?> scheduledFuture1 =
        scheduledExecutorService1.scheduleWithFixedDelay(
            new Car(entries, exits, 0), firstCarEntryTime, enteringCarsDelay, TimeUnit.SECONDS);

    ScheduledFuture<?> scheduledFuture2 =
        scheduledExecutorService2.scheduleWithFixedDelay(
            new Car(entries, exits, 1), firstCarExitTime, exitingCarsDelay, TimeUnit.SECONDS);

    scheduledFuture1.get();
    scheduledFuture2.get();
    scheduledExecutorService1.shutdown();
    scheduledExecutorService2.shutdown();
  }
 public void cleanup() {
   logger.info("canceling the jobs");
   jobHandle.cancel(true);
   ciEventsJobHandle.cancel(true);
   logger.info("shuting down the scheduler");
   ciEventScheduler.shutdown();
   controllerEventScheduler.shutdown();
 }
  public void stop() {

    try {
      flush();
    } catch (IOException e) {
      LOGGER.error("Error flushing", e);
    }
    try {
      close();
    } catch (IOException e) {
      LOGGER.error("Error closing", e);
    }
    try {
      backgroundFlushTask.shutdown();
      // Wait a while for existing tasks to terminate
      if (!backgroundFlushTask.awaitTermination(15, TimeUnit.SECONDS)) {
        backgroundFlushTask.shutdownNow(); // Cancel currently executing tasks
        // Wait a while for tasks to respond to being cancelled
        if (!backgroundFlushTask.awaitTermination(15, TimeUnit.SECONDS)) {
          LOGGER.error("Stream did not terminate");
        }
      }
    } catch (InterruptedException ie) {
      // (Re-)Cancel if current thread also interrupted
      backgroundFlushTask.shutdownNow();
      // Preserve interrupt status
      Thread.currentThread().interrupt();
    }
  }
Exemple #4
0
 private void stop() {
   log.info("ExecutorManager stopping...");
   scheduler.shutdown();
   try {
     scheduler.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
   } catch (Exception e) {
     log.error("scheduler await termination error", e);
   }
   canceller.shutdown();
   try {
     canceller.awaitTermination(0, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
     log.error("canceller await termination error", e);
   }
   nearRealTimeOrderedTasksMap.clear();
 }
  @Override
  public void stop() {
    logger.info("Avro source {} stopping: {}", getName(), this);

    server.close();

    try {
      server.join();
    } catch (InterruptedException e) {
      logger.info(
          "Avro source "
              + getName()
              + ": Interrupted while waiting "
              + "for Avro server to stop. Exiting. Exception follows.",
          e);
    }
    sourceCounter.stop();
    connectionCountUpdater.shutdown();
    while (!connectionCountUpdater.isTerminated()) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException ex) {
        logger.error(
            "Interrupted while waiting for connection count executor " + "to terminate", ex);
        Throwables.propagate(ex);
      }
    }
    super.stop();
    logger.info("Avro source {} stopped. Metrics: {}", getName(), sourceCounter);
  }
  @Override
  public synchronized void close() {
    while (!getText().isEmpty()) {
      sendMessage();

      try {
        Thread.sleep(1_000);
      } catch (InterruptedException exception) {
        executor.shutdown();

        LOG.error(getClass() + " A problem occurred while closing sender", exception);
      }
    }

    executor.shutdown();
  }
 /** stop the scheduler for data collecting jobs. */
 public void stop() {
   if (isRunning()) {
     scheduler.shutdown();
     isRunning = false;
     LOG.info("Collector shutdown");
   }
 }
Exemple #8
0
 @PreDestroy
 public void shutdown() {
   request.stop();
   readBytes.stop();
   writtenBytes.stop();
   executor.shutdown();
 }
  /**
   * Test to make sure we reschedule the task for execution if the new requested execution is
   * earlier than the previous one
   */
  public void testRescheduleForEarlierTime() throws InterruptedException {
    ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
    MyConflationListener listener = new MyConflationListener();
    OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex, listener);

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger counter = new AtomicInteger();

    Runnable increment =
        new Runnable() {

          public void run() {
            counter.incrementAndGet();
          }
        };

    decorator.schedule(increment, 120, TimeUnit.SECONDS);
    decorator.schedule(increment, 10, TimeUnit.MILLISECONDS);

    long start = System.nanoTime();

    ex.shutdown();
    ex.awaitTermination(60, TimeUnit.SECONDS);
    long elapsed = System.nanoTime() - start;
    assertEquals(1, counter.get());
    assertEquals(1, listener.getDropCount());
    assertTrue(elapsed < TimeUnit.SECONDS.toNanos(120));
  }
  /**
   * Empty this pool, freeing any resources.
   *
   * @throws IllegalStateException if this pool has not been initialized
   */
  @Override
  public void close() {
    logger.debug("closing connection pool of size {}", available.size() + active.size());
    isInitialized();
    poolLock.lock();
    try {
      while (!available.isEmpty()) {
        final PooledConnectionProxy pc = available.remove();
        pc.getConnection().close();
        logger.trace("destroyed connection: {}", pc);
      }
      while (!active.isEmpty()) {
        final PooledConnectionProxy pc = active.remove();
        pc.getConnection().close();
        logger.trace("destroyed connection: {}", pc);
      }
      logger.debug("pool closed");
    } finally {
      poolLock.unlock();
    }

    logger.debug("shutting down executor");
    poolExecutor.shutdown();
    logger.debug("executor shutdown");
    initialized = false;
  }
 @Override
 public void stop() {
   cleanup();
   if (!isShared) {
     reaper.shutdown();
   }
 }
  /**
   * Release the tryClaim of the coordinate. It means that nobody owns the coordinate anymore.
   * Requires that that this instance owns the tryClaim to the coordinate.
   */
  public void releaseClaim() throws CloudnameException {
    scheduler.shutdown();
    zkClient.deregisterListener(this);

    while (true) {
      final TrackedConfig config;
      synchronized (trackedConfigList) {
        if (trackedConfigList.isEmpty()) {
          break;
        }
        config = trackedConfigList.remove(0);
      }
      config.stop();
    }

    sendEventToCoordinateListener(
        CoordinateListener.Event.NOT_OWNER, "Released claim of coordinate");

    synchronized (lastStatusVersionMonitor) {
      try {
        zkClient.getZookeeper().delete(path, lastStatusVersion);
      } catch (InterruptedException e) {
        throw new CloudnameException(e);
      } catch (KeeperException e) {
        throw new CloudnameException(e);
      }
    }
  }
  @Test
  public void shouldNotExhaustThreads() throws Exception {
    final ScheduledExecutorService executorService =
        Executors.newScheduledThreadPool(2, testingThreadFactory);
    final GremlinExecutor gremlinExecutor =
        GremlinExecutor.build()
            .executorService(executorService)
            .scheduledExecutorService(executorService)
            .create();

    final AtomicInteger count = new AtomicInteger(0);
    assertTrue(
        IntStream.range(0, 1000)
            .mapToObj(i -> gremlinExecutor.eval("1+1"))
            .allMatch(
                f -> {
                  try {
                    return (Integer) f.get() == 2;
                  } catch (Exception ex) {
                    throw new RuntimeException(ex);
                  } finally {
                    count.incrementAndGet();
                  }
                }));

    assertEquals(1000, count.intValue());

    executorService.shutdown();
    executorService.awaitTermination(30000, TimeUnit.MILLISECONDS);
  }
 public void setPingEnabled(boolean enabled) {
   this.pingEnabled = enabled;
   if (enabled) {
     if (pingScheduler == null) {
       long interval = idleTimeoutMs - 5000L;
       if (interval < 0) {
         interval /= 2;
       }
       pingScheduler = Executors.newScheduledThreadPool(1);
       pingScheduler.scheduleAtFixedRate(
           new Runnable() {
             @Override
             public void run() {
               try {
                 WoopraPing ping =
                     new WoopraPing(domain, getVisitor().getCookie(), clientInfo, idleTimeoutMs);
                 ping.ping();
               } catch (Throwable t) {
                 Log.e(TAG, "unknown ping error", t);
               }
             }
           },
           interval,
           interval,
           TimeUnit.MILLISECONDS);
     }
   } else {
     if (pingScheduler != null) {
       pingScheduler.shutdown();
       pingScheduler = null;
     }
   }
 }
Exemple #15
0
  public void close() throws IOException {
    flush();

    if (!commitExecutor.isShutdown()) {
      commitExecutor.shutdown();
      try {
        if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
          throw new OException("Background data flush task can not be stopped.");
      } catch (InterruptedException e) {
        OLogManager.instance().error(this, "Data flush thread was interrupted");

        Thread.interrupted();
        throw new OException("Data flush thread was interrupted", e);
      }
    }

    synchronized (syncObject) {
      for (OFileClassic fileClassic : files.values()) {
        if (fileClassic.isOpen()) fileClassic.close();
      }

      if (nameIdMapHolder != null) {
        nameIdMapHolder.setLength(0);
        for (Map.Entry<String, Long> entry : nameIdMap.entrySet()) {
          writeNameIdEntry(new NameFileIdEntry(entry.getKey(), entry.getValue()), false);
        }
        nameIdMapHolder.getFD().sync();
        nameIdMapHolder.close();
      }
    }
  }
  @Test
  public void testKeyValueStateInWindowFunctionTumbling() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final long thirtySeconds = 30_000;

      final CollectingOutput<Tuple2<Integer, Integer>> out = new CollectingOutput<>();
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      StatefulFunction.globalCounts.clear();

      AggregatingProcessingTimeWindowOperator<Integer, Tuple2<Integer, Integer>> op =
          new AggregatingProcessingTimeWindowOperator<>(
              new StatefulFunction(),
              fieldOneSelector,
              IntSerializer.INSTANCE,
              tupleSerializer,
              thirtySeconds,
              thirtySeconds);

      op.setup(mockTask, createTaskConfig(fieldOneSelector, IntSerializer.INSTANCE), out);
      op.open();

      // because the window interval is so large, everything should be in one window
      // and aggregate into one value per key

      synchronized (lock) {
        for (int i = 0; i < 10; i++) {
          StreamRecord<Tuple2<Integer, Integer>> next1 = new StreamRecord<>(new Tuple2<>(1, i));
          op.setKeyContextElement(next1);
          op.processElement(next1);

          StreamRecord<Tuple2<Integer, Integer>> next2 = new StreamRecord<>(new Tuple2<>(2, i));
          op.setKeyContextElement(next2);
          op.processElement(next2);
        }
      }

      out.waitForNElements(2, 60_000);

      List<Tuple2<Integer, Integer>> result = out.getElements();
      assertEquals(2, result.size());

      Collections.sort(result, tupleComparator);
      assertEquals(45, result.get(0).f1.intValue());
      assertEquals(45, result.get(1).f1.intValue());

      assertEquals(10, StatefulFunction.globalCounts.get(1).intValue());
      assertEquals(10, StatefulFunction.globalCounts.get(2).intValue());

      op.close();
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  public static synchronized void clearThreadPools() {

    if (globalThreadPool != null) {
      globalThreadPool.shutdown();
      try {
        if (!globalThreadPool.awaitTermination(10, TimeUnit.SECONDS)) {
          throw new IllegalStateException("Couldn't finish the globalThreadPool");
        }
      } catch (InterruptedException e) {
      } finally {
        globalThreadPool = null;
      }
    }

    if (globalScheduledThreadPool != null) {
      globalScheduledThreadPool.shutdown();
      try {
        if (!globalScheduledThreadPool.awaitTermination(10, TimeUnit.SECONDS)) {
          throw new IllegalStateException("Couldn't finish the globalScheduledThreadPool");
        }
      } catch (InterruptedException e) {
      } finally {
        globalScheduledThreadPool = null;
      }
    }
  }
 public static void main(String[] args) throws InterruptedException, ExecutionException {
   // *1
   ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
   // *2
   Runnable task1 =
       new Runnable() {
         public void run() {
           System.out.println("Taskrepeating.");
         }
       };
   // *3 每隔5秒执行一次
   final ScheduledFuture future1 = service.scheduleAtFixedRate(task1, 0, 1, TimeUnit.SECONDS);
   // *4
   ScheduledFuture future2 =
       service.schedule(
           new Callable() {
             public String call() {
               future1.cancel(true);
               return "taskcancelled!";
             }
           },
           10,
           TimeUnit.SECONDS);
   System.out.println(future2.get());
   // *5
   service.shutdown();
 }
Exemple #19
0
  public void delete() throws IOException {
    synchronized (syncObject) {
      for (long fileId : files.keySet()) doDeleteFile(fileId);

      if (nameIdMapHolderFile != null) {
        nameIdMapHolder.close();

        if (!nameIdMapHolderFile.delete())
          throw new OStorageException(
              "Can not delete disk cache file which contains name-id mapping.");
      }
    }

    if (!commitExecutor.isShutdown()) {
      commitExecutor.shutdown();
      try {
        if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
          throw new OException("Background data flush task can not be stopped.");
      } catch (InterruptedException e) {
        OLogManager.instance().error(this, "Data flush thread was interrupted");

        Thread.interrupted();
        throw new OException("Data flush thread was interrupted", e);
      }
    }
  }
 @Override
 public void stopLogging() {
   super.stopLogging();
   if (_rolledPolicyExecutor != null) {
     _rolledPolicyExecutor.shutdown();
   }
 }
 @Override
 public void shutdown() throws IOException {
   isShutDown = true;
   httpClient.close();
   scalarMeasurementPool.shutdown();
   offloadingThreadPool.shutdown();
 }
  @Test
  public void testKeyValueStateInWindowFunction() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>(50);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      StatefulFunction.globalCounts.clear();

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              new StatefulFunction(),
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              50,
              50);

      op.setup(mockTask, createTaskConfig(identitySelector, IntSerializer.INSTANCE), out);
      op.open();

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
      }
      out.waitForNElements(2, 60000);

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
        op.processElement(new StreamRecord<Integer>(2));
      }
      out.waitForNElements(8, 60000);

      List<Integer> result = out.getElements();
      assertEquals(8, result.size());

      Collections.sort(result);
      assertEquals(Arrays.asList(1, 1, 1, 1, 2, 2, 2, 2), result);

      assertEquals(4, StatefulFunction.globalCounts.get(1).intValue());
      assertEquals(4, StatefulFunction.globalCounts.get(2).intValue());

      synchronized (lock) {
        op.close();
      }
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
 @Override
 public void destroy() {
   registers.clear();
   configInstances.clear();
   configFiles.clear();
   scheduleService.shutdown();
   init = Boolean.FALSE;
 }
 public void onDestroy() {
   executorService.shutdown();
   serializeDownloadQueue();
   downloadService.clear(false);
   downloadService.unregisterReceiver(ejectEventReceiver);
   downloadService.unregisterReceiver(headsetEventReceiver);
   downloadService.unregisterReceiver(intentReceiver);
 }
 public void shutdownBlocking() {
   service.shutdown();
   try {
     service.awaitTermination(defaultExpiration * 2, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
     throw new AssertionError(e);
   }
 }
 @Override
 public void close() {
   try {
     scheduler.shutdown();
   } catch (Exception e) {
     LOG.warn("Error when closing zookeeper based ledger manager: ", e);
   }
 }
    @Override
    public void run() {
      if (System.currentTimeMillis() > expiryTime) {
        scheduledExecutorService.shutdown();
        throw new RuntimeException(
            "failed to discover beans: " + managedTypes.getServiceEndpoints());
      }

      if (registered.isEmpty()) {
        scheduledExecutorService.shutdown();
        log.info("all services registered successfully");
        return;
      }

      for (final AnnotatedType<?> type : managedTypes.getServiceEndpoints()) {
        if (!registered.contains(type) || beanManager.getBeans(type.getJavaClass()).size() == 0) {
          continue;
        }

        final MessageCallback callback =
            (MessageCallback) CDIServerUtil.lookupBean(beanManager, type.getJavaClass());

        registered.remove(type);

        // Discriminate on @Command
        final Map<String, Method> commandPoints = new HashMap<String, Method>();
        for (final AnnotatedMethod method : type.getMethods()) {
          if (method.isAnnotationPresent(Command.class)) {
            final Command command = method.getAnnotation(Command.class);
            for (String cmdName : command.value()) {
              if (cmdName.equals("")) cmdName = method.getJavaMember().getName();
              commandPoints.put(cmdName, method.getJavaMember());
            }
          }
        }

        final String subjectName = CDIServerUtil.resolveServiceName(type.getJavaClass());

        if (commandPoints.isEmpty()) {
          bus.subscribe(subjectName, callback);
        } else {
          bus.subscribeLocal(
              subjectName, new CommandBindingsCallback(commandPoints, callback, bus));
        }
      }
    }
 /** {@inheritDoc} */
 @Override
 public void stop() {
   try {
     indexLastDirtyElements(5);
   } finally {
     executorService.shutdown();
   }
 }
 private void tearDown() throws IOException {
   // close job runtime writer
   if (jobRuntimeLogBW != null) {
     jobRuntimeLogBW.close();
   }
   // shut pool
   if (pool != null) pool.shutdown();
 }
  /**
   * Stop the bundle. Nothing to stop for now.
   *
   * @param bundleContext <tt>BundleContext</tt> provided by OSGi framework
   * @throws Exception if something goes wrong during stop
   */
  public void stop(BundleContext bundleContext) throws Exception {
    if (logger.isDebugEnabled()) logger.debug("Update checker [STOPPED]");

    if (mUpdateExecutor != null) {
      mUpdateExecutor.shutdown();
      mUpdateExecutor = null;
    }
  }