List of usage examples for java.util.concurrent ScheduledExecutorService awaitTermination
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
From source file:com.liferay.portal.search.elasticsearch.internal.connection.EmbeddedElasticsearchConnection.java
@Override public void close() { super.close(); if (_node == null) { return;/*from ww w .ja v a 2 s . c o m*/ } try { Class.forName(ByteBufferUtil.class.getName()); } catch (ClassNotFoundException cnfe) { if (_log.isWarnEnabled()) { _log.warn( StringBundler.concat("Unable to preload ", String.valueOf(ByteBufferUtil.class), " to prevent Netty shutdown concurrent class loading ", "interruption issue"), cnfe); } } if (PortalRunMode.isTestMode()) { settingsBuilder.put("index.refresh_interval", "-1"); settingsBuilder.put("index.translog.flush_threshold_ops", Integer.MAX_VALUE); settingsBuilder.put("index.translog.interval", "1d"); Settings settings = settingsBuilder.build(); Injector injector = _node.injector(); IndicesService indicesService = injector.getInstance(IndicesService.class); Iterator<IndexService> iterator = indicesService.iterator(); while (iterator.hasNext()) { IndexService indexService = iterator.next(); injector = indexService.injector(); IndexSettingsService indexSettingsService = injector.getInstance(IndexSettingsService.class); indexSettingsService.refreshSettings(settings); } ThreadPool threadPool = injector.getInstance(ThreadPool.class); ScheduledExecutorService scheduledExecutorService = threadPool.scheduler(); if (scheduledExecutorService instanceof ThreadPoolExecutor) { ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) scheduledExecutorService; threadPoolExecutor.setRejectedExecutionHandler(_REJECTED_EXECUTION_HANDLER); } scheduledExecutorService.shutdown(); try { scheduledExecutorService.awaitTermination(1, TimeUnit.HOURS); } catch (InterruptedException ie) { if (_log.isWarnEnabled()) { _log.warn("Thread pool shutdown wait was interrupted", ie); } } } _node.close(); _node = null; _file.deltree(_jnaTmpDirName); }
From source file:org.apache.bookkeeper.bookie.LedgerStorageCheckpointTest.java
@Before public void setUp() throws Exception { LOG.info("Setting up test {}", getClass()); PowerMockito.mockStatic(Executors.class); try {//from w ww. ja v a 2 s.c om // start zookeeper service startZKCluster(); } catch (Exception e) { LOG.error("Error setting up", e); throw e; } ScheduledExecutorService scheduledExecutorService = PowerMockito.mock(ScheduledExecutorService.class); executorController = new MockExecutorController().controlSubmit(scheduledExecutorService) .controlScheduleAtFixedRate(scheduledExecutorService, 10); PowerMockito.when(scheduledExecutorService.awaitTermination(anyLong(), any(TimeUnit.class))) .thenReturn(true); PowerMockito.when(Executors.newSingleThreadScheduledExecutor(any())).thenReturn(scheduledExecutorService); }
From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorTest.java
@Test public void shouldNotShutdownExecutorServicesSuppliedToGremlinExecutor() throws Exception { final ScheduledExecutorService service = Executors.newScheduledThreadPool(4, testingThreadFactory); final GremlinExecutor gremlinExecutor = GremlinExecutor.build().executorService(service) .scheduledExecutorService(service).create(); gremlinExecutor.close();/*from w w w. j av a 2 s . com*/ assertFalse(service.isShutdown()); service.shutdown(); service.awaitTermination(30000, TimeUnit.MILLISECONDS); }
From source file:com.btoddb.chronicle.plunkers.HdfsPlunkerImplIT.java
@Test @Ignore("very flakey, need to work out a more stable way of testing") public void testLongRun() throws Exception { plunker.setIdleTimeout(0);/*from w w w. j a v a2 s. c o m*/ plunker.setRollPeriod(2); plunker.setTimeoutCheckPeriod(100); plunker.init(config); final int sleep = 200; final int maxCount = 100; // 20 seconds at 'sleep' interval should be 10 files final AtomicInteger count = new AtomicInteger(); // do this to prime HDFS FileSystem object - otherwise timing is off plunker.handleInternal(Arrays.asList(new Event("the-body").withHeader("customer", "cust") .withHeader("msgId", String.valueOf(count.getAndIncrement())))); ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); System.out.println("start"); executor.scheduleWithFixedDelay(new Runnable() { @Override public void run() { try { System.out.println("time = " + System.currentTimeMillis()); plunker.handleInternal(Arrays.asList(new Event("the-body").withHeader("customer", "cust") .withHeader("msgId", String.valueOf(count.get())))); } catch (Exception e) { e.printStackTrace(); } count.incrementAndGet(); } }, 0, sleep, TimeUnit.MILLISECONDS); while (count.get() < maxCount) { Thread.sleep(sleep / 2); } executor.shutdown(); executor.awaitTermination(60, TimeUnit.SECONDS); Thread.sleep(1500); plunker.shutdown(); Event[] events = new Event[count.get()]; for (int i = 0; i < count.get(); i++) { events[i] = new Event("the-body").withHeader("customer", "cust").withHeader("msgId", String.valueOf(i)); } File theDir = new File(String.format("%s/the/cust/path", baseDir.getPath())); assertThat(theDir, ftUtils.countWithSuffix(".tmp", 0)); assertThat(theDir, ftUtils.countWithSuffix(".avro", 10)); assertThat(theDir, ftUtils.hasEventsInDir(events)); }
From source file:org.apache.nifi.controller.StandardFlowService.java
@Override public void stop(final boolean force) { writeLock.lock();//from w w w . j ava2 s. c o m try { if (!isRunning()) { return; } running.set(false); if (clusterCoordinator != null) { final Thread shutdownClusterCoordinator = new Thread(new Runnable() { @Override public void run() { clusterCoordinator.shutdown(); } }); shutdownClusterCoordinator.setDaemon(true); shutdownClusterCoordinator.setName("Shutdown Cluster Coordinator"); shutdownClusterCoordinator.start(); } if (!controller.isTerminated()) { controller.shutdown(force); } if (configuredForClustering && senderListener != null) { try { senderListener.stop(); } catch (final IOException ioe) { logger.warn("Protocol sender/listener did not stop gracefully due to: " + ioe); } } final ScheduledExecutorService executorService = executor.get(); if (executorService != null) { if (force) { executorService.shutdownNow(); } else { executorService.shutdown(); } boolean graceful; try { graceful = executorService.awaitTermination(gracefulShutdownSeconds, TimeUnit.SECONDS); } catch (final InterruptedException e) { graceful = false; } if (!graceful) { logger.warn("Scheduling service did not gracefully shutdown within configured " + gracefulShutdownSeconds + " second window"); } } } finally { writeLock.unlock(); } }
From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorTest.java
@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 {//from w ww.j a v a 2s . c om 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); }
From source file:com.amazonaws.services.dynamodbv2.streamsadapter.functionals.CorrectnessTest.java
/** * This test spawns a thread to periodically write items to the source table. It shuts down and restarts the KCL * worker while writes are happening (to simulate the real-world situation of a worker dying and another taking its * place). There are two things being verified here: * 1. New KCL worker resumes from the checkpoint * 2. All stream records are processed/*from w w w . j a va 2 s . c o m*/ * * @throws Exception */ @Test public void workerFailureTest() throws Exception { LOG.info("Starting single shard KCL worker failure test."); KinesisClientLibConfiguration workerConfig = new KinesisClientLibConfiguration(leaseTable, streamId, credentials, KCL_WORKER_ID).withInitialPositionInStream(InitialPositionInStream.TRIM_HORIZON); startKCLWorker(workerConfig); // A thread that keeps writing to the table every 2 seconds ScheduledExecutorService loadGeneratorService = Executors.newSingleThreadScheduledExecutor(); loadGeneratorService.scheduleAtFixedRate(new Runnable() { @Override public void run() { insertAndUpdateItems(1); } }, 0/* initialDelay */, 2/* period */, TimeUnit.SECONDS); while (recordProcessorFactory.getNumRecordsProcessed() < 10) { LOG.info("Sleep till first few records are processed"); Thread.sleep(THREAD_SLEEP_2S); } shutDownKCLWorker(); // Calculate number of records processed by first worker and also the number of processed-but-not-checkpointed // records, since checkpoint happens after every batch of 10 records int numRecordsProcessedByFirstWorker = recordProcessorFactory.getNumRecordsProcessed(); int numRecordsNotCheckpointed = numRecordsProcessedByFirstWorker % ReplicatingRecordProcessor.CHECKPOINT_BATCH_SIZE; // Start a new worker startKCLWorker(workerConfig); while (recordProcessorFactory.getNumRecordsProcessed() < 0) { LOG.info("Sleep till RecordProcessor is initialized"); Thread.sleep(THREAD_SLEEP_2S); } loadGeneratorService.shutdown(); if (!loadGeneratorService.awaitTermination(THREAD_SLEEP_5S, TimeUnit.MILLISECONDS)) { loadGeneratorService.shutdownNow(); } int numStreamRecords = 2 * this.numItemsInSrcTable; int remainingRecordsToBeProcessed = numStreamRecords - numRecordsProcessedByFirstWorker + numRecordsNotCheckpointed; /* * The second worker must process atleast remainingRecordsToBeProcessed * num of records so that we have replicated everything to destination * table. Thus, this should never technically end up as an infinite * loop. If it does, something else is gone wrong. */ while (recordProcessorFactory.getNumRecordsProcessed() < remainingRecordsToBeProcessed) { LOG.info("Sleep till remaining records are processed"); Thread.sleep(THREAD_SLEEP_2S); } shutDownKCLWorker(); ScanResult srcTableScan = TestUtil.scanTable(dynamoDBClient, srcTable); ScanResult destTableScan = TestUtil.scanTable(dynamoDBClient, destTable); assertEquals(srcTableScan.getItems(), destTableScan.getItems()); }
From source file:net.sf.mpaxs.spi.computeHost.Host.java
/** * Meldet diesen Host beim Masterserver an. Nach erfolgreicher Anmeldung * kann der Masterserver Jobs an diesen Host vergeben. *///from w w w . j a v a 2 s .co m private void connectToMasterServer() { final ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); final long startUpAt = System.currentTimeMillis(); final long failAfter = 5000; ses.scheduleAtFixedRate(new Runnable() { @Override public void run() { Logger.getLogger(Host.class.getName()).log(Level.FINE, "Trying to connect to MasterServer from IP " + settings.getLocalIp()); long currentTime = System.currentTimeMillis(); if (currentTime - startUpAt >= failAfter) { Logger.getLogger(Host.class.getName()).log(Level.WARNING, "Waited {0} seconds for MasterServer, shutting down ComputeHost", (failAfter / 1000)); System.exit(1); } try { Logger.getLogger(Host.class.getName()).log(Level.FINE, "Trying to bind to MasterServer at " + settings.getMasterServerIP() + ":" + settings.getMasterServerPort() + " with name: " + settings.getMasterServerName()); IRemoteServer remRef = (IRemoteServer) Naming.lookup("//" + settings.getMasterServerIP() + ":" + settings.getMasterServerPort() + "/" + settings.getMasterServerName()); settings.setRemoteReference(remRef); UUID hostID = remRef.addHost(authToken, settings.getName(), settings.getLocalIp(), settings.getCores()); settings.setHostID(hostID); Logger.getLogger(Host.class.getName()).log(Level.FINE, "Connection to server established!"); ses.shutdown(); try { ses.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException ex) { Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex); } } catch (NotBoundException ex) { Logger.getLogger(Host.class.getName()).log(Level.INFO, "Master server not found, waiting for connection!"); //Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex); } catch (MalformedURLException ex) { Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex); System.exit(1); } catch (RemoteException ex) { Logger.getLogger(Host.class.getName()).log(Level.SEVERE, null, ex); System.exit(1); } } }, 1, settings.getMasterServerTimeout(), TimeUnit.SECONDS); }
From source file:org.apache.jackrabbit.core.RepositoryImpl.java
/** * Protected method that performs the actual shutdown after the shutdown * lock has been acquired by the {@link #shutdown()} method. *//*from www . j a v a 2 s.com*/ protected synchronized void doShutdown() { log.info("Shutting down repository..."); // stop optional cluster node ClusterNode clusterNode = context.getClusterNode(); if (clusterNode != null) { clusterNode.stop(); } if (securityMgr != null) { securityMgr.close(); } // close active user sessions // (copy sessions to array to avoid ConcurrentModificationException; // manually copy entries rather than calling ReferenceMap#toArray() in // order to work around http://issues.apache.org/bugzilla/show_bug.cgi?id=25551) List<Session> sa; synchronized (activeSessions) { sa = new ArrayList<Session>(activeSessions.size()); for (Session session : activeSessions.values()) { sa.add(session); } } for (Session session : sa) { if (session != null) { session.logout(); } } // shutdown system search manager if there is one if (systemSearchMgr != null) { systemSearchMgr.close(); } // shut down workspaces synchronized (wspInfos) { for (WorkspaceInfo wspInfo : wspInfos.values()) { wspInfo.dispose(); } } try { InternalVersionManager m = context.getInternalVersionManager(); if (m != null) { m.close(); } } catch (Exception e) { log.error("Error while closing Version Manager.", e); } repDescriptors.clear(); DataStore dataStore = context.getDataStore(); if (dataStore != null) { try { // close the datastore dataStore.close(); } catch (DataStoreException e) { log.error("error while closing datastore", e); } } try { // close repository file system context.getFileSystem().close(); } catch (FileSystemException e) { log.error("error while closing repository file system", e); } try { nodeIdFactory.close(); } catch (RepositoryException e) { log.error("error while closing repository file system", e); } // make sure this instance is not used anymore disposed = true; // wake up threads waiting on this instance's monitor (e.g. workspace janitor) notifyAll(); // Shut down the executor service ScheduledExecutorService executor = context.getExecutor(); executor.shutdown(); try { // Wait for all remaining background threads to terminate if (!executor.awaitTermination(10, TimeUnit.SECONDS)) { log.warn("Attempting to forcibly shutdown runaway threads"); executor.shutdownNow(); } } catch (InterruptedException e) { log.warn("Interrupted while waiting for background threads", e); } repConfig.getConnectionFactory().close(); // finally release repository lock if (repLock != null) { try { repLock.release(); } catch (RepositoryException e) { log.error("failed to release the repository lock", e); } } log.info("Repository has been shutdown"); }
From source file:com.palantir.atlasdb.transaction.impl.SnapshotTransactionTest.java
@Test public void testTransactionAtomicity() throws Exception { // This test runs multiple transactions in parallel, with KeyValueService.put calls throwing // a RuntimeException from time to time and hanging other times. which effectively kills the // thread. We ensure that every transaction either adds 5 rows to the table or adds 0 rows // by checking at the end that the number of rows is a multiple of 5. final String tableName = "table"; Random random = new Random(1); final UnstableKeyValueService unstableKvs = new UnstableKeyValueService(keyValueService, random); final TestTransactionManager unstableTransactionManager = new TestTransactionManagerImpl(unstableKvs, timestampService, lockClient, lockService, transactionService, conflictDetectionManager, sweepStrategyManager);// w w w . ja v a2 s.co m ScheduledExecutorService service = PTExecutors.newScheduledThreadPool(20); for (int i = 0; i < 30; i++) { final int threadNumber = i; service.schedule(new Callable<Void>() { @Override public Void call() throws Exception { if (threadNumber == 10) { unstableKvs.setRandomlyThrow(true); } if (threadNumber == 20) { unstableKvs.setRandomlyHang(true); } Transaction transaction = unstableTransactionManager.createNewTransaction(); BatchingVisitable<RowResult<byte[]>> results = transaction.getRange(tableName, RangeRequest.builder().build()); final MutableInt nextIndex = new MutableInt(0); results.batchAccept(1, AbortingVisitors.batching(new AbortingVisitor<RowResult<byte[]>, Exception>() { @Override public boolean visit(RowResult<byte[]> row) throws Exception { byte[] dataBytes = row.getColumns().get("data".getBytes()); BigInteger dataValue = new BigInteger(dataBytes); nextIndex.setValue(Math.max(nextIndex.toInteger(), dataValue.intValue() + 1)); return true; } })); // nextIndex now contains the least row number not already in the table. Add 5 more // rows to the table. for (int j = 0; j < 5; j++) { int rowNumber = nextIndex.toInteger() + j; Cell cell = Cell.create(("row" + rowNumber).getBytes(), "data".getBytes()); transaction.put(tableName, ImmutableMap.of(cell, BigInteger.valueOf(rowNumber).toByteArray())); Thread.yield(); } transaction.commit(); return null; } }, i * 20, TimeUnit.MILLISECONDS); } service.shutdown(); service.awaitTermination(1, TimeUnit.SECONDS); // Verify each table has a number of rows that's a multiple of 5 Transaction verifyTransaction = txManager.createNewTransaction(); BatchingVisitable<RowResult<byte[]>> results = verifyTransaction.getRange(tableName, RangeRequest.builder().build()); final MutableInt numRows = new MutableInt(0); results.batchAccept(1, AbortingVisitors.batching(new AbortingVisitor<RowResult<byte[]>, Exception>() { @Override public boolean visit(RowResult<byte[]> row) throws Exception { numRows.increment(); return true; } })); Assert.assertEquals(0, numRows.toInteger() % 5); }