Search in sources :

Example 21 with ProcessSession

use of org.apache.nifi.processor.ProcessSession in project nifi by apache.

the class ResultProcessorTest method testProcessResultFileSuccess.

@Test
public void testProcessResultFileSuccess() {
    ProcessSession processSession = mock(ProcessSession.class);
    ComponentLog componentLog = mock(ComponentLog.class);
    FlowFile flowFile = mock(FlowFile.class);
    Exception exception = null;
    String name = "basename";
    when(processSession.putAttribute(eq(flowFile), anyString(), anyString())).thenReturn(flowFile);
    resultProcessor.process(processSession, componentLog, flowFile, exception, name);
    verify(processSession).putAttribute(flowFile, CoreAttributes.FILENAME.key(), name);
    verify(processSession).putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), MediaType.APPLICATION_XML_UTF_8.toString());
    verify(processSession).transfer(flowFile, successRelationship);
    verifyNoMoreInteractions(componentLog);
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) Matchers.anyString(org.mockito.Matchers.anyString) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Example 22 with ProcessSession

use of org.apache.nifi.processor.ProcessSession in project nifi by apache.

the class BinFiles method processBins.

private int processBins(final ProcessContext context) {
    final ComponentLog logger = getLogger();
    int processedBins = 0;
    Bin bin;
    while ((bin = readyBins.poll()) != null) {
        boolean binAlreadyCommitted;
        try {
            binAlreadyCommitted = this.processBin(bin, context);
        } catch (final ProcessException e) {
            logger.error("Failed to process bundle of {} files due to {}", new Object[] { bin.getContents().size(), e });
            final ProcessSession binSession = bin.getSession();
            for (final FlowFile flowFile : bin.getContents()) {
                binSession.transfer(flowFile, REL_FAILURE);
            }
            binSession.commit();
            continue;
        } catch (final Exception e) {
            logger.error("Failed to process bundle of {} files due to {}; rolling back sessions", new Object[] { bin.getContents().size(), e });
            bin.getSession().rollback();
            continue;
        }
        // If this bin's session has been committed, move on.
        if (!binAlreadyCommitted) {
            final ProcessSession binSession = bin.getSession();
            binSession.transfer(bin.getContents(), REL_ORIGINAL);
            binSession.commit();
        }
        processedBins++;
    }
    return processedBins;
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) ProcessException(org.apache.nifi.processor.exception.ProcessException) ComponentLog(org.apache.nifi.logging.ComponentLog) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException)

Example 23 with ProcessSession

use of org.apache.nifi.processor.ProcessSession in project nifi by apache.

the class BinFiles method binFlowFiles.

private int binFlowFiles(final ProcessContext context, final ProcessSessionFactory sessionFactory) {
    int flowFilesBinned = 0;
    while (binManager.getBinCount() <= context.getProperty(MAX_BIN_COUNT).asInteger()) {
        if (!isScheduled()) {
            break;
        }
        final ProcessSession session = sessionFactory.createSession();
        final List<FlowFile> flowFiles = session.get(1000);
        if (flowFiles.isEmpty()) {
            break;
        }
        final Map<String, List<FlowFile>> flowFileGroups = new HashMap<>();
        for (FlowFile flowFile : flowFiles) {
            flowFile = this.preprocessFlowFile(context, session, flowFile);
            try {
                final String groupingIdentifier = getGroupId(context, flowFile, session);
                flowFileGroups.computeIfAbsent(groupingIdentifier, id -> new ArrayList<>()).add(flowFile);
            } catch (final Exception e) {
                getLogger().error("Could not determine which Bin to add {} to; will route to failure", new Object[] { flowFile }, e);
                session.transfer(flowFile, REL_FAILURE);
                continue;
            }
        }
        for (final Map.Entry<String, List<FlowFile>> entry : flowFileGroups.entrySet()) {
            final Set<FlowFile> unbinned = binManager.offer(entry.getKey(), entry.getValue(), session, sessionFactory);
            for (final FlowFile flowFile : unbinned) {
                Bin bin = new Bin(sessionFactory.createSession(), 0, Long.MAX_VALUE, 0, Integer.MAX_VALUE, null);
                bin.offer(flowFile, session);
                this.readyBins.add(bin);
            }
            flowFilesBinned += entry.getValue().size();
        }
    }
    return flowFilesBinned;
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) StandardValidators(org.apache.nifi.processor.util.StandardValidators) ValidationContext(org.apache.nifi.components.ValidationContext) HashMap(java.util.HashMap) ComponentLog(org.apache.nifi.logging.ComponentLog) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ProcessException(org.apache.nifi.processor.exception.ProcessException) ArrayList(java.util.ArrayList) Relationship(org.apache.nifi.processor.Relationship) Map(java.util.Map) AbstractSessionFactoryProcessor(org.apache.nifi.processor.AbstractSessionFactoryProcessor) ValidationResult(org.apache.nifi.components.ValidationResult) FlowFile(org.apache.nifi.flowfile.FlowFile) Collection(java.util.Collection) ProcessContext(org.apache.nifi.processor.ProcessContext) Set(java.util.Set) IOException(java.io.IOException) ProcessSession(org.apache.nifi.processor.ProcessSession) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ProcessSessionFactory(org.apache.nifi.processor.ProcessSessionFactory) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled) Queue(java.util.Queue) DataUnit(org.apache.nifi.processor.DataUnit) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped) FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with ProcessSession

use of org.apache.nifi.processor.ProcessSession in project nifi by apache.

the class AbstractMQTTProcessor method onTrigger.

@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;
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession)

Example 25 with ProcessSession

use of org.apache.nifi.processor.ProcessSession in project nifi by apache.

the class TestConsumeMQTT method testMessageNotConsumedOnCommitFail.

/**
 * If the session.commit() fails, we should not remove the unprocessed message
 */
@Test
public void testMessageNotConsumedOnCommitFail() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    testRunner.run(1, false);
    ConsumeMQTT processor = (ConsumeMQTT) testRunner.getProcessor();
    MQTTQueueMessage mock = mock(MQTTQueueMessage.class);
    when(mock.getPayload()).thenReturn(new byte[0]);
    when(mock.getTopic()).thenReturn("testTopic");
    BlockingQueue<MQTTQueueMessage> mqttQueue = getMqttQueue(processor);
    mqttQueue.add(mock);
    try {
        ProcessSession session = testRunner.getProcessSessionFactory().createSession();
        transferQueue(processor, (ProcessSession) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { ProcessSession.class }, (proxy, method, args) -> {
            if (method.getName().equals("commit")) {
                throw new RuntimeException();
            } else {
                return method.invoke(session, args);
            }
        }));
        fail("Expected runtime exception");
    } catch (InvocationTargetException e) {
        assertTrue("Expected generic runtime exception, not " + e, e.getCause() instanceof RuntimeException);
    }
    assertTrue("Expected mqttQueue to contain uncommitted message.", mqttQueue.contains(mock));
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) MQTTQueueMessage(org.apache.nifi.processors.mqtt.common.MQTTQueueMessage) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Aggregations

ProcessSession (org.apache.nifi.processor.ProcessSession)129 FlowFile (org.apache.nifi.flowfile.FlowFile)96 ProcessContext (org.apache.nifi.processor.ProcessContext)55 IOException (java.io.IOException)54 ProcessException (org.apache.nifi.processor.exception.ProcessException)51 Test (org.junit.Test)47 Relationship (org.apache.nifi.processor.Relationship)45 List (java.util.List)42 ArrayList (java.util.ArrayList)41 Map (java.util.Map)39 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)39 ComponentLog (org.apache.nifi.logging.ComponentLog)39 HashSet (java.util.HashSet)38 Set (java.util.Set)38 HashMap (java.util.HashMap)35 Collections (java.util.Collections)33 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)33 Tags (org.apache.nifi.annotation.documentation.Tags)33 InputRequirement (org.apache.nifi.annotation.behavior.InputRequirement)31 MockFlowFile (org.apache.nifi.util.MockFlowFile)31