Java Code Examples for org.apache.nifi.processor.ProcessSessionFactory#createSession()

The following examples show how to use org.apache.nifi.processor.ProcessSessionFactory#createSession() . 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: PutTCP.java    From localization_nifi with 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 2
Source File: CountEvents.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    this.sessionFactory = sessionFactory;

    final ProcessSession session = sessionFactory.createSession();
    if (!firstScheduleCounted.getAndSet(true)) {
        session.adjustCounter("Scheduled", 1, true);
    }

    session.adjustCounter("Triggered", 1, true);
}
 
Example 3
Source File: StandardFunnel.java    From nifi with 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 4
Source File: AbstractPort.java    From nifi with 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) {
        session.rollback();
        throw t;
    }
}
 
Example 5
Source File: AbstractFlumeProcessor.java    From nifi with 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 6
Source File: AbstractMQTTProcessor.java    From nifi with 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 7
Source File: PutUDP.java    From nifi with 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 8
Source File: StandardFunnel.java    From localization_nifi with 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 9
Source File: AbstractPort.java    From localization_nifi with 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 10
Source File: AbstractFlumeProcessor.java    From localization_nifi with 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 11
Source File: AbstractMQTTProcessor.java    From localization_nifi with 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 12
Source File: PutUDP.java    From localization_nifi with 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 13
Source File: AbstractKafkaProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Override
public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    if (this.acceptTask) { // acts as a circuit breaker to allow existing tasks to wind down so 'kafkaResource' can be reset before new tasks are accepted.
        this.taskCounter.incrementAndGet();
        final ProcessSession session = sessionFactory.createSession();
        try {
            /*
             * We can't be doing double null check here since as a pattern
             * it only works for lazy init but not reset, which is what we
             * are doing here. In fact the first null check is dangerous
             * since 'kafkaResource' can become null right after its null
             * check passed causing subsequent NPE.
             */
            synchronized (this) {
                if (this.kafkaResource == null) {
                    this.kafkaResource = this.buildKafkaResource(context, session);
                }
            }

            /*
             * The 'processed' boolean flag does not imply any failure or success. It simply states that:
             * - ConsumeKafka - some messages were received form Kafka and 1_ FlowFile were generated
             * - PublishKafka - some messages were sent to Kafka based on existence of the input FlowFile
             */
            boolean processed = this.rendezvousWithKafka(context, session);
            session.commit();
            if (processed) {
                this.postCommit(context);
            } else {
                context.yield();
            }
        } catch (Throwable e) {
            this.acceptTask = false;
            session.rollback(true);
            this.getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, e });
        } finally {
            synchronized (this) {
                if (this.taskCounter.decrementAndGet() == 0 && !this.acceptTask) {
                    this.close();
                    this.acceptTask = true;
                }
            }
        }
    } else {
        context.yield();
    }
}
 
Example 14
Source File: PutSplunk.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    // first complete any batches from previous executions
    FlowFileMessageBatch batch;
    while ((batch = completeBatches.poll()) != null) {
        batch.completeSession();
    }

    // create a session and try to get a FlowFile, if none available then close any idle senders
    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;
    }

    // get a sender from the pool, or create a new one if the pool is empty
    // if we can't create a new connection then route flow files to failure and yield
    // acquireSender will handle the routing to failure and yielding
    ChannelSender sender = acquireSender(context, session, flowFile);
    if (sender == null) {
        return;
    }

    try {
        String delimiter = context.getProperty(MESSAGE_DELIMITER).evaluateAttributeExpressions(flowFile).getValue();
        if (delimiter != null) {
            delimiter = delimiter.replace("\\n", "\n").replace("\\r", "\r").replace("\\t", "\t");
        }

        // if no delimiter then treat the whole FlowFile as a single message
        if (delimiter == null) {
            processSingleMessage(context, session, flowFile, sender);
        } else {
            processDelimitedMessages(context, session, flowFile, sender, delimiter);
        }

    } finally {
        relinquishSender(sender);
    }
}
 
Example 15
Source File: PutDruidRecord.java    From nifi with Apache License 2.0 4 votes vote down vote up
public void onTrigger(ProcessContext context, ProcessSessionFactory factory) throws ProcessException {
    final ProcessSession session = factory.createSession();
    processFlowFile(context, session);
}
 
Example 16
Source File: AbstractKafkaProcessor.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Override
public final void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException {
    if (this.acceptTask) { // acts as a circuit breaker to allow existing tasks to wind down so 'kafkaResource' can be reset before new tasks are accepted.
        this.taskCounter.incrementAndGet();
        final ProcessSession session = sessionFactory.createSession();
        try {
            /*
             * We can't be doing double null check here since as a pattern
             * it only works for lazy init but not reset, which is what we
             * are doing here. In fact the first null check is dangerous
             * since 'kafkaResource' can become null right after its null
             * check passed causing subsequent NPE.
             */
            synchronized (this) {
                if (this.kafkaResource == null) {
                    this.kafkaResource = this.buildKafkaResource(context, session);
                }
            }

            /*
             * The 'processed' boolean flag does not imply any failure or success. It simply states that:
             * - ConsumeKafka - some messages were received form Kafka and 1_ FlowFile were generated
             * - PublishKafka - some messages were sent to Kafka based on existence of the input FlowFile
             */
            boolean processed = this.rendezvousWithKafka(context, session);
            session.commit();
            if (processed) {
                this.postCommit(context);
            } else {
                context.yield();
            }
        } catch (Throwable e) {
            this.acceptTask = false;
            session.rollback(true);
            this.getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, e });
        } finally {
            synchronized (this) {
                if (this.taskCounter.decrementAndGet() == 0 && !this.acceptTask) {
                    this.close();
                    this.acceptTask = true;
                }
            }
        }
    } else {
        context.yield();
    }
}
 
Example 17
Source File: PutSplunk.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    // first complete any batches from previous executions
    FlowFileMessageBatch batch;
    while ((batch = completeBatches.poll()) != null) {
        batch.completeSession();
    }

    // create a session and try to get a FlowFile, if none available then close any idle senders
    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;
    }

    // get a sender from the pool, or create a new one if the pool is empty
    // if we can't create a new connection then route flow files to failure and yield
    // acquireSender will handle the routing to failure and yielding
    ChannelSender sender = acquireSender(context, session, flowFile);
    if (sender == null) {
        return;
    }

    try {
        String delimiter = context.getProperty(MESSAGE_DELIMITER).evaluateAttributeExpressions(flowFile).getValue();
        if (delimiter != null) {
            delimiter = delimiter.replace("\\n", "\n").replace("\\r", "\r").replace("\\t", "\t");
        }

        // if no delimiter then treat the whole FlowFile as a single message
        if (delimiter == null) {
            processSingleMessage(context, session, flowFile, sender);
        } else {
            processDelimitedMessages(context, session, flowFile, sender, delimiter);
        }

    } finally {
        relinquishSender(sender);
    }
}