/** * Utility to iterate through a collection while checking the cancelled flag. * * @param iterable the collection to iterate through * @param consumer the processor invoked for each element * @param <T> the type of elements being processed */ public <T> void iterateWhileActive(Iterable<T> iterable, Consumer<T> consumer) { for (T obj : iterable) { if (!isActive()) { return; } consumer.accept(obj); } }
@Override public SubContext fork() { TaskContext child = new TaskContext(); child.adapter = adapter; return child; }
@Override public String toString() { return getSystemString(); }
/** * Logs the given message and sets it as current state. * * @param message the message to log * @param args the parameters used to format the message (see {@link Strings#apply(String, Object...)}) */ public void logAsCurrentState(String message, Object... args) { log(message, args); setState(message, args); }
@SuppressWarnings("unchecked") protected void execIterate(Function<E, Boolean> handler, Compiler compiler, Limit limit, boolean nativeLimit, ResultSet rs) throws Exception { TaskContext tc = TaskContext.get(); Set<String> columns = dbs.readColumns(rs); while (rs.next() && tc.isActive()) { if (nativeLimit || limit.nextRow()) { SQLEntity e = makeEntity(descriptor, null, columns, rs); compiler.executeJoinFetches(e, columns, rs); if (!handler.apply((E) e)) { return; } } if (!nativeLimit && !limit.shouldContinue()) { return; } } }
@Override public void run() { try { Watch w = Watch.start(); try { if (ctx == null) { CallContext.initialize(); } else { CallContext.setCurrent(ctx); } TaskContext.get().setSystem(SYSTEM_ASYNC).setSubSystem(category).setJob(String.valueOf(jobNumber)); runnable.run(); promise.success(null); } finally { CallContext.detach(); durationAverage.addValue(w.elapsedMillis()); } } catch (Exception t) { Exceptions.handle(Tasks.LOG, t); promise.fail(t); } }
@Override public void iterate(Function<Row, Boolean> handler, @Nullable Limit limit) throws SQLException { Watch w = Watch.start(); try (ResultSet rs = query.prepareStmt().executeQuery()) { query.avarage.addValue(w.elapsedMillis()); TaskContext tc = TaskContext.get(); processResultSet(handler, limit, rs, tc); } }
/** * Parses the previously supplied input and calls the given consumer for each row. * <p> * Note that this method will close the given input. * * @param consumer the consume to call for each line * @throws IOException if an IO error occures while reading from the given input */ public void execute(Consumer<Values> consumer) throws IOException { try { this.consumer = consumer; TaskContext tc = TaskContext.get(); read(); while (tc.isActive() && !isEOF() && limit.shouldContinue()) { readRow(); consumeNewLine(); } } finally { input.close(); } }
TaskContext ctx = TaskContext.get(); RateLimit rateLimit = RateLimit.timeInterval(1, TimeUnit.SECONDS);
/** * Executes the change against the given database. * * @param db the datbase to change */ public void execute(Database db) { error = null; for (String statement : getSql()) { if (TaskContext.get().isActive()) { try { OMA.LOG.FINE("Executing Schema Update: %s", statement); db.createQuery(statement).executeUpdate(); } catch (SQLException e) { error = e.getMessage(); } } } executed = !isFailed(); }
protected void processResultSet(Function<Row, Boolean> handler, Limit effectiveLimit, ResultSet resultSet, TaskContext taskContext) throws SQLException { while (resultSet.next() && taskContext.isActive()) { Row row = loadIntoRow(resultSet); if (effectiveLimit.nextRow()) { if (!handler.apply(row)) { return; } } if (!effectiveLimit.shouldContinue()) { return; } } }
@Override public void iterate(Function<Row, Boolean> handler, @Nullable Limit limit) throws SQLException { Watch w = Watch.start(); fieldNames = null; try (Connection c = ds.getConnection()) { try (PreparedStatement stmt = createPreparedStatement(c)) { if (stmt == null) { return; } Limit effectiveLimit = limit != null ? limit : Limit.UNLIMITED; applyMaxRows(stmt, effectiveLimit); applyFetchSize(stmt, effectiveLimit); try (ResultSet rs = stmt.executeQuery()) { w.submitMicroTiming(MICROTIMING_KEY, sql); TaskContext tc = TaskContext.get(); processResultSet(handler, effectiveLimit, rs, tc); } } } }
@Override public void log(String message) { Tasks.LOG.INFO(ctx.getSystemString() + ": " + message); }
@Override protected void processResultSet(Function<Row, Boolean> handler, Limit effectiveLimit, ResultSet resultSet, TaskContext taskContext) throws SQLException { while (resultSet.next() && taskContext.isActive()) { Row row = loadIntoRow(resultSet); if (effectiveLimit.nextRow()) { if (!handler.apply(row)) { return; } } if (!effectiveLimit.shouldContinue()) { return; } } }
private void executeScroll(SearchResponse initialSearchResponse, ResultHandler<? super E> handler, EntityDescriptor entityDescriptor) { SearchResponse searchResponse = initialSearchResponse; TaskContext ctx = TaskContext.get(); RateLimit rateLimit = RateLimit.timeInterval(1, TimeUnit.SECONDS); long lastScroll = 0; Limit lim = new Limit(start, limit); while (true) { lastScroll = performScrollMonitoring(lastScroll); for (SearchHit hit : searchResponse.getHits()) { if (!processHit(handler, entityDescriptor, ctx, rateLimit, lim, hit)) { return; } } if (searchResponse.getHits().getHits().length == 0) { return; } searchResponse = scrollFurther(entityDescriptor, searchResponse.getScrollId()); } }
@Override public void trace(String message) { if (Tasks.LOG.isFINE()) { Tasks.LOG.FINE(ctx.getSystemString() + ": " + message); } }
@Override public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { // Delegate to active handlers... for (SAX2DOMHandler handler : activeHandlers) { handler.createElement(name, attributes); } // Start a new handler is necessary NodeHandler handler = handlers.get(name); if (handler != null) { SAX2DOMHandler saxHandler = new SAX2DOMHandler(handler, documentBuilder.newDocument()); saxHandler.createElement(name, attributes); activeHandlers.add(saxHandler); } // Check if the user tried to interrupt parsing.... if (!taskContext.isActive()) { throw new UserInterruptException(); } }
/** * Sets the <tt>Sub-System</tt> component of the <tt>System String</tt> * * @param subSystem the new sub system component to set * @return the task context itself for fluent method calls * @see #getSystemString() */ public TaskContext setSubSystem(String subSystem) { if (Strings.isEmpty(subSystem)) { this.subSystem = GENERIC; } else { this.subSystem = subSystem; } parent.addToMDC(MDC_SYSTEM, getSystemString()); return this; }