org.apache.nifi.annotation.lifecycle.OnStopped Java Examples

The following examples show how to use org.apache.nifi.annotation.lifecycle.OnStopped. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReportingTaskWrapper.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void run() {
    scheduleState.incrementActiveThreadCount();
    try (final NarCloseable narCloseable = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
        taskNode.getReportingTask().onTrigger(taskNode.getReportingContext());
    } catch (final Throwable t) {
        final ComponentLog componentLog = new SimpleProcessLogger(taskNode.getIdentifier(), taskNode.getReportingTask());
        componentLog.error("Error running task {} due to {}", new Object[]{taskNode.getReportingTask(), t.toString()});
        if (componentLog.isDebugEnabled()) {
            componentLog.error("", t);
        }
    } finally {
        try {
            // if the reporting task is no longer scheduled to run and this is the last thread,
            // invoke the OnStopped methods
            if (!scheduleState.isScheduled() && scheduleState.getActiveThreadCount() == 1 && scheduleState.mustCallOnStoppedMethods()) {
                try (final NarCloseable x = NarCloseable.withComponentNarLoader(taskNode.getReportingTask().getClass(), taskNode.getIdentifier())) {
                    ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, taskNode.getReportingTask(), taskNode.getConfigurationContext());
                }
            }
        } finally {
            scheduleState.decrementActiveThreadCount();
        }
    }
}
 
Example #2
Source File: AbstractKafkaProcessor.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Will call {@link Closeable#close()} on the target resource after which
 * the target resource will be set to null. Should only be called when there
 * are no more threads being executed on this processor or when it has been
 * verified that only a single thread remains.
 *
 * @see KafkaPublisher
 */
@OnStopped
public void close() {
    if (this.taskCounter.get() == 0) {
        try {
            if (this.kafkaResource != null) {
                try {
                    this.kafkaResource.close();
                } catch (Exception e) {
                    this.getLogger().warn("Failed while closing " + this.kafkaResource, e);
                }
            }
        } finally {
            this.kafkaResource = null;
        }
    }
}
 
Example #3
Source File: ListenHTTP.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnStopped
public void shutdownHttpServer() {
    final StreamThrottler throttler = throttlerRef.getAndSet(null);
    if(throttler != null) {
        try {
            throttler.close();
        } catch (IOException e) {
            getLogger().error("Failed to close StreamThrottler", e);
        }
    }

    final Server toShutdown = this.server;
    if (toShutdown == null) {
        return;
    }

    shutdownHttpServer(toShutdown);
}
 
Example #4
Source File: StandardProcessScheduler.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private synchronized void stopConnectable(final Connectable connectable) {
    final ScheduleState state = getScheduleState(requireNonNull(connectable));
    if (!state.isScheduled()) {
        return;
    }

    state.setScheduled(false);
    getSchedulingAgent(connectable).unschedule(connectable, state);

    if (!state.isScheduled() && state.getActiveThreadCount() == 0 && state.mustCallOnStoppedMethods()) {
        final ConnectableProcessContext processContext = new ConnectableProcessContext(connectable, encryptor, getStateManager(connectable.getIdentifier()));
        try (final NarCloseable x = NarCloseable.withComponentNarLoader(connectable.getClass(), connectable.getIdentifier())) {
            ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, connectable, processContext);
        }
    }
}
 
Example #5
Source File: GetAzureEventHub.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnStopped
public void tearDown() throws ProcessException {
    for (final PartitionReceiver receiver : partitionToReceiverMap.values()) {
        if (null != receiver) {
            receiver.close();
        }
    }

    partitionToReceiverMap.clear();
    try {
        if (null != eventHubClient) {
            eventHubClient.closeSync();
        }
    } catch (final ServiceBusException e) {
        throw new ProcessException(e);
    }
}
 
Example #6
Source File: ConsumeWindowsEventLog.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Cleanup
 */
@OnStopped
public void stop() {
    if (isSubscribed()) {
        wEvtApi.EvtClose(subscriptionHandle);
    }
    subscriptionHandle = null;
    evtSubscribeCallback = null;
    if (!renderedXMLs.isEmpty()) {
        if (sessionFactory != null) {
            getLogger().info("Finishing processing leftover events");
            ProcessSession session = sessionFactory.createSession();
            processQueue(session);
        } else {
            throw new ProcessException("Stopping the processor but there is no ProcessSessionFactory stored and there are messages in the internal queue. Removing the processor now will " +
                    "clear the queue but will result in DATA LOSS. This is normally due to starting the processor, receiving events and stopping before the onTrigger happens. The messages " +
                    "in the internal queue cannot finish processing until until the processor is triggered to run.");
        }
    }
    sessionFactory = null;
    provenanceUri = null;
    renderedXMLs = null;
}
 
Example #7
Source File: ExecuteFlumeSource.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnStopped
public void stopped() {
    if (source instanceof PollableSource) {
        source.stop();
    } else {
        EventDrivenSourceRunner runner = runnerRef.get();
        if (runner != null) {
            runner.stop();
            runnerRef.compareAndSet(runner, null);
        }

        NifiSessionFactoryChannel eventDrivenSourceChannel = eventDrivenSourceChannelRef.get();
        if (eventDrivenSourceChannel != null) {
            eventDrivenSourceChannel.stop();
            eventDrivenSourceChannelRef.compareAndSet(eventDrivenSourceChannel, null);
        }
    }
    sessionFactoryRef.set(null);
}
 
Example #8
Source File: ConsumeWindowsEventLogTest.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStopProcessesQueue() throws InvocationTargetException, IllegalAccessException {
    testRunner.run(1, false);

    List<String> eventXmls = Arrays.asList("one", "two", "three");
    for (WinNT.HANDLE eventHandle : mockEventHandles(wEvtApi, kernel32, eventXmls)) {
        getRenderingCallback().onEvent(WEvtApi.EvtSubscribeNotifyAction.DELIVER, null, eventHandle);
    }

    ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, evtSubscribe, testRunner.getProcessContext());

    List<MockFlowFile> flowFilesForRelationship = testRunner.getFlowFilesForRelationship(ConsumeWindowsEventLog.REL_SUCCESS);
    assertEquals(eventXmls.size(), flowFilesForRelationship.size());
    for (int i = 0; i < eventXmls.size(); i++) {
        flowFilesForRelationship.get(i).assertContentEquals(eventXmls.get(i));
    }
}
 
Example #9
Source File: GetKafka.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnStopped
public void shutdownConsumer() {
    this.consumerStreamsReady.set(false);
    if (consumer != null) {
        try {
            consumer.commitOffsets();
        } finally {
            consumer.shutdown();
        }
    }
    if (this.executor != null) {
        this.executor.shutdown();
        try {
            if (!this.executor.awaitTermination(30000, TimeUnit.MILLISECONDS)) {
                this.executor.shutdownNow();
                getLogger().warn("Executor did not stop in 30 sec. Terminated.");
            }
            this.executor = null;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
 
Example #10
Source File: SpringContextProcessor.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Will close the 'exchanger' which in turn will close both Spring
 * Application Context and the ClassLoader that loaded it allowing new
 * instance of Spring Application Context to be created upon the next start
 * (which may have an updated classpath and functionality) without
 * restarting NiFi.
 */
@OnStopped
public void closeSpringContext(ProcessContext processContext) {
    if (this.exchanger != null) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Closing Spring Application Context defined in " + this.applicationContextConfigFileName);
            }
            this.exchanger.close();
            if (logger.isInfoEnabled()) {
                logger.info("Successfully closed Spring Application Context defined in "
                        + this.applicationContextConfigFileName);
            }
        } catch (IOException e) {
            getLogger().warn("Failed while closing Spring Application Context", e);
        }
    }
}
 
Example #11
Source File: PostHTTP.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@OnStopped
public void onStopped() {
    this.acceptsRef.set(null);

    for (final Map.Entry<String, Config> entry : configMap.entrySet()) {
        final Config config = entry.getValue();
        config.getConnectionManager().shutdown();
    }

    configMap.clear();

    final StreamThrottler throttler = throttlerRef.getAndSet(null);
    if (throttler != null) {
        try {
            throttler.close();
        } catch (IOException e) {
            getLogger().error("Failed to close StreamThrottler", e);
        }
    }
}
 
Example #12
Source File: PublishKafka_0_10.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void closePool() {
    if (publisherPool != null) {
        publisherPool.close();
    }

    publisherPool = null;
}
 
Example #13
Source File: ConsumeKafka_0_10.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void close() {
    final ConsumerPool pool = consumerPool;
    consumerPool = null;
    if (pool != null) {
        pool.close();
    }
}
 
Example #14
Source File: PublishAMQPTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
@OnStopped
public void close() {
    if (this.closeConnection) {
        super.close();
    }
}
 
Example #15
Source File: AbstractWebSocketGatewayProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void onStopped(final ProcessContext context) throws IOException {
    if (webSocketService == null) {
        return;
    }

    try {
        // Deregister processor, so that it won't receive messages anymore.
        webSocketService.deregisterProcessor(endpointId, this);
        webSocketService = null;
    } catch (WebSocketConfigurationException e) {
        logger.warn("Failed to deregister processor {} due to: {}", new Object[]{this, e}, e);
    }
}
 
Example #16
Source File: GetSplunk.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void onStopped() {
    if (splunkService != null) {
        isInitialized.set(false);
        splunkService.logout();
        splunkService = null;
    }
}
 
Example #17
Source File: AbstractSiteToSiteReportingTask.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void shutdown() throws IOException {
    final SiteToSiteClient client = getClient();
    if (client != null) {
        client.close();
    }
}
 
Example #18
Source File: SolrProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public final void closeClient() {
    if (solrClient != null) {
        try {
            solrClient.close();
        } catch (IOException e) {
            getLogger().debug("Error closing SolrClient", e);
        }
    }
}
 
Example #19
Source File: ListenSMTP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void stop() {
    try {
        smtp.stop();
    } finally {
        smtp = null;
    }
}
 
Example #20
Source File: GetJMSTopic.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void onStopped() {
    final WrappedMessageConsumer consumer = this.wrappedConsumer;
    if (consumer != null) {
        consumer.close(getLogger());
        this.wrappedConsumer = null;
    }
}
 
Example #21
Source File: AbstractJMSProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Will destroy the instance of {@link CachingConnectionFactory} and sets
 * 'targetResource' to null;
 */
@OnStopped
public void close() {
    if (this.cachingConnectionFactory != null) {
        this.cachingConnectionFactory.destroy();
    }
    this.targetResource = null;
}
 
Example #22
Source File: ConsumeMQTT.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void onStopped(final ProcessContext context) throws IOException {
    if(mqttQueue != null && !mqttQueue.isEmpty() && processSessionFactory != null) {
        logger.info("Finishing processing leftover messages");
        ProcessSession session = processSessionFactory.createSession();
        transferQueue(session);
    } else {
        if (mqttQueue!= null && !mqttQueue.isEmpty()){
            throw new ProcessException("Stopping the processor but there is no ProcessSessionFactory stored and there are messages in the MQTT internal queue. Removing the processor now will " +
                    "clear the queue but will result in DATA LOSS. This is normally due to starting the processor, receiving messages and stopping before the onTrigger happens. The messages " +
                    "in the MQTT internal queue cannot finish processing until until the processor is triggered to run.");
        }
    }
}
 
Example #23
Source File: PutHiveStreaming.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void cleanup() {
    ComponentLog log = getLogger();
    sendHeartBeat.set(false);
    for (Map.Entry<HiveEndPoint, HiveWriter> entry : allWriters.entrySet()) {
        try {
            HiveWriter w = entry.getValue();
            w.flushAndClose();
        } catch (Exception ex) {
            log.warn("Error while closing writer to " + entry.getKey() + ". Exception follows.", ex);
            if (ex instanceof InterruptedException) {
                Thread.currentThread().interrupt();
            }
        }
    }

    callTimeoutPool.shutdown();
    try {
        while (!callTimeoutPool.isTerminated()) {
            callTimeoutPool.awaitTermination(options.getCallTimeOut(), TimeUnit.MILLISECONDS);
        }
    } catch (Throwable t) {
        log.warn("shutdown interrupted on " + callTimeoutPool, t);
    }

    callTimeoutPool = null;
    ugi = null;
    hiveConfigurator.stopRenewer();
}
 
Example #24
Source File: ConsumeWindowsEventLogTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test(expected = ProcessException.class)
public void testScheduleQueueStopThrowsException() throws Throwable {
    ReflectionUtils.invokeMethodsWithAnnotation(OnScheduled.class, evtSubscribe, testRunner.getProcessContext());

    WinNT.HANDLE handle = mockEventHandles(wEvtApi, kernel32, Arrays.asList("test")).get(0);
    getRenderingCallback().onEvent(WEvtApi.EvtSubscribeNotifyAction.DELIVER, null, handle);

    try {
        ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, evtSubscribe, testRunner.getProcessContext());
    } catch (InvocationTargetException e) {
        throw e.getCause();
    }
}
 
Example #25
Source File: PutIgniteCache.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Close data streamer and calls base classes close ignite cache
 */
@OnStopped
public final void closeIgniteDataStreamer() {
    if (igniteDataStreamer != null) {
        getLogger().info("Closing ignite data streamer");
        igniteDataStreamer.flush();
        igniteDataStreamer = null;
    }
}
 
Example #26
Source File: AbstractMongoProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public final void closeClient() {
    if (mongoClient != null) {
        getLogger().info("Closing MongoClient");
        mongoClient.close();
        mongoClient = null;
    }
}
 
Example #27
Source File: GeoEnrichIP.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnStopped
public void closeReader() throws IOException {
    final DatabaseReader reader = databaseReaderRef.get();
    if (reader != null) {
        reader.close();
    }
}
 
Example #28
Source File: EventDrivenSchedulingAgent.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void trigger(final Connectable worker, final ScheduleState scheduleState, final ConnectableProcessContext processContext, final ProcessSessionFactory sessionFactory) {
    final int newThreadCount = scheduleState.incrementActiveThreadCount();
    if (newThreadCount > worker.getMaxConcurrentTasks() && worker.getMaxConcurrentTasks() > 0) {
        // its possible that the worker queue could give us a worker node that is eligible to run based
        // on the number of threads but another thread has already incremented the thread count, result in
        // reaching the maximum number of threads. we won't know this until we atomically increment the thread count
        // on the Schedule State, so we check it here. in this case, we cannot trigger the Processor, as doing so would
        // result in using more than the maximum number of defined threads
        scheduleState.decrementActiveThreadCount();
        return;
    }

    try {
        try (final AutoCloseable ncl = NarCloseable.withComponentNarLoader(worker.getClass(), worker.getIdentifier())) {
            worker.onTrigger(processContext, sessionFactory);
        } catch (final ProcessException pe) {
            logger.error("{} failed to process session due to {}", worker, pe.toString());
        } catch (final Throwable t) {
            logger.error("{} failed to process session due to {}", worker, t.toString());
            logger.error("", t);

            logger.warn("{} Administratively Pausing for {} due to processing failure: {}", worker, getAdministrativeYieldDuration(), t.toString());
            logger.warn("", t);
            try {
                Thread.sleep(FormatUtils.getTimeDuration(adminYieldDuration, TimeUnit.MILLISECONDS));
            } catch (final InterruptedException e) {
            }

        }
    } finally {
        if (!scheduleState.isScheduled() && scheduleState.getActiveThreadCount() == 1 && scheduleState.mustCallOnStoppedMethods()) {
            try (final NarCloseable x = NarCloseable.withComponentNarLoader(worker.getClass(), worker.getIdentifier())) {
                ReflectionUtils.quietlyInvokeMethodsWithAnnotation(OnStopped.class, worker, processContext);
            }
        }

        scheduleState.decrementActiveThreadCount();
    }
}
 
Example #29
Source File: ReflectionUtilsTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void ensureSuccessWhenArgumentCountDoesntMatch() throws Exception {
    ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, new B(), 3, "hjk");
    assertEquals(2, this.invocations.size());
    assertEquals("B", this.invocations.get(0));
    assertEquals("B", this.invocations.get(1));
}
 
Example #30
Source File: ReflectionUtilsTest.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeWithBridgePresent() throws Exception {
    ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, new B(), 2);
    assertEquals(2, this.invocations.size());
    assertEquals("B", this.invocations.get(0));
    assertEquals("B", this.invocations.get(1));
}