Search in sources :

Example 11 with ChannelSender

use of org.apache.nifi.processor.util.put.sender.ChannelSender in project nifi by apache.

the class PutUDP method onTrigger.

/**
 * 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);
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) ChannelSender(org.apache.nifi.processor.util.put.sender.ChannelSender) IOException(java.io.IOException) ProcessException(org.apache.nifi.processor.exception.ProcessException) StopWatch(org.apache.nifi.util.StopWatch)

Aggregations

ChannelSender (org.apache.nifi.processor.util.put.sender.ChannelSender)11 SocketChannelSender (org.apache.nifi.processor.util.put.sender.SocketChannelSender)9 DatagramChannelSender (org.apache.nifi.processor.util.put.sender.DatagramChannelSender)8 SSLSocketChannelSender (org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender)8 IOException (java.io.IOException)4 FlowFile (org.apache.nifi.flowfile.FlowFile)4 ProcessSession (org.apache.nifi.processor.ProcessSession)3 StopWatch (org.apache.nifi.util.StopWatch)3 Charset (java.nio.charset.Charset)2 ArrayList (java.util.ArrayList)2 OnStopped (org.apache.nifi.annotation.lifecycle.OnStopped)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SSLContext (javax.net.ssl.SSLContext)1