Java Code Examples for org.apache.nifi.processor.ProcessSessionFactory

The following examples show how to use org.apache.nifi.processor.ProcessSessionFactory. These examples are extracted from open source projects. 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 Project: localization_nifi   Source File: BinFiles.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final int totalBinCount = binManager.getBinCount() + readyBins.size();
    final int maxBinCount = context.getProperty(MAX_BIN_COUNT).asInteger();
    final int flowFilesBinned;

    if (totalBinCount < maxBinCount) {
        flowFilesBinned = binFlowFiles(context, sessionFactory);
        getLogger().debug("Binned {} FlowFiles", new Object[] {flowFilesBinned});
    } else {
        flowFilesBinned = 0;
        getLogger().debug("Will not bin any FlowFiles because {} bins already exist;"
            + "will wait until bins have been emptied before any more are created", new Object[] {totalBinCount});
    }

    if (!isScheduled()) {
        return;
    }

    final int binsMigrated = migrateBins(context);
    final int binsProcessed = processBins(context);
    //If we accomplished nothing then let's yield
    if (flowFilesBinned == 0 && binsMigrated == 0 && binsProcessed == 0) {
        context.yield();
    }
}
 
Example 2
@Override
public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    if (processSessionFactory == null) {
        processSessionFactory = sessionFactory;
    }

    if (!isProcessorRegisteredToService()) {
        try {
            registerProcessorToService(context, webSocketService -> onWebSocketServiceReady(webSocketService));
        } catch (IOException|WebSocketConfigurationException e) {
            throw new ProcessException("Failed to register processor to WebSocket service due to: " + e, e);
        }
    }

    context.yield();//nothing really to do here since threading managed by smtp server sessions
}
 
Example 3
Source Project: localization_nifi   Source File: TestListenSyslog.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParsingError() throws IOException {
    final FailParseProcessor proc = new FailParseProcessor();
    final TestRunner runner = TestRunners.newTestRunner(proc);
    runner.setProperty(ListenSyslog.PROTOCOL, ListenSyslog.UDP_VALUE.getValue());
    runner.setProperty(ListenSyslog.PORT, "0");

    // schedule to start listening on a random port
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();
    proc.onScheduled(context);

    try {
        final int port = proc.getPort();
        final DatagramSender sender = new DatagramSender(port, 1, 1, INVALID_MESSAGE);
        sender.run();

        // should keep re-processing event1 from the error queue
        proc.onTrigger(context, processSessionFactory);
        runner.assertTransferCount(ListenSyslog.REL_INVALID, 1);
        runner.assertTransferCount(ListenSyslog.REL_SUCCESS, 0);
    } finally {
        proc.onUnscheduled();
    }
}
 
Example 4
Source Project: nifi   Source File: ExecuteFlumeSource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    if (source instanceof PollableSource) {
        super.onTrigger(context, sessionFactory);
    } else if (source instanceof EventDrivenSource) {
        ProcessSessionFactory old = sessionFactoryRef.getAndSet(sessionFactory);
        if (old != sessionFactory) {
            if (runnerRef.get() != null) {
                stopped();
                sessionFactoryRef.set(sessionFactory);
            }

            runnerRef.set(new EventDrivenSourceRunner());
            eventDrivenSourceChannelRef.set(new NifiSessionFactoryChannel(sessionFactoryRef.get(), SUCCESS));
            eventDrivenSourceChannelRef.get().start();
            source.setChannelProcessor(new ChannelProcessor(
                new NifiChannelSelector(eventDrivenSourceChannelRef.get())));
            runnerRef.get().setSource(source);
            runnerRef.get().start();
        }
    }
}
 
Example 5
Source Project: localization_nifi   Source File: ConsumeWindowsEventLog.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    this.sessionFactory = sessionFactory;
    if (!isSubscribed()) {
        String errorMessage;
        try {
            errorMessage = subscribe(context);
        } catch (URISyntaxException e) {
            getLogger().error(e.getMessage(), e);
            context.yield();
            return;
        }
        if (errorMessage != null) {
            context.yield();
            getLogger().error(errorMessage);
            return;
        }
    }
    processQueue(sessionFactory.createSession());
}
 
Example 6
Source Project: nifi   Source File: SmtpConsumer.java    License: Apache License 2.0 6 votes vote down vote up
public SmtpConsumer(
        final MessageContext context,
        final ProcessSessionFactory sessionFactory,
        final int port,
        final String host,
        final ComponentLog log,
        final int maxMessageSize
) {
    this.context = context;
    this.sessionFactory = sessionFactory;
    this.port = port;
    if (host == null || host.trim().isEmpty()) {
        this.host = context.getSMTPServer().getHostName();
    } else {
        this.host = host;
    }
    this.log = log;
    this.maxMessageSize = maxMessageSize;
}
 
Example 7
Source Project: localization_nifi   Source File: SmtpConsumer.java    License: Apache License 2.0 6 votes vote down vote up
public SmtpConsumer(
        final MessageContext context,
        final ProcessSessionFactory sessionFactory,
        final int port,
        final String host,
        final ComponentLog log,
        final int maxMessageSize
) {
    this.context = context;
    this.sessionFactory = sessionFactory;
    this.port = port;
    if (host == null || host.trim().isEmpty()) {
        this.host = context.getSMTPServer().getHostName();
    } else {
        this.host = host;
    }
    this.log = log;
    this.maxMessageSize = maxMessageSize;
}
 
Example 8
Source Project: nifi   Source File: TestConsumeAzureEventHub.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setupProcessor() {
    processor = new ConsumeAzureEventHub();
    final ProcessorInitializationContext initContext = Mockito.mock(ProcessorInitializationContext.class);
    final String componentId = "componentId";
    when(initContext.getIdentifier()).thenReturn(componentId);
    MockComponentLog componentLog = new MockComponentLog(componentId, processor);
    when(initContext.getLogger()).thenReturn(componentLog);
    processor.initialize(initContext);

    final ProcessSessionFactory processSessionFactory = Mockito.mock(ProcessSessionFactory.class);
    processor.setProcessSessionFactory(processSessionFactory);
    processor.setNamespaceName("namespace");

    sharedState = new SharedSessionState(processor, new AtomicLong(0));
    processSession = new MockProcessSession(sharedState, processor);
    when(processSessionFactory.createSession()).thenReturn(processSession);

    eventProcessor = processor.new EventProcessor();

    partitionContext = Mockito.mock(PartitionContext.class);
    when(partitionContext.getEventHubPath()).thenReturn("eventhub-name");
    when(partitionContext.getPartitionId()).thenReturn("partition-id");
    when(partitionContext.getConsumerGroupName()).thenReturn("consumer-group");
}
 
Example 9
Source Project: nifi   Source File: BinFiles.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final int totalBinCount = binManager.getBinCount() + readyBins.size();
    final int maxBinCount = context.getProperty(MAX_BIN_COUNT).asInteger();
    final int flowFilesBinned;

    if (totalBinCount < maxBinCount) {
        flowFilesBinned = binFlowFiles(context, sessionFactory);
        getLogger().debug("Binned {} FlowFiles", new Object[] {flowFilesBinned});
    } else {
        flowFilesBinned = 0;
        getLogger().debug("Will not bin any FlowFiles because {} bins already exist;"
            + "will wait until bins have been emptied before any more are created", new Object[] {totalBinCount});
    }

    if (!isScheduled()) {
        return;
    }

    final int binsMigrated = migrateBins(context);
    final int binsProcessed = processBins(context);
    //If we accomplished nothing then let's yield
    if (flowFilesBinned == 0 && binsMigrated == 0 && binsProcessed == 0) {
        context.yield();
    }
}
 
Example 10
Source Project: nifi   Source File: TestListenHTTP.java    License: Apache License 2.0 6 votes vote down vote up
private void startWebServerAndSendRequests(Runnable sendRequestToWebserver, int numberOfExpectedFlowFiles, int returnCode) throws Exception {
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    final ProcessContext context = runner.getProcessContext();

    // Need at least one trigger to make sure server is listening
    proc.onTrigger(context, processSessionFactory);

    new Thread(sendRequestToWebserver).start();

    long responseTimeout = 10000;

    int numTransferred = 0;
    long startTime = System.currentTimeMillis();
    while (numTransferred < numberOfExpectedFlowFiles && (System.currentTimeMillis() - startTime < responseTimeout)) {
        proc.onTrigger(context, processSessionFactory);
        numTransferred = runner.getFlowFilesForRelationship(RELATIONSHIP_SUCCESS).size();
        Thread.sleep(100);
    }

    runner.assertTransferCount(ListenHTTP.RELATIONSHIP_SUCCESS, numberOfExpectedFlowFiles);
}
 
Example 11
Source Project: nifi   Source File: ListenHTTPServlet.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void init(final ServletConfig config) throws ServletException {
    final ServletContext context = config.getServletContext();
    this.logger = (ComponentLog) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_LOGGER);
    this.sessionFactoryHolder = (AtomicReference<ProcessSessionFactory>) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_SESSION_FACTORY_HOLDER);
    this.processContext = (ProcessContext) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_PROCESS_CONTEXT_HOLDER);
    this.authorizedPattern = (Pattern) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_AUTHORITY_PATTERN);
    this.headerPattern = (Pattern) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_HEADER_PATTERN);
    this.flowFileMap = (ConcurrentMap<String, FlowFileEntryTimeWrapper>) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_FLOWFILE_MAP);
    this.streamThrottler = (StreamThrottler) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_STREAM_THROTTLER);
    this.basePath = (String) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_BASE_PATH);
    this.returnCode = (int) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_RETURN_CODE);
    this.multipartRequestMaxSize = (long) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_MULTIPART_REQUEST_MAX_SIZE);
    this.multipartReadBufferSize = (int) context.getAttribute(ListenHTTP.CONTEXT_ATTRIBUTE_MULTIPART_READ_BUFFER_SIZE);
}
 
Example 12
Source Project: nifi   Source File: ListenHTTP.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    try {
        if (!initialized.get()) {
            createHttpServerFromService(context);
        }
    } catch (Exception e) {
        getLogger().warn("Failed to start http server during initialization: " + e);
        context.yield();
        throw new ProcessException("Failed to initialize the server", e);
    }

    sessionFactoryReference.compareAndSet(null, sessionFactory);

    for (final String id : findOldFlowFileIds(context)) {
        final FlowFileEntryTimeWrapper wrapper = flowFileMap.remove(id);
        if (wrapper != null) {
            getLogger().warn("failed to received acknowledgment for HOLD with ID {} sent by {}; rolling back session", new Object[] {id, wrapper.getClientIP()});
            wrapper.session.rollback();
        }
    }

    context.yield();
}
 
Example 13
Source Project: nifi   Source File: ConsumeWindowsEventLog.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    this.sessionFactory = sessionFactory;

    if (!isSubscribed()) {
        String errorMessage = subscribe(context);
        if (errorMessage != null) {
            context.yield();
            getLogger().error(errorMessage);
            return;
        }
    }

    final int flowFileCount = processQueue(sessionFactory.createSession());

    final long now = System.currentTimeMillis();
    if (flowFileCount > 0) {
        lastActivityTimestamp = now;

    } else if (inactiveDurationToReconnect > 0) {
        if ((now - lastActivityTimestamp) > inactiveDurationToReconnect) {
            getLogger().info("Exceeds configured 'inactive duration to reconnect' {} ms. Unsubscribe to reconnect..", new Object[]{inactiveDurationToReconnect});
            unsubscribe();
        }
    }
}
 
Example 14
Source Project: nifi   Source File: TestUpdateAttribute.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testStateFailures() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    final UpdateAttribute processor = (UpdateAttribute) runner.getProcessor();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    MockStateManager mockStateManager = runner.getStateManager();

    runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
    runner.setProperty("count", "${getStateValue('count'):plus(1)}");
    runner.setProperty("sum", "${getStateValue('sum'):plus(${pencils})}");
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");

    processor.onScheduled(runner.getProcessContext());

    final Map<String, String> attributes2 = new HashMap<>();
    attributes2.put("pencils", "2");

    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);

    runner.enqueue(new byte[0],attributes2);
    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueNotEmpty();

    mockStateManager.setFailOnStateGet(Scope.LOCAL, false);
    mockStateManager.setFailOnStateSet(Scope.LOCAL, true);

    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueEmpty();

    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_FAILED_SET_STATE, 1);
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("count", "1");
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("sum", "2");
}
 
Example 15
Source Project: localization_nifi   Source File: InvokeScriptedProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Invokes the onTrigger() method of the scripted processor. If the script
 * failed to reload, the processor yields until the script can be reloaded
 * successfully. If the scripted processor's onTrigger() method throws an
 * exception, a ProcessException will be thrown. If no processor is defined
 * by the script, an error is logged with the system.
 *
 * @param context provides access to convenience methods for obtaining
 * property values, delaying the scheduling of the processor, provides
 * access to Controller Services, etc.
 * @param sessionFactory provides access to a {@link ProcessSessionFactory},
 * which can be used for accessing FlowFiles, etc.
 * @throws ProcessException if the scripted processor's onTrigger() method
 * throws an exception
 */
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {

    // Initialize the rest of the processor resources if we have not already done so
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }

    ComponentLog log = getLogger();

    // ensure the processor (if it exists) is loaded
    final Processor instance = processor.get();

    // ensure the processor did not fail to reload at some point
    final Collection<ValidationResult> results = validationResults.get();
    if (!results.isEmpty()) {
        log.error(String.format("Unable to run because the Processor is not valid: [%s]",
                StringUtils.join(results, ", ")));
        context.yield();
        return;
    }
    if (instance != null) {
        try {
            // run the processor
            instance.onTrigger(context, sessionFactory);
        } catch (final ProcessException e) {
            final String message = String.format("An error occurred executing the configured Processor [%s]: %s",
                    context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue(), e);
            log.error(message);
            throw e;
        }
    } else {
        log.error("There is no processor defined by the script");
    }
}
 
Example 16
Source Project: nifi   Source File: AttributeRollingWindowIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testStateFailures() throws InterruptedException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(AttributeRollingWindow.class);
    MockStateManager mockStateManager = runner.getStateManager();
    final AttributeRollingWindow processor = (AttributeRollingWindow) runner.getProcessor();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();

    runner.setProperty(AttributeRollingWindow.VALUE_TO_TRACK, "${value}");
    runner.setProperty(AttributeRollingWindow.TIME_WINDOW, "3 sec");

    processor.onScheduled(runner.getProcessContext());

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("value", "1");

    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);

    runner.enqueue(new byte[0],attributes);
    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueNotEmpty();

    mockStateManager.setFailOnStateGet(Scope.LOCAL, false);
    mockStateManager.setFailOnStateSet(Scope.LOCAL, true);

    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueEmpty();

    runner.assertAllFlowFilesTransferred(AttributeRollingWindow.REL_FAILED_SET_STATE, 1);
    MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(REL_FAILED_SET_STATE).get(0);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_VALUE_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_COUNT_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_MEAN_KEY);
}
 
Example 17
Source Project: localization_nifi   Source File: PutTCP.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * event handler method to handle the FlowFile being forwarded to the Processor by the framework. The FlowFile contents is sent out over a TCP connection using an acquired ChannelSender object. If
 * the FlowFile contents was sent out successfully then the FlowFile is forwarded to the success relationship. If an error occurred then the FlowFile is forwarded to the failure relationship.
 *
 * @param context
 *            - the current process context.
 *
 * @param sessionFactory
 *            - a factory object to obtain a process session.
 */
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        pruneIdleSenders(context.getProperty(IDLE_EXPIRATION).asTimePeriod(TimeUnit.MILLISECONDS).longValue());
        context.yield();
        return;
    }

    ChannelSender sender = acquireSender(context, session, flowFile);
    if (sender == null) {
        return;
    }

    try {
        String outgoingMessageDelimiter = getOutgoingMessageDelimiter(context, flowFile);
        ByteArrayOutputStream content = readContent(session, flowFile);
        if (outgoingMessageDelimiter != null) {
            Charset charset = Charset.forName(context.getProperty(CHARSET).getValue());
            content = appendDelimiter(content, outgoingMessageDelimiter, charset);
        }
        StopWatch stopWatch = new StopWatch(true);
        sender.send(content.toByteArray());
        session.getProvenanceReporter().send(flowFile, transitUri, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        session.transfer(flowFile, REL_SUCCESS);
        session.commit();
    } catch (Exception e) {
        onFailure(context, session, flowFile);
        getLogger().error("Exception while handling a process session, transferring {} to failure.", new Object[] { flowFile }, e);
    } finally {
        // If we are going to use this sender again, then relinquish it back to the pool.
        if (!isConnectionPerFlowFile(context)) {
            relinquishSender(sender);
        } else {
            sender.close();
        }
    }
}
 
Example 18
Source Project: localization_nifi   Source File: PutUDP.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * event handler method to handle the FlowFile being forwarded to the Processor by the framework. The FlowFile contents is sent out as a UDP datagram using an acquired ChannelSender object. If the
 * FlowFile contents was sent out successfully then the FlowFile is forwarded to the success relationship. If an error occurred then the FlowFile is forwarded to the failure relationship.
 *
 * @param context
 *            - the current process context.
 *
 * @param sessionFactory
 *            - a factory object to obtain a process session.
 */
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        pruneIdleSenders(context.getProperty(IDLE_EXPIRATION).asTimePeriod(TimeUnit.MILLISECONDS).longValue());
        context.yield();
        return;
    }

    ChannelSender sender = acquireSender(context, session, flowFile);
    if (sender == null) {
        return;
    }

    try {
        byte[] content = readContent(session, flowFile);
        StopWatch stopWatch = new StopWatch(true);
        sender.send(content);
        session.getProvenanceReporter().send(flowFile, transitUri, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        session.transfer(flowFile, REL_SUCCESS);
        session.commit();
    } catch (Exception e) {
        getLogger().error("Exception while handling a process session, transferring {} to failure.", new Object[] { flowFile }, e);
        onFailure(context, session, flowFile);
    } finally {
        relinquishSender(sender);
    }
}
 
Example 19
Source Project: localization_nifi   Source File: ListenHTTP.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    sessionFactoryReference.compareAndSet(null, sessionFactory);

    for (final String id : findOldFlowFileIds(context)) {
        final FlowFileEntryTimeWrapper wrapper = flowFileMap.remove(id);
        if (wrapper != null) {
            getLogger().warn("failed to received acknowledgment for HOLD with ID {}; rolling back session", new Object[] {id});
            wrapper.session.rollback();
        }
    }

    context.yield();
}
 
Example 20
@Test
public void testStateFailures() throws InterruptedException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(AttributeRollingWindow.class);
    MockStateManager mockStateManager = runner.getStateManager();
    final AttributeRollingWindow processor = (AttributeRollingWindow) runner.getProcessor();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();

    runner.setProperty(AttributeRollingWindow.VALUE_TO_TRACK, "${value}");
    runner.setProperty(AttributeRollingWindow.TIME_WINDOW, "3 sec");

    processor.onScheduled(runner.getProcessContext());

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("value", "1");

    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);

    runner.enqueue(new byte[0],attributes);
    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueNotEmpty();

    mockStateManager.setFailOnStateGet(Scope.LOCAL, false);
    mockStateManager.setFailOnStateSet(Scope.LOCAL, true);

    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueEmpty();

    runner.assertAllFlowFilesTransferred(AttributeRollingWindow.REL_FAILED_SET_STATE, 1);
    MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(REL_FAILED_SET_STATE).get(0);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_VALUE_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_COUNT_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_MEAN_KEY);
}
 
Example 21
Source Project: localization_nifi   Source File: TestUpdateAttribute.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testStateFailures() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    final UpdateAttribute processor = (UpdateAttribute) runner.getProcessor();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();
    MockStateManager mockStateManager = runner.getStateManager();

    runner.setProperty(UpdateAttribute.STORE_STATE, STORE_STATE_LOCALLY);
    runner.setProperty("count", "${getStateValue('count'):plus(1)}");
    runner.setProperty("sum", "${getStateValue('sum'):plus(${pencils})}");
    runner.setProperty(UpdateAttribute.STATEFUL_VARIABLES_INIT_VALUE, "0");

    processor.onScheduled(runner.getProcessContext());

    final Map<String, String> attributes2 = new HashMap<>();
    attributes2.put("pencils", "2");

    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);

    runner.enqueue(new byte[0],attributes2);
    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueNotEmpty();

    mockStateManager.setFailOnStateGet(Scope.LOCAL, false);
    mockStateManager.setFailOnStateSet(Scope.LOCAL, true);

    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueEmpty();

    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_FAILED_SET_STATE, 1);
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("count", "1");
    runner.getFlowFilesForRelationship(UpdateAttribute.REL_FAILED_SET_STATE).get(0).assertAttributeEquals("sum", "2");
}
 
Example 22
Source Project: localization_nifi   Source File: AbstractFlumeProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();
    try {
        onTrigger(context, session);
        session.commit();
    } catch (final Throwable t) {
        getLogger()
            .error("{} failed to process due to {}; rolling back session", new Object[]{this, t});
        session.rollback(true);
        throw t;
    }
}
 
Example 23
Source Project: localization_nifi   Source File: StandardFunnel.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();

    try {
        onTrigger(context, session);
        session.commit();
    } catch (final ProcessException e) {
        session.rollback();
        throw e;
    } catch (final Throwable t) {
        session.rollback();
        throw new RuntimeException(t);
    }
}
 
Example 24
Source Project: localization_nifi   Source File: ListenSMTP.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    if (smtp == null) {
        try {
            final SMTPServer server = prepareServer(context, sessionFactory);
            server.start();
            smtp = server;
        } catch (final Exception ex) {//have to catch exception due to awkward exception handling in subethasmtp
            smtp = null;
            getLogger().error("Unable to start SMTP server due to " + ex.getMessage(), ex);
        }
    }
    context.yield();//nothing really to do here since threading managed by smtp server sessions
}
 
Example 25
Source Project: nifi   Source File: InvokeScriptedProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Invokes the onTrigger() method of the scripted processor. If the script
 * failed to reload, the processor yields until the script can be reloaded
 * successfully. If the scripted processor's onTrigger() method throws an
 * exception, a ProcessException will be thrown. If no processor is defined
 * by the script, an error is logged with the system.
 *
 * @param context provides access to convenience methods for obtaining
 * property values, delaying the scheduling of the processor, provides
 * access to Controller Services, etc.
 * @param sessionFactory provides access to a {@link ProcessSessionFactory},
 * which can be used for accessing FlowFiles, etc.
 * @throws ProcessException if the scripted processor's onTrigger() method
 * throws an exception
 */
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {

    // Initialize the rest of the processor resources if we have not already done so
    synchronized (scriptingComponentHelper.isInitialized) {
        if (!scriptingComponentHelper.isInitialized.get()) {
            scriptingComponentHelper.createResources();
        }
    }

    ComponentLog log = getLogger();

    // ensure the processor (if it exists) is loaded
    final Processor instance = processor.get();

    // ensure the processor did not fail to reload at some point
    final Collection<ValidationResult> results = validationResults.get();
    if (!results.isEmpty()) {
        log.error(String.format("Unable to run because the Processor is not valid: [%s]",
                StringUtils.join(results, ", ")));
        context.yield();
        return;
    }
    if (instance != null) {
        try {
            // run the processor
            instance.onTrigger(context, sessionFactory);
        } catch (final ProcessException e) {
            final String message = String.format("An error occurred executing the configured Processor [%s]: %s",
                    context.getProperty(ScriptingComponentUtils.SCRIPT_FILE).getValue(), e);
            log.error(message);
            throw e;
        }
    } else {
        log.error("There is no processor defined by the script");
    }
}
 
Example 26
Source Project: nifi   Source File: AbstractMQTTProcessor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    if (processSessionFactory == null) {
        processSessionFactory = sessionFactory;
    }
    ProcessSession session = sessionFactory.createSession();
    try {
        onTrigger(context, session);
        session.commit();
    } catch (final Throwable t) {
        getLogger().error("{} failed to process due to {}; rolling back session", new Object[]{this, t});
        session.rollback(true);
        throw t;
    }
}
 
Example 27
Source Project: nifi   Source File: FlowFileIngestService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a FlowFileIngestService
 *
 * @param sessionFactoryReference a reference to a {@link ProcessSessionFactory} to route {@link
 *                                FlowFile}s to process relationships.
 */
public FlowFileIngestService(final ComponentLog logger,
                             final AtomicReference<ProcessSessionFactory> sessionFactoryReference,
                             final ProcessContext context) {
    this.context = checkNotNull(context);
    this.sessionFactoryReference = checkNotNull(sessionFactoryReference);
    this.logger = checkNotNull(logger);
}
 
Example 28
Source Project: nifi   Source File: GetTCP.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    if (this.delegatingMessageHandler == null) {
        this.delegatingMessageHandler = new NiFiDelegatingMessageHandler(sessionFactory);
    }
    this.run(context);
    context.yield();
}
 
Example 29
Source Project: nifi   Source File: PutUDP.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * event handler method to handle the FlowFile being forwarded to the Processor by the framework. The FlowFile contents is sent out as a UDP datagram using an acquired ChannelSender object. If the
 * FlowFile contents was sent out successfully then the FlowFile is forwarded to the success relationship. If an error occurred then the FlowFile is forwarded to the failure relationship.
 *
 * @param context
 *            - the current process context.
 *
 * @param sessionFactory
 *            - a factory object to obtain a process session.
 */
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    final ProcessSession session = sessionFactory.createSession();
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        final PruneResult result = pruneIdleSenders(context.getProperty(IDLE_EXPIRATION).asTimePeriod(TimeUnit.MILLISECONDS).longValue());
        // yield if we closed an idle connection, or if there were no connections in the first place
        if (result.getNumClosed() > 0 || (result.getNumClosed() == 0 && result.getNumConsidered() == 0)) {
            context.yield();
        }
        return;
    }

    ChannelSender sender = acquireSender(context, session, flowFile);
    if (sender == null) {
        return;
    }

    try {
        byte[] content = readContent(session, flowFile);
        StopWatch stopWatch = new StopWatch(true);
        sender.send(content);
        session.getProvenanceReporter().send(flowFile, transitUri, stopWatch.getElapsed(TimeUnit.MILLISECONDS));
        session.transfer(flowFile, REL_SUCCESS);
        session.commit();
    } catch (Exception e) {
        getLogger().error("Exception while handling a process session, transferring {} to failure.", new Object[] { flowFile }, e);
        onFailure(context, session, flowFile);
    } finally {
        relinquishSender(sender);
    }
}
 
Example 30
Source Project: nifi   Source File: RecordBinManager.java    License: Apache License 2.0 5 votes vote down vote up
public RecordBinManager(final ProcessContext context, final ProcessSessionFactory sessionFactory, final ComponentLog logger) {
    this.context = context;
    this.sessionFactory = sessionFactory;
    this.logger = logger;

    final Integer maxBins = context.getProperty(MergeRecord.MAX_BIN_COUNT).asInteger();
    this.maxBinCount = maxBins == null ? Integer.MAX_VALUE : maxBins.intValue();
}