public boolean doSomething() throws InterruptedException { TestStatus.latch = new CountDownLatch(1); executor.submit(new Runnable() { @Override public void run() { TestStatus.latch.countDown(); } }); TestStatus.latch.await(2000, TimeUnit.MILLISECONDS); return true; } }
@Stateless public class ReportBean { @Resource private ManagedExecutorService executorService; public void runReports() { ReportTask reportTask = new ReportTask(); Future<Report> future = executorService.submit(reportTask); } }
@Stateless public class ReportBean { @Resource private ManagedExecutorService executorService; public void runReports() { ReportTask reportTask = new ReportTask(); Future<Report> future = executorService.submit(reportTask); } }
/** * Sends the order to build the session as soon as this is constructed. The actual execution happens in * background, as we don't want to delay the boot of the application. */ @PostConstruct public void init() { sessionFuture = executor.submit(cassandraSessionCallable); }
@PostConstruct public void init() { if (null == executor) { // we have no executor, we are probably not in an EE environment try { FutureTask<Session> task = new FutureTask<>(cassandraSessionCallable); task.run(); sessionFuture = task; } catch (Exception e) { throw new RuntimeException("Could not connect to Cassandra", e); } return; } // we are in an EE environment, delegate it to the container sessionFuture = executor.submit(cassandraSessionCallable); }
@PreDestroy protected void shutdown() { executorService.submit(() -> { long waitStart = System.currentTimeMillis(); long totalWait = 0; while (activeExecutions.get() > 0 && totalWait < 60000L) { try { Thread.sleep(1000L); } catch (InterruptedException e) { break; } totalWait = System.currentTimeMillis() - waitStart; } try { client.close(); } catch (IOException e) { log.warn("Exception while shutting down HttpClient: " + e.getMessage(), e); } }); }
@Singleton @Startup @LocalBean public class MyTaskManager { @Resource ManagedExecutorService executor; @EJB MyEjb myEjb; @PostConstruct void init() { executor.submit(new MyTask()); } class MyTask implements Runnable { @Override public void run() { ... myEjb.doSomeStuff(); ... } } }
@TransactionAttribute(TransactionAttributeType.NEVER) public void blockingCondaOp(int hostId, CondaOp op, CondaInstallType condaInstallType, MachineType machineType, Project proj, String channelUrl, String lib, String version) throws AppException { Hosts host = em.find(Hosts.class, hostId); AnacondaRepo repo = getRepo(proj, channelUrl, false); PythonDep dep = getDep(repo, machineType, condaInstallType, lib, version, false, false); Future<?> f = kagentExecutorService.submit(new PythonDepsFacade.CondaTask( this.web, proj, host, op, dep)); try { f.get(100, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException ex) { Logger.getLogger(PythonDepsFacade.class.getName()). log(Level.SEVERE, null, ex); } }
/** * Launches a thread per kagent (up to the threadpool max-size limit) that * send a REST * call to the kagent to execute the anaconda command. * * @param op * @param proj * @param arg * @param hosts * @throws AppException */ public void blockingCondaEnvironmentOp(CondaOp op, String proj, String arg, List<Hosts> hosts) throws AppException { List<Future> waiters = new ArrayList<>(); for (Hosts h : hosts) { logger.log(Level.INFO, "Create anaconda enviornment for {0} on {1}", new Object[]{proj, h.getHostIp()}); Future<?> f = kagentExecutorService.submit( new AnacondaTask(this.web, proj, h, op, arg)); waiters.add(f); } for (Future f : waiters) { try { f.get(10, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException ex) { Logger.getLogger(PythonDepsFacade.class.getName()). log(Level.SEVERE, null, ex); } } }
log.info("Reloading " + entry.getKey()); try { Future<Boolean> result = executorService.submit(entry.getValue()); entry.getValue().setReference(result); } catch (Exception e) {
executorService.submit( new Runnable() {
/** * Attempt to async close a query using the executor. If the executor can't accommodate the close then the query will be closed in-line * * @param queryId * non-null queryId */ private void asyncClose(String queryId) { if (queryId != null) { final Principal p = ctx.getCallerPrincipal(); final String closeQueryId = queryId; try { executor.submit(() -> { close(closeQueryId, p); }); } catch (RejectedExecutionException e) { // log only log.warn("close query rejected by executor id=" + closeQueryId + " principal=" + p, e); // do it the old (slow way) close(queryId); } } }
executorService.submit(() -> { try { Solver<Roster> solver = solverFactory.buildSolver();
executorService.submit(() -> { try { Solver<Roster> solver = solverFactory.buildSolver();