List of usage examples for java.util.function Consumer accept
void accept(T t);
From source file:org.apache.pulsar.io.debezium.PulsarDatabaseHistory.java
@Override protected void recoverRecords(Consumer<HistoryRecord> records) { setupClientIfNeeded();//w ww. j av a 2 s.c o m try (Reader<String> historyReader = pulsarClient.newReader(Schema.STRING).topic(topicName) .startMessageId(MessageId.earliest).create()) { log.info("Scanning the database history topic '{}'", topicName); // Read all messages in the topic ... MessageId lastProcessedMessageId = null; // read the topic until the end while (historyReader.hasMessageAvailable()) { Message<String> msg = historyReader.readNext(); try { if (null == lastProcessedMessageId || lastProcessedMessageId.compareTo(msg.getMessageId()) < 0) { if (!isBlank(msg.getValue())) { HistoryRecord recordObj = new HistoryRecord(reader.read(msg.getValue())); if (log.isTraceEnabled()) { log.trace("Recovering database history: {}", recordObj); } if (recordObj == null || !recordObj.isValid()) { log.warn("Skipping invalid database history record '{}'. " + "This is often not an issue, but if it happens repeatedly please check the '{}' topic.", recordObj, topicName); } else { records.accept(recordObj); log.trace("Recovered database history: {}", recordObj); } } lastProcessedMessageId = msg.getMessageId(); } } catch (IOException ioe) { log.error("Error while deserializing history record '{}'", msg.getValue(), ioe); } catch (final Exception e) { throw e; } } log.info("Successfully completed scanning the database history topic '{}'", topicName); } catch (IOException ioe) { log.error("Encountered issues on recovering history records", ioe); throw new RuntimeException("Encountered issues on recovering history records", ioe); } }
From source file:com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinitionImpl.java
private <X> RefinedAttributeDefinition<X> substituteRefinedAttributeDefinition( Supplier<RefinedAttributeDefinition<X>> getter, Consumer<RefinedAttributeDefinition<X>> setter, Supplier<ResourceAttributeDefinition<X>> getterOfOriginal) { RefinedAttributeDefinition<X> value = getter.get(); if (value == null) { ResourceAttributeDefinition original = getterOfOriginal.get(); if (original == null) { return null; }//ww w.j av a2 s . co m value = findAttributeDefinition(original.getName()); setter.accept(value); } return value; }
From source file:org.apache.nifi.processors.standard.Wait.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final ComponentLog logger = getLogger(); // Signal id is computed from attribute 'RELEASE_SIGNAL_IDENTIFIER' with expression language support final PropertyValue signalIdProperty = context.getProperty(RELEASE_SIGNAL_IDENTIFIER); final Integer bufferCount = context.getProperty(WAIT_BUFFER_COUNT).asInteger(); final Map<Relationship, List<FlowFile>> processedFlowFiles = new HashMap<>(); final Function<Relationship, List<FlowFile>> getFlowFilesFor = r -> processedFlowFiles.computeIfAbsent(r, k -> new ArrayList<>()); final AtomicReference<String> targetSignalId = new AtomicReference<>(); final AtomicInteger bufferedCount = new AtomicInteger(0); final List<FlowFile> failedFilteringFlowFiles = new ArrayList<>(); final Supplier<FlowFileFilter.FlowFileFilterResult> acceptResultSupplier = () -> bufferedCount .incrementAndGet() == bufferCount ? ACCEPT_AND_TERMINATE : ACCEPT_AND_CONTINUE; final List<FlowFile> flowFiles = session.get(f -> { final String fSignalId = signalIdProperty.evaluateAttributeExpressions(f).getValue(); // if the computed value is null, or empty, we transfer the FlowFile to failure relationship if (StringUtils.isBlank(fSignalId)) { // We can't penalize f before getting it from session, so keep it in a temporal list. logger.error("FlowFile {} has no attribute for given Release Signal Identifier", new Object[] { f }); failedFilteringFlowFiles.add(f); return ACCEPT_AND_CONTINUE; }/* w w w . j a va 2 s.co m*/ final String targetSignalIdStr = targetSignalId.get(); if (targetSignalIdStr == null) { // This is the first one. targetSignalId.set(fSignalId); return acceptResultSupplier.get(); } if (targetSignalIdStr.equals(fSignalId)) { return acceptResultSupplier.get(); } return REJECT_AND_CONTINUE; }); final String attributeCopyMode = context.getProperty(ATTRIBUTE_COPY_MODE).getValue(); final boolean replaceOriginalAttributes = ATTRIBUTE_COPY_REPLACE.getValue().equals(attributeCopyMode); final AtomicReference<Signal> signalRef = new AtomicReference<>(); final Consumer<FlowFile> transferToFailure = flowFile -> { flowFile = session.penalize(flowFile); getFlowFilesFor.apply(REL_FAILURE).add(flowFile); }; final Consumer<Entry<Relationship, List<FlowFile>>> transferFlowFiles = routedFlowFiles -> { Relationship relationship = routedFlowFiles.getKey(); if (REL_WAIT.equals(relationship)) { final String waitMode = context.getProperty(WAIT_MODE).getValue(); if (WAIT_MODE_KEEP_IN_UPSTREAM.getValue().equals(waitMode)) { // Transfer to self. relationship = Relationship.SELF; } } final List<FlowFile> flowFilesWithSignalAttributes = routedFlowFiles.getValue().stream() .map(f -> copySignalAttributes(session, f, signalRef.get(), replaceOriginalAttributes)) .collect(Collectors.toList()); session.transfer(flowFilesWithSignalAttributes, relationship); }; failedFilteringFlowFiles.forEach(f -> { flowFiles.remove(f); transferToFailure.accept(f); }); if (flowFiles.isEmpty()) { // If there was nothing but failed FlowFiles while filtering, transfer those and end immediately. processedFlowFiles.entrySet().forEach(transferFlowFiles); return; } // the cache client used to interact with the distributed cache final AtomicDistributedMapCacheClient cache = context.getProperty(DISTRIBUTED_CACHE_SERVICE) .asControllerService(AtomicDistributedMapCacheClient.class); final WaitNotifyProtocol protocol = new WaitNotifyProtocol(cache); final String signalId = targetSignalId.get(); final Signal signal; // get notifying signal try { signal = protocol.getSignal(signalId); signalRef.set(signal); } catch (final IOException e) { throw new ProcessException(String.format("Failed to get signal for %s due to %s", signalId, e), e); } String targetCounterName = null; long targetCount = 1; int releasableFlowFileCount = 1; final List<FlowFile> candidates = new ArrayList<>(); for (FlowFile flowFile : flowFiles) { // Set wait start timestamp if it's not set yet String waitStartTimestamp = flowFile.getAttribute(WAIT_START_TIMESTAMP); if (waitStartTimestamp == null) { waitStartTimestamp = String.valueOf(System.currentTimeMillis()); flowFile = session.putAttribute(flowFile, WAIT_START_TIMESTAMP, waitStartTimestamp); } long lWaitStartTimestamp; try { lWaitStartTimestamp = Long.parseLong(waitStartTimestamp); } catch (NumberFormatException nfe) { logger.error("{} has an invalid value '{}' on FlowFile {}", new Object[] { WAIT_START_TIMESTAMP, waitStartTimestamp, flowFile }); transferToFailure.accept(flowFile); continue; } // check for expiration long expirationDuration = context.getProperty(EXPIRATION_DURATION).asTimePeriod(TimeUnit.MILLISECONDS); long now = System.currentTimeMillis(); if (now > (lWaitStartTimestamp + expirationDuration)) { logger.info("FlowFile {} expired after {}ms", new Object[] { flowFile, (now - lWaitStartTimestamp) }); getFlowFilesFor.apply(REL_EXPIRED).add(flowFile); continue; } // If there's no signal yet, then we don't have to evaluate target counts. Return immediately. if (signal == null) { if (logger.isDebugEnabled()) { logger.debug("No release signal found for {} on FlowFile {} yet", new Object[] { signalId, flowFile }); } getFlowFilesFor.apply(REL_WAIT).add(flowFile); continue; } // Fix target counter name and count from current FlowFile, if those are not set yet. if (candidates.isEmpty()) { targetCounterName = context.getProperty(SIGNAL_COUNTER_NAME).evaluateAttributeExpressions(flowFile) .getValue(); try { targetCount = Long.valueOf(context.getProperty(TARGET_SIGNAL_COUNT) .evaluateAttributeExpressions(flowFile).getValue()); } catch (final NumberFormatException e) { transferToFailure.accept(flowFile); logger.error("Failed to parse targetCount when processing {} due to {}", new Object[] { flowFile, e }, e); continue; } try { releasableFlowFileCount = Integer.valueOf(context.getProperty(RELEASABLE_FLOWFILE_COUNT) .evaluateAttributeExpressions(flowFile).getValue()); } catch (final NumberFormatException e) { transferToFailure.accept(flowFile); logger.error("Failed to parse releasableFlowFileCount when processing {} due to {}", new Object[] { flowFile, e }, e); continue; } } // FlowFile is now validated and added to candidates. candidates.add(flowFile); } boolean waitCompleted = false; boolean waitProgressed = false; if (signal != null && !candidates.isEmpty()) { if (releasableFlowFileCount > 1) { signal.releaseCandidatese(targetCounterName, targetCount, releasableFlowFileCount, candidates, released -> getFlowFilesFor.apply(REL_SUCCESS).addAll(released), waiting -> getFlowFilesFor.apply(REL_WAIT).addAll(waiting)); waitProgressed = !getFlowFilesFor.apply(REL_SUCCESS).isEmpty(); } else { // releasableFlowFileCount = 0 or 1 boolean reachedTargetCount = StringUtils.isBlank(targetCounterName) ? signal.isTotalCountReached(targetCount) : signal.isCountReached(targetCounterName, targetCount); if (reachedTargetCount) { if (releasableFlowFileCount == 0) { getFlowFilesFor.apply(REL_SUCCESS).addAll(candidates); } else { // releasableFlowFileCount = 1 getFlowFilesFor.apply(REL_SUCCESS).add(candidates.remove(0)); getFlowFilesFor.apply(REL_WAIT).addAll(candidates); // If releasableFlowFileCount == 0, leave signal as it is, // so that any number of FlowFile can be released as long as target count condition matches. waitCompleted = true; } } else { getFlowFilesFor.apply(REL_WAIT).addAll(candidates); } } } // Transfer FlowFiles. processedFlowFiles.entrySet().forEach(transferFlowFiles); // Update signal if needed. try { if (waitCompleted) { protocol.complete(signalId); } else if (waitProgressed) { protocol.replace(signal); } } catch (final IOException e) { session.rollback(); throw new ProcessException( String.format("Unable to communicate with cache while updating %s due to %s", signalId, e), e); } }
From source file:com.ethercamp.harmony.service.WalletService.java
private void checkForChangesInWallet(BlockSummary blockSummary, List<Transaction> transactions, Consumer<TransactionInfo> sendHandler, Consumer<TransactionInfo> receiveHandler) { final Set<ByteArrayWrapper> subscribed = addresses.keySet().stream().map(a -> cleanAddress(a)) .flatMap(a -> {//from ww w .j a v a 2 s .c o m try { return Stream.of(new ByteArrayWrapper(Hex.decode(a))); } catch (Exception e) { log.error("Problem getting bytes representation from " + a); return Stream.empty(); } }).collect(Collectors.toSet()); final List<Transaction> confirmedTransactions = transactions.stream() .filter(transaction -> isSetContains(subscribed, transaction.getReceiveAddress()) || isSetContains(subscribed, transaction.getSender())) .collect(Collectors.toList()); confirmedTransactions.forEach(transaction -> { final String hash = toHexString(transaction.getHash()); final BigInteger amount = ByteUtil.bytesToBigInteger(transaction.getValue()); final boolean hasSender = isSetContains(subscribed, transaction.getSender()); final boolean hasReceiver = isSetContains(subscribed, transaction.getReceiveAddress()); log.debug("Handle transaction hash:" + hash + ", hasSender:" + hasSender + ", amount:" + amount); if (hasSender) { sendHandler .accept(new TransactionInfo(hash, amount, hasSender, toHexString(transaction.getSender()))); } if (hasReceiver) { receiveHandler.accept( new TransactionInfo(hash, amount, hasSender, toHexString(transaction.getReceiveAddress()))); } }); // check if balance changes due to block reward final Optional<ByteArrayWrapper> coinbase = Optional.ofNullable(blockSummary) .map(bs -> new ByteArrayWrapper(bs.getBlock().getCoinbase())); final Boolean matchCoinbase = coinbase.map(c -> subscribed.stream().anyMatch(a -> c.equals(a))) .orElse(false); final boolean needToSendUpdate = matchCoinbase || !confirmedTransactions.isEmpty(); if (needToSendUpdate) { // update wallet if transactions are related to wallet addresses clientMessageService.sendToTopic("/topic/getWalletInfo", getWalletInfo()); } }
From source file:org.apache.pulsar.broker.service.BrokerService.java
/** * Allows a listener to listen on update of {@link ServiceConfiguration} change, so listener can take appropriate * action if any specific config-field value has been changed. * </p>//from ww w . j a v a 2 s . c om * On notification, listener should first check if config value has been changed and after taking appropriate * action, listener should update config value with new value if it has been changed (so, next time listener can * compare values on configMap change). * @param <T> * * @param configKey * : configuration field name * @param listener * : listener which takes appropriate action on config-value change */ public <T> void registerConfigurationListener(String configKey, Consumer<T> listener) { configRegisteredListeners.put(configKey, listener); dynamicConfigurationCache.registerListener(new ZooKeeperCacheListener<Map<String, String>>() { @SuppressWarnings("unchecked") @Override public void onUpdate(String path, Map<String, String> data, Stat stat) { if (BROKER_SERVICE_CONFIGURATION_PATH.equalsIgnoreCase(path) && data != null && data.containsKey(configKey)) { log.info("Updating configuration {}/{}", configKey, data.get(configKey)); listener.accept( (T) FieldParser.value(data.get(configKey), dynamicConfigurationMap.get(configKey))); } } }); }
From source file:org.apache.pulsar.broker.service.BrokerService.java
private void updateDynamicServiceConfiguration() { try {/*w w w . j a va 2 s .c o m*/ // create dynamic-config znode if not present if (pulsar.getZkClient().exists(BROKER_SERVICE_CONFIGURATION_PATH, false) == null) { try { byte[] data = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(Maps.newHashMap()); ZkUtils.createFullPathOptimistic(pulsar.getZkClient(), BROKER_SERVICE_CONFIGURATION_PATH, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException e) { // Ok } } Optional<Map<String, String>> data = dynamicConfigurationCache.get(BROKER_SERVICE_CONFIGURATION_PATH); if (data.isPresent() && data.get() != null) { data.get().forEach((key, value) -> { try { Field field = ServiceConfiguration.class.getDeclaredField(key); if (field != null && field.isAnnotationPresent(FieldContext.class)) { field.setAccessible(true); field.set(pulsar().getConfiguration(), FieldParser.value(value, field)); log.info("Successfully updated {}/{}", key, value); } } catch (Exception e) { log.warn("Failed to update service configuration {}/{}, {}", key, value, e.getMessage()); } }); } } catch (Exception e) { log.warn("Failed to read zookeeper path [{}]:", BROKER_SERVICE_CONFIGURATION_PATH, e); } // register a listener: it updates field value and triggers appropriate registered field-listener only if // field's value has been changed so, registered doesn't have to update field value in ServiceConfiguration dynamicConfigurationCache.registerListener(new ZooKeeperCacheListener<Map<String, String>>() { @SuppressWarnings("unchecked") @Override public void onUpdate(String path, Map<String, String> data, Stat stat) { if (BROKER_SERVICE_CONFIGURATION_PATH.equalsIgnoreCase(path) && data != null) { data.forEach((configKey, value) -> { Field configField = dynamicConfigurationMap.get(configKey); Object newValue = FieldParser.value(data.get(configKey), configField); if (configField != null) { Consumer listener = configRegisteredListeners.get(configKey); try { Object existingValue = configField.get(pulsar.getConfiguration()); configField.set(pulsar.getConfiguration(), newValue); log.info("Successfully updated configuration {}/{}", configKey, data.get(configKey)); if (listener != null && !existingValue.equals(newValue)) { listener.accept(newValue); } } catch (Exception e) { log.error("Failed to update config {}/{}", configKey, newValue); } } else { log.error("Found non-dynamic field in dynamicConfigMap {}/{}", configKey, newValue); } }); } } }); }
From source file:org.codice.ddf.spatial.ogc.csw.catalog.common.source.AbstractCswSource.java
/** * Reinitializes the CswSource when there is a configuration change. Otherwise, it checks with * the server to see if any capabilities have changed. * * @param configuration The configuration with which to connect to the server *///w ww . java 2s . c o m public void refresh(Map<String, Object> configuration) { LOGGER.debug("{}: Entering refresh()", cswSourceConfiguration.getId()); if (configuration == null || configuration.isEmpty()) { LOGGER.error("Received null or empty configuration during refresh for {}: {}", this.getClass().getSimpleName(), cswSourceConfiguration.getId()); return; } // Set Blank Defaults String spatialFilter = (String) configuration.get(FORCE_SPATIAL_FILTER_PROPERTY); if (StringUtils.isBlank(spatialFilter)) { spatialFilter = NO_FORCE_SPATIAL_FILTER; } setForceSpatialFilter(spatialFilter); String currentContentTypeMapping = (String) configuration.get(Metacard.CONTENT_TYPE); if (StringUtils.isBlank(currentContentTypeMapping)) { cswSourceConfiguration.putMetacardCswMapping(Metacard.CONTENT_TYPE, CswRecordMetacardType.CSW_TYPE); } //if the event service address has changed attempt to remove the subscription before changing to the new event service address if (cswSourceConfiguration.getEventServiceAddress() != null && cswSourceConfiguration.isRegisterForEvents() && !cswSourceConfiguration.getEventServiceAddress() .equals(configuration.get(EVENT_SERVICE_ADDRESS))) { removeEventServiceSubscription(); } // Filter Configuration Map Map<String, Object> filteredConfiguration = filter(configuration); // Run Consumers from Filtered Configuration Map for (Map.Entry<String, Object> entry : filteredConfiguration.entrySet()) { String key = entry.getKey(); Consumer consumer = consumerMap.get(key); if (consumer != null) { LOGGER.debug("Refreshing Configuration : {} with : {}", key, entry.getValue()); consumer.accept(entry.getValue()); } } configureCswSource(); initClientFactory(); configureEventService(); }
From source file:org.kie.workbench.common.forms.migration.tool.pipelines.basic.impl.adapters.fields.AbstractFieldAdapter.java
@Override public void parseField(Field originalField, FormMigrationSummary formSummary, FormDefinition newFormDefinition, Consumer<LayoutComponent> layoutElementConsumer) { FieldDefinition fieldDefinition = getFieldDefinition(originalField); fieldDefinition.setId(String.valueOf(originalField.getId())); fieldDefinition.setName(originalField.getFieldName()); fieldDefinition.setLabel(lookupI18nValue(originalField.getLabel())); fieldDefinition.setHelpMessage(lookupI18nValue(originalField.getTitle())); fieldDefinition.setStandaloneClassName( StringUtils.defaultIfBlank(originalField.getBag(), originalField.getFieldType().getFieldClass())); fieldDefinition.setReadOnly(originalField.getReadonly()); fieldDefinition.setRequired(originalField.getFieldRequired()); String binding = StringUtils.defaultString( StringUtils.defaultIfBlank(originalField.getInputBinding(), originalField.getOutputBinding())); if (!StringUtils.isEmpty(binding)) { if (binding.contains("/")) { binding = binding.substring(binding.indexOf("/") + 1); }/*from w w w.j av a2 s . com*/ ModelPropertyImpl property = new ModelPropertyImpl(binding, fieldDefinition.getFieldTypeInfo()); newFormDefinition.getModel().getProperties().add(property); } fieldDefinition.setBinding(binding); if (fieldDefinition instanceof HasPlaceHolder) { ((HasPlaceHolder) fieldDefinition).setPlaceHolder(fieldDefinition.getLabel()); } newFormDefinition.getFields().add(fieldDefinition); LayoutComponent component = new LayoutComponent(DRAGGABLE_TYPE); component.addProperty(FormLayoutComponent.FORM_ID, newFormDefinition.getId()); component.addProperty(FormLayoutComponent.FIELD_ID, fieldDefinition.getId()); layoutElementConsumer.accept(component); }
From source file:org.nmdp.service.epitope.task.URLProcessor.java
private long refreshFromUrl(URL url, Consumer<InputStream> consumer, long lastModified) throws MalformedURLException, IOException { logger.debug("trying url: " + url); long sourceLastModified = 0; final URLConnection urlConnection = url.openConnection(); if (url.getProtocol().equalsIgnoreCase("ftp")) { sourceLastModified = getFtpLastModifiedTime(url); } else {//from w w w. j av a 2 s .c o m sourceLastModified = urlConnection.getLastModified(); } String sourceLastModifiedStr = dateFormat.format(sourceLastModified); String lastModifiedStr = dateFormat.format(lastModified); if (sourceLastModified == 0) { logger.warn("resource has no modification date, forcing refresh..."); } else if (sourceLastModified < lastModified) { logger.warn("resource is older than last modification date (source: " + sourceLastModifiedStr + ", last: " + lastModifiedStr + "), leaving it"); return lastModified; } else if (sourceLastModified == lastModified) { logger.debug("resource is current (modified: " + lastModifiedStr + ")"); return lastModified; } else { logger.info("resource is newer than last modification date, refreshing (source: " + sourceLastModifiedStr + ", cache: " + lastModifiedStr + ")"); } urlConnection.connect(); InputStream is = urlConnection.getInputStream(); if (unzip) { ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); logger.info("unzipping, got zip entry: {}", entry.getName()); is = zis; } consumer.accept(is); is.close(); return sourceLastModified; }
From source file:org.jboss.pnc.buildagent.server.BootstrapUndertowBuildAgentHandlers.java
public void bootstrap(final Consumer<Boolean> completionHandler) throws BuildAgentException { String servletPath = bindPath + "/servlet"; String socketPath = bindPath + "/socket"; String httpPath = bindPath + "/"; DeploymentInfo servletBuilder = deployment() .setClassLoader(BootstrapUndertowBuildAgentHandlers.class.getClassLoader()) .setContextPath(servletPath).setDeploymentName("ROOT.war") .addServlets(servlet("WelcomeServlet", Welcome.class).addMapping("/"), servlet("TerminalServlet", Terminal.class).addMapping("/terminal/*"), servlet("UploaderServlet", Upload.class).addMapping("/upload/*"), servlet("DownloaderServlet", Download.class).addMapping("/download/*")); DeploymentManager manager = defaultContainer().addDeployment(servletBuilder); manager.deploy();// w ww. ja v a2 s . co m HttpHandler servletHandler = null; try { servletHandler = manager.start(); } catch (ServletException e) { throw new BuildAgentException("Cannot deploy servlets.", e); } PathHandler pathHandler = Handlers.path().addPrefixPath(servletPath, servletHandler) .addPrefixPath(socketPath, exchange -> BootstrapUndertowBuildAgentHandlers.this.handleWebSocketRequests(exchange, socketPath)) .addPrefixPath(httpPath, exchange -> BootstrapUndertowBuildAgentHandlers.this .handleHttpRequests(exchange, httpPath)); server = Undertow.builder().addHttpListener(getPort(), getHost()).setHandler(pathHandler).build(); server.start(); completionHandler.accept(true); }