Search in sources :

Example 46 with OnScheduled

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

the class AbstractMongoProcessor method createClient.

@OnScheduled
public final void createClient(ProcessContext context) throws IOException {
    if (mongoClient != null) {
        closeClient();
    }
    getLogger().info("Creating MongoClient");
    // Set up the client for secure (SSL/TLS communications) if configured to do so
    final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
    final SSLContext sslContext;
    if (sslService != null) {
        final SSLContextService.ClientAuth clientAuth;
        if (StringUtils.isBlank(rawClientAuth)) {
            clientAuth = SSLContextService.ClientAuth.REQUIRED;
        } else {
            try {
                clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
            } catch (final IllegalArgumentException iae) {
                throw new ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
            }
        }
        sslContext = sslService.createSSLContext(clientAuth);
    } else {
        sslContext = null;
    }
    try {
        if (sslContext == null) {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context)));
        } else {
            mongoClient = new MongoClient(new MongoClientURI(getURI(context), getClientOptions(sslContext)));
        }
    } catch (Exception e) {
        getLogger().error("Failed to schedule {} due to {}", new Object[] { this.getClass().getName(), e }, e);
        throw e;
    }
}
Also used : MongoClient(com.mongodb.MongoClient) ProviderCreationException(org.apache.nifi.authentication.exception.ProviderCreationException) SSLContextService(org.apache.nifi.ssl.SSLContextService) MongoClientURI(com.mongodb.MongoClientURI) SSLContext(javax.net.ssl.SSLContext) ProviderCreationException(org.apache.nifi.authentication.exception.ProviderCreationException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 47 with OnScheduled

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

the class PutDatabaseRecord method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) {
    synchronized (this) {
        schemaCache.clear();
    }
    process = new Put<>();
    process.setLogger(getLogger());
    process.initConnection(initConnection);
    process.putFlowFile(putFlowFile);
    process.adjustRoute(RollbackOnFailure.createAdjustRoute(REL_FAILURE, REL_RETRY));
    process.onCompleted((c, s, fc, conn) -> {
        try {
            conn.commit();
        } catch (SQLException e) {
            // Throw ProcessException to rollback process session.
            throw new ProcessException("Failed to commit database connection due to " + e, e);
        }
    });
    process.onFailed((c, s, fc, conn, e) -> {
        try {
            conn.rollback();
        } catch (SQLException re) {
            // Just log the fact that rollback failed.
            // ProcessSession will be rollback by the thrown Exception so don't have to do anything here.
            getLogger().warn("Failed to rollback database connection due to %s", new Object[] { re }, re);
        }
    });
    process.cleanup((c, s, fc, conn) -> {
        // make sure that we try to set the auto commit back to whatever it was.
        if (fc.originalAutoCommit) {
            try {
                conn.setAutoCommit(true);
            } catch (final SQLException se) {
                getLogger().warn("Failed to reset autocommit due to {}", new Object[] { se });
            }
        }
    });
    exceptionHandler = new ExceptionHandler<>();
    exceptionHandler.mapException(s -> {
        try {
            if (s == null) {
                return ErrorTypes.PersistentFailure;
            }
            throw s;
        } catch (IllegalArgumentException | MalformedRecordException | SQLNonTransientException e) {
            return ErrorTypes.InvalidInput;
        } catch (IOException | SQLException e) {
            return ErrorTypes.TemporalFailure;
        } catch (Exception e) {
            return ErrorTypes.UnknownFailure;
        }
    });
    exceptionHandler.adjustError(RollbackOnFailure.createAdjustError(getLogger()));
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLNonTransientException(java.sql.SQLNonTransientException) SQLException(java.sql.SQLException) IOException(java.io.IOException) SQLNonTransientException(java.sql.SQLNonTransientException) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) BatchUpdateException(java.sql.BatchUpdateException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SQLException(java.sql.SQLException) SQLDataException(java.sql.SQLDataException) IOException(java.io.IOException) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 48 with OnScheduled

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

the class PutFTP method determinePrePostSendProperties.

@OnScheduled
public void determinePrePostSendProperties(final ProcessContext context) {
    final Map<Integer, PropertyDescriptor> preDescriptors = new TreeMap<>();
    final Map<Integer, PropertyDescriptor> postDescriptors = new TreeMap<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        final String name = descriptor.getName();
        final Matcher preMatcher = PRE_SEND_CMD_PATTERN.matcher(name);
        if (preMatcher.matches()) {
            final int index = Integer.parseInt(preMatcher.group(1));
            preDescriptors.put(index, descriptor);
        } else {
            final Matcher postMatcher = POST_SEND_CMD_PATTERN.matcher(name);
            if (postMatcher.matches()) {
                final int index = Integer.parseInt(postMatcher.group(1));
                postDescriptors.put(index, descriptor);
            }
        }
    }
    final List<PropertyDescriptor> preDescriptorList = new ArrayList<>(preDescriptors.values());
    final List<PropertyDescriptor> postDescriptorList = new ArrayList<>(postDescriptors.values());
    this.preSendDescriptorRef.set(preDescriptorList);
    this.postSendDescriptorRef.set(postDescriptorList);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 49 with OnScheduled

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

the class QueryRecord method setupQueues.

@OnScheduled
public synchronized void setupQueues(final ProcessContext context) {
    // queue and add as necessary, knowing that the queue already exists.
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (!descriptor.isDynamic()) {
            continue;
        }
        final String sql = context.getProperty(descriptor).evaluateAttributeExpressions().getValue();
        final BlockingQueue<CachedStatement> queue = new LinkedBlockingQueue<>(context.getMaxConcurrentTasks());
        statementQueues.put(sql, queue);
    }
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 50 with OnScheduled

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

the class RouteOnAttribute method onScheduled.

/**
 * When this processor is scheduled, update the dynamic properties into the map
 * for quick access during each onTrigger call
 * @param context ProcessContext used to retrieve dynamic properties
 */
@OnScheduled
public void onScheduled(final ProcessContext context) {
    final Map<Relationship, PropertyValue> newPropertyMap = new HashMap<>();
    for (final PropertyDescriptor descriptor : context.getProperties().keySet()) {
        if (!descriptor.isDynamic()) {
            continue;
        }
        getLogger().debug("Adding new dynamic property: {}", new Object[] { descriptor });
        newPropertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor));
    }
    this.propertyMap = newPropertyMap;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) Relationship(org.apache.nifi.processor.Relationship) DynamicRelationship(org.apache.nifi.annotation.behavior.DynamicRelationship) PropertyValue(org.apache.nifi.components.PropertyValue) 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