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;
}
}
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()));
}
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);
}
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);
}
}
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;
}
Aggregations