Search in sources :

Example 16 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class PutRiemann method onScheduled.

@OnScheduled
public void onScheduled(ProcessContext context) throws ProcessException {
    if (batchSize == -1) {
        batchSize = context.getProperty(BATCH_SIZE).asInteger();
    }
    if (riemannClient == null || !riemannClient.isConnected()) {
        transport = Transport.valueOf(context.getProperty(TRANSPORT_PROTOCOL).getValue());
        String host = context.getProperty(RIEMANN_HOST).getValue().trim();
        int port = context.getProperty(RIEMANN_PORT).asInteger();
        writeTimeout = context.getProperty(TIMEOUT).asLong();
        RiemannClient client = null;
        try {
            switch(transport) {
                case TCP:
                    client = RiemannClient.tcp(host, port);
                    break;
                case UDP:
                    client = RiemannClient.udp(host, port);
                    break;
            }
            client.connect();
            riemannClient = client;
        } catch (IOException e) {
            if (client != null) {
                client.close();
            }
            context.yield();
            throw new ProcessException(String.format("Unable to connect to Riemann [%s:%d] (%s)\n%s", host, port, transport, e.getMessage()));
        }
    }
    if (customAttributes.size() == 0) {
        for (Map.Entry<PropertyDescriptor, String> property : context.getProperties().entrySet()) {
            // only custom defined properties
            if (!getSupportedPropertyDescriptors().contains(property.getKey())) {
                customAttributes.add(property.getKey());
            }
        }
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) RiemannClient(com.aphyr.riemann.client.RiemannClient) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) IOException(java.io.IOException) Map(java.util.Map) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 17 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class ExecuteScript method setup.

/**
 * Performs setup operations when the processor is scheduled to run. This includes evaluating the processor's
 * properties, as well as reloading the script (from file or the "Script Body" property)
 *
 * @param context the context in which to perform the setup operations
 */
@OnScheduled
public void setup(final ProcessContext context) {
    scriptingComponentHelper.setupVariables(context);
    // Create a script engine for each possible task
    int maxTasks = context.getMaxConcurrentTasks();
    scriptingComponentHelper.setup(maxTasks, getLogger());
    scriptToRun = scriptingComponentHelper.getScriptBody();
    try {
        if (scriptToRun == null && scriptingComponentHelper.getScriptPath() != null) {
            try (final FileInputStream scriptStream = new FileInputStream(scriptingComponentHelper.getScriptPath())) {
                scriptToRun = IOUtils.toString(scriptStream, Charset.defaultCharset());
            }
        }
    } catch (IOException ioe) {
        throw new ProcessException(ioe);
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 18 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class SiteToSiteProvenanceReportingTask method onScheduled.

@OnScheduled
public void onScheduled(final ConfigurationContext context) throws IOException {
    consumer = new ProvenanceEventConsumer();
    consumer.setStartPositionValue(context.getProperty(START_POSITION).getValue());
    consumer.setBatchSize(context.getProperty(BATCH_SIZE).asInteger());
    consumer.setLogger(getLogger());
    // initialize component type filtering
    consumer.setComponentTypeRegex(context.getProperty(FILTER_COMPONENT_TYPE).getValue());
    consumer.setComponentTypeRegexExclude(context.getProperty(FILTER_COMPONENT_TYPE_EXCLUDE).getValue());
    final String[] targetEventTypes = StringUtils.stripAll(StringUtils.split(context.getProperty(FILTER_EVENT_TYPE).getValue(), ','));
    if (targetEventTypes != null) {
        for (String type : targetEventTypes) {
            try {
                consumer.addTargetEventType(ProvenanceEventType.valueOf(type));
            } catch (Exception e) {
                getLogger().warn(type + " is not a correct event type, removed from the filtering.");
            }
        }
    }
    final String[] targetEventTypesExclude = StringUtils.stripAll(StringUtils.split(context.getProperty(FILTER_EVENT_TYPE_EXCLUDE).getValue(), ','));
    if (targetEventTypesExclude != null) {
        for (String type : targetEventTypesExclude) {
            try {
                consumer.addTargetEventTypeExclude(ProvenanceEventType.valueOf(type));
            } catch (Exception e) {
                getLogger().warn(type + " is not a correct event type, removed from the exclude filtering.");
            }
        }
    }
    // initialize component ID filtering
    final String[] targetComponentIds = StringUtils.stripAll(StringUtils.split(context.getProperty(FILTER_COMPONENT_ID).getValue(), ','));
    if (targetComponentIds != null) {
        consumer.addTargetComponentId(targetComponentIds);
    }
    final String[] targetComponentIdsExclude = StringUtils.stripAll(StringUtils.split(context.getProperty(FILTER_COMPONENT_ID_EXCLUDE).getValue(), ','));
    if (targetComponentIdsExclude != null) {
        consumer.addTargetComponentIdExclude(targetComponentIdsExclude);
    }
    consumer.setScheduled(true);
}
Also used : ProvenanceEventConsumer(org.apache.nifi.reporting.util.provenance.ProvenanceEventConsumer) ProcessException(org.apache.nifi.processor.exception.ProcessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 19 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class UpdateRecord method createRecordPaths.

@OnScheduled
public void createRecordPaths(final ProcessContext context) {
    recordPathCache = new RecordPathCache(context.getProperties().size() * 2);
    final List<String> recordPaths = new ArrayList<>(context.getProperties().size() - 2);
    for (final PropertyDescriptor property : context.getProperties().keySet()) {
        if (property.isDynamic()) {
            recordPaths.add(property.getName());
        }
    }
    this.recordPaths = recordPaths;
}
Also used : RecordPathCache(org.apache.nifi.record.path.util.RecordPathCache) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 20 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class DistributeLoad method createWeightedList.

@OnScheduled
public void createWeightedList(final ProcessContext context) {
    final Map<Integer, Integer> weightings = new LinkedHashMap<>();
    String distStrat = context.getProperty(DISTRIBUTION_STRATEGY).getValue();
    if (distStrat.equals(STRATEGY_LOAD_DISTRIBUTION_SERVICE)) {
        String hostNamesValue = context.getProperty(HOSTNAMES).getValue();
        String[] hostNames = hostNamesValue.split("(?:,+|;+|\\s+)");
        Set<String> hostNameSet = new HashSet<>();
        for (String hostName : hostNames) {
            if (StringUtils.isNotBlank(hostName)) {
                hostNameSet.add(hostName);
            }
        }
        LoadDistributionService svc = context.getProperty(LOAD_DISTRIBUTION_SERVICE_TEMPLATE).asControllerService(LoadDistributionService.class);
        myListener = new LoadDistributionListener() {

            @Override
            public void update(Map<String, Integer> loadInfo) {
                for (Relationship rel : relationshipsRef.get()) {
                    String hostname = rel.getDescription();
                    Integer weight = 1;
                    if (loadInfo.containsKey(hostname)) {
                        weight = loadInfo.get(hostname);
                    }
                    weightings.put(Integer.decode(rel.getName()), weight);
                }
                updateWeightedRelationships(weightings);
            }
        };
        Map<String, Integer> loadInfo = svc.getLoadDistribution(hostNameSet, myListener);
        for (Relationship rel : relationshipsRef.get()) {
            String hostname = rel.getDescription();
            Integer weight = 1;
            if (loadInfo.containsKey(hostname)) {
                weight = loadInfo.get(hostname);
            }
            weightings.put(Integer.decode(rel.getName()), weight);
        }
    } else {
        final int numRelationships = context.getProperty(NUM_RELATIONSHIPS).asInteger();
        for (int i = 1; i <= numRelationships; i++) {
            weightings.put(i, 1);
        }
        for (final PropertyDescriptor propDesc : context.getProperties().keySet()) {
            if (!this.properties.contains(propDesc)) {
                final int relationship = Integer.parseInt(propDesc.getName());
                final int weighting = context.getProperty(propDesc).asInteger();
                weightings.put(relationship, weighting);
            }
        }
    }
    updateWeightedRelationships(weightings);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) LinkedHashMap(java.util.LinkedHashMap) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) LoadDistributionService(org.apache.nifi.loading.LoadDistributionService) LoadDistributionListener(org.apache.nifi.loading.LoadDistributionListener) HashSet(java.util.HashSet) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Aggregations

OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)59 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)12 ProcessException (org.apache.nifi.processor.exception.ProcessException)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 ComponentLog (org.apache.nifi.logging.ComponentLog)8 SSLContextService (org.apache.nifi.ssl.SSLContextService)7 Map (java.util.Map)6 SSLContext (javax.net.ssl.SSLContext)6 StateMap (org.apache.nifi.components.state.StateMap)5 FileInputStream (java.io.FileInputStream)4 PropertyValue (org.apache.nifi.components.PropertyValue)4 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)4 InetAddress (java.net.InetAddress)3 CacheLoader (com.google.common.cache.CacheLoader)2 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)2 File (java.io.File)2 InputStream (java.io.InputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 NetworkInterface (java.net.NetworkInterface)2