From 4a43e81343b4eaf967a7e5a5417fd7bb35226503 Mon Sep 17 00:00:00 2001 From: Brian Ghigiarelli Date: Thu, 23 Jul 2015 22:52:35 -0400 Subject: [PATCH] [NIFI-784] Moving dynamic propertyMap to onScheduled method and updating logger statements to use lazy evaluation with String formatter --- .../processors/standard/RouteOnAttribute.java | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/RouteOnAttribute.java b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/RouteOnAttribute.java index 69107ddd5a..52dc80b129 100644 --- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/RouteOnAttribute.java +++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/RouteOnAttribute.java @@ -27,12 +27,14 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import org.apache.nifi.annotation.behavior.DynamicProperty; +import org.apache.nifi.annotation.behavior.DynamicRelationship; import org.apache.nifi.annotation.behavior.EventDriven; import org.apache.nifi.annotation.behavior.SideEffectFree; import org.apache.nifi.annotation.behavior.SupportsBatching; import org.apache.nifi.annotation.documentation.CapabilityDescription; -import org.apache.nifi.annotation.behavior.DynamicRelationship; import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.annotation.lifecycle.OnUnscheduled; import org.apache.nifi.components.AllowableValue; import org.apache.nifi.components.PropertyDescriptor; import org.apache.nifi.components.PropertyValue; @@ -103,6 +105,8 @@ public class RouteOnAttribute extends AbstractProcessor { private volatile String configuredRouteStrategy = ROUTE_STRATEGY.getDefaultValue(); private volatile Set dynamicPropertyNames = new HashSet<>(); + private final Map propertyMap = new HashMap<>(); + @Override protected void init(final ProcessorInitializationContext context) { final Set set = new HashSet<>(); @@ -166,6 +170,27 @@ public class RouteOnAttribute extends AbstractProcessor { this.relationships.set(newRelationships); } + /** + * When this processor is + * @param context + */ + @OnScheduled + public void onScheduled(final ProcessContext context) { + for (final PropertyDescriptor descriptor : context.getProperties().keySet()) { + if (!descriptor.isDynamic()) { + continue; + } + getLogger().debug("Adding new dynamic property: {}", new Object[]{descriptor}); + propertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor)); + } + } + + @OnUnscheduled + public void onUnscheduled(final ProcessContext context) { + getLogger().debug("Clearing propertyMap"); + propertyMap.clear(); + } + @Override public void onTrigger(final ProcessContext context, final ProcessSession session) { FlowFile flowFile = session.get(); @@ -174,14 +199,6 @@ public class RouteOnAttribute extends AbstractProcessor { } final ProcessorLog logger = getLogger(); - final Map propertyMap = new HashMap<>(); - for (final PropertyDescriptor descriptor : context.getProperties().keySet()) { - if (!descriptor.isDynamic()) { - continue; - } - - propertyMap.put(new Relationship.Builder().name(descriptor.getName()).build(), context.getProperty(descriptor)); - } final Set matchingRelationships = new HashSet<>(); for (final Map.Entry entry : propertyMap.entrySet()) { @@ -216,7 +233,7 @@ public class RouteOnAttribute extends AbstractProcessor { } if (destinationRelationships.isEmpty()) { - logger.info(this + " routing " + flowFile + " to unmatched"); + logger.info("{} routing {} to unmatched", new Object[]{ this, flowFile }); flowFile = session.putAttribute(flowFile, ROUTE_ATTRIBUTE_KEY, REL_NO_MATCH.getName()); session.getProvenanceReporter().route(flowFile, REL_NO_MATCH); session.transfer(flowFile, REL_NO_MATCH); @@ -236,7 +253,7 @@ public class RouteOnAttribute extends AbstractProcessor { // now transfer any clones generated for (final Map.Entry entry : transferMap.entrySet()) { - logger.info(this + " cloned " + flowFile + " into " + entry.getValue() + " and routing clone to relationship " + entry.getKey()); + logger.info("{} cloned {} into {} and routing clone to relationship {}", new Object[]{ this, flowFile, entry.getValue(), entry.getKey() }); FlowFile updatedFlowFile = session.putAttribute(entry.getValue(), ROUTE_ATTRIBUTE_KEY, entry.getKey().getName()); session.getProvenanceReporter().route(updatedFlowFile, entry.getKey()); session.transfer(updatedFlowFile, entry.getKey());