diff --git a/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/FlowController.java b/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/FlowController.java index 99d8d6e28a..346e801660 100644 --- a/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/FlowController.java +++ b/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/FlowController.java @@ -756,7 +756,9 @@ public class FlowController implements EventAccess, ControllerServiceProvider, H /** *

- * Creates a new ProcessorNode with the given type and identifier.

+ * Creates a new ProcessorNode with the given type and identifier and initializes it invoking the + * methods annotated with {@link OnAdded}. + *

* * @param type * @param id @@ -766,6 +768,24 @@ public class FlowController implements EventAccess, ControllerServiceProvider, H * instantiated for any reason */ public ProcessorNode createProcessor(final String type, String id) throws ProcessorInstantiationException { + return createProcessor(type, id, true); + } + + /** + *

+ * Creates a new ProcessorNode with the given type and identifier and optionally initializes it. + *

+ * + * @param type the fully qualified Processor class name + * @param id the unique ID of the Processor + * @param firstTimeAdded whether or not this is the first time this Processor is added to the graph. If {@code true}, + * will invoke methods annotated with the {@link OnAdded} annotation. + * @return + * @throws NullPointerException if either arg is null + * @throws ProcessorInstantiationException if the processor cannot be + * instantiated for any reason + */ + public ProcessorNode createProcessor(final String type, String id, final boolean firstTimeAdded) throws ProcessorInstantiationException { id = id.intern(); final Processor processor = instantiateProcessor(type, id); final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory(controllerServiceProvider); @@ -774,12 +794,13 @@ public class FlowController implements EventAccess, ControllerServiceProvider, H final LogRepository logRepository = LogRepositoryFactory.getRepository(id); logRepository.addObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID, LogLevel.WARN, new ProcessorLogObserver(getBulletinRepository(), procNode)); - // TODO: We should only call this the first time that it is added to the graph.... - try (final NarCloseable x = NarCloseable.withNarLoader()) { - ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor); - } catch (final Exception e) { - logRepository.removeObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID); - throw new ProcessorLifeCycleException("Failed to invoke @OnAdded methods of " + procNode.getProcessor(), e); + if ( firstTimeAdded ) { + try (final NarCloseable x = NarCloseable.withNarLoader()) { + ReflectionUtils.invokeMethodsWithAnnotation(OnAdded.class, processor); + } catch (final Exception e) { + logRepository.removeObserver(StandardProcessorNode.BULLETIN_OBSERVER_ID); + throw new ProcessorLifeCycleException("Failed to invoke @OnAdded methods of " + procNode.getProcessor(), e); + } } return procNode; diff --git a/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/StandardFlowSynchronizer.java b/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/StandardFlowSynchronizer.java index ffea644ca2..b60d18711f 100644 --- a/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/StandardFlowSynchronizer.java +++ b/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/controller/StandardFlowSynchronizer.java @@ -594,7 +594,7 @@ public class StandardFlowSynchronizer implements FlowSynchronizer { final List processorNodeList = getChildrenByTagName(processGroupElement, "processor"); for (final Element processorElement : processorNodeList) { final ProcessorDTO processorDTO = FlowFromDOMFactory.getProcessor(processorElement, encryptor); - final ProcessorNode procNode = controller.createProcessor(processorDTO.getType(), processorDTO.getId()); + final ProcessorNode procNode = controller.createProcessor(processorDTO.getType(), processorDTO.getId(), false); processGroup.addProcessor(procNode); updateProcessor(procNode, processorDTO, processGroup, controller); } diff --git a/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/fingerprint/FingerprintFactory.java b/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/fingerprint/FingerprintFactory.java index 9be46a1284..7bdf278373 100644 --- a/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/fingerprint/FingerprintFactory.java +++ b/nar-bundles/framework-bundle/framework/core/src/main/java/org/apache/nifi/fingerprint/FingerprintFactory.java @@ -467,7 +467,7 @@ public final class FingerprintFactory { Processor processor = null; try { if (controller != null) { - processor = controller.createProcessor(className, UUID.randomUUID().toString()).getProcessor(); + processor = controller.createProcessor(className, UUID.randomUUID().toString(), false).getProcessor(); } } catch (ProcessorInstantiationException e) { logger.warn("Unable to create Processor of type {} due to {}; its default properties will be fingerprinted instead of being ignored.", className, e.toString());