Search in sources :

Example 51 with OnScheduled

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

the class TailFile method recoverState.

@OnScheduled
public void recoverState(final ProcessContext context) throws IOException {
    // set isMultiChanging
    isMultiChanging.set(context.getProperty(MODE).getValue().equals(MODE_MULTIFILE.getValue()));
    // set last lookup to now
    lastLookup.set(new Date().getTime());
    // maxAge
    long maxAge = context.getProperty(MAXIMUM_AGE).getValue() == null ? Long.MAX_VALUE : context.getProperty(MAXIMUM_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
    // get list of files to tail
    List<String> filesToTail = new ArrayList<String>();
    if (context.getProperty(MODE).getValue().equals(MODE_MULTIFILE.getValue())) {
        filesToTail.addAll(getFilesToTail(context.getProperty(BASE_DIRECTORY).evaluateAttributeExpressions().getValue(), context.getProperty(FILENAME).evaluateAttributeExpressions().getValue(), context.getProperty(RECURSIVE).asBoolean(), maxAge));
    } else {
        filesToTail.add(context.getProperty(FILENAME).evaluateAttributeExpressions().getValue());
    }
    final Scope scope = getStateScope(context);
    final StateMap stateMap = context.getStateManager().getState(scope);
    if (stateMap.getVersion() == -1L) {
        // state has been cleared or never stored so recover as 'empty state'
        initStates(filesToTail, Collections.emptyMap(), true);
        recoverState(context, filesToTail, Collections.emptyMap());
        return;
    }
    Map<String, String> statesMap = stateMap.toMap();
    if (statesMap.containsKey(TailFileState.StateKeys.FILENAME) && !statesMap.keySet().stream().anyMatch(key -> key.startsWith(MAP_PREFIX))) {
        // If statesMap contains "filename" key without "file.0." prefix,
        // and there's no key with "file." prefix, then
        // it indicates that the statesMap is created with earlier version of NiFi.
        // In this case, we need to migrate the state by adding prefix indexed with 0.
        final Map<String, String> migratedStatesMap = new HashMap<>(statesMap.size());
        for (String key : statesMap.keySet()) {
            migratedStatesMap.put(MAP_PREFIX + "0." + key, statesMap.get(key));
        }
        // LENGTH is added from NiFi 1.1.0. Set the value with using the last position so that we can use existing state
        // to avoid sending duplicated log data after updating NiFi.
        migratedStatesMap.put(MAP_PREFIX + "0." + TailFileState.StateKeys.LENGTH, statesMap.get(TailFileState.StateKeys.POSITION));
        statesMap = Collections.unmodifiableMap(migratedStatesMap);
        getLogger().info("statesMap has been migrated. {}", new Object[] { migratedStatesMap });
    }
    initStates(filesToTail, statesMap, false);
    recoverState(context, filesToTail, statesMap);
}
Also used : Scope(org.apache.nifi.components.state.Scope) HashMap(java.util.HashMap) StateMap(org.apache.nifi.components.state.StateMap) ArrayList(java.util.ArrayList) Date(java.util.Date) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 52 with OnScheduled

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

the class TransformXml method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) {
    final ComponentLog logger = getLogger();
    final Integer cacheSize = context.getProperty(CACHE_SIZE).asInteger();
    final Long cacheTTL = context.getProperty(CACHE_TTL_AFTER_LAST_ACCESS).asTimePeriod(TimeUnit.SECONDS);
    if (cacheSize > 0) {
        CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(cacheSize);
        if (cacheTTL > 0) {
            cacheBuilder = cacheBuilder.expireAfterAccess(cacheTTL, TimeUnit.SECONDS);
        }
        cache = cacheBuilder.build(new CacheLoader<String, Templates>() {

            public Templates load(String path) throws TransformerConfigurationException {
                return newTemplates(context, path);
            }
        });
    } else {
        cache = null;
        logger.warn("Stylesheet cache disabled because cache size is set to 0");
    }
}
Also used : CacheBuilder(com.google.common.cache.CacheBuilder) CacheLoader(com.google.common.cache.CacheLoader) ComponentLog(org.apache.nifi.logging.ComponentLog) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 53 with OnScheduled

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

the class ValidateXml method parseSchema.

@OnScheduled
public void parseSchema(final ProcessContext context) throws IOException, SAXException {
    try {
        final File file = new File(context.getProperty(SCHEMA_FILE).getValue());
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(SCHEMA_LANGUAGE);
        final Schema schema = schemaFactory.newSchema(file);
        this.schemaRef.set(schema);
    } catch (final SAXException e) {
        throw e;
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) FlowFile(org.apache.nifi.flowfile.FlowFile) File(java.io.File) SAXException(org.xml.sax.SAXException) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 54 with OnScheduled

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

the class ListenBeats method onScheduled.

@Override
@OnScheduled
public void onScheduled(ProcessContext context) throws IOException {
    super.onScheduled(context);
    // wanted to ensure charset was already populated here
    beatsEncoder = new BeatsEncoder();
}
Also used : BeatsEncoder(org.apache.nifi.processors.beats.frame.BeatsEncoder) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 55 with OnScheduled

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

the class GetTCP method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) throws ProcessException {
    this.receiveBufferSize = context.getProperty(RECEIVE_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    this.originalServerAddressList = context.getProperty(ENDPOINT_LIST).getValue();
    this.endOfMessageByte = ((byte) context.getProperty(END_OF_MESSAGE_BYTE).asInteger().intValue());
    this.connectionAttemptCount = context.getProperty(CONNECTION_ATTEMPT_COUNT).asInteger();
    this.reconnectInterval = context.getProperty(RECONNECT_INTERVAL).asTimePeriod(TimeUnit.MILLISECONDS);
    this.clientScheduler = new ScheduledThreadPoolExecutor(originalServerAddressList.split(",").length + 1);
    this.clientScheduler.setKeepAliveTime(10, TimeUnit.SECONDS);
    this.clientScheduler.allowCoreThreadTimeOut(true);
    for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = entry.getKey();
        if (descriptor.isDynamic()) {
            this.dynamicAttributes.put(descriptor.getName(), entry.getValue());
        }
    }
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) HashMap(java.util.HashMap) Map(java.util.Map) 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