Search in sources :

Example 1 with SocketChannelSender

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

the class PutSyslog method createSender.

// visible for testing to override and provide a mock sender if desired
protected ChannelSender createSender(final SSLContextService sslContextService, final String protocol, final String host, final int port, final int maxSendBufferSize, final int timeout) throws IOException {
    ChannelSender sender;
    if (protocol.equals(UDP_VALUE.getValue())) {
        sender = new DatagramChannelSender(host, port, maxSendBufferSize, getLogger());
    } else {
        // if an SSLContextService is provided then we make a secure sender
        if (sslContextService != null) {
            final SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.REQUIRED);
            sender = new SSLSocketChannelSender(host, port, maxSendBufferSize, sslContext, getLogger());
        } else {
            sender = new SocketChannelSender(host, port, maxSendBufferSize, getLogger());
        }
    }
    sender.setTimeout(timeout);
    sender.open();
    return sender;
}
Also used : DatagramChannelSender(org.apache.nifi.processor.util.put.sender.DatagramChannelSender) SSLSocketChannelSender(org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender) ChannelSender(org.apache.nifi.processor.util.put.sender.ChannelSender) SocketChannelSender(org.apache.nifi.processor.util.put.sender.SocketChannelSender) DatagramChannelSender(org.apache.nifi.processor.util.put.sender.DatagramChannelSender) SSLContext(javax.net.ssl.SSLContext) SSLSocketChannelSender(org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender) SocketChannelSender(org.apache.nifi.processor.util.put.sender.SocketChannelSender) SSLSocketChannelSender(org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender)

Example 2 with SocketChannelSender

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

the class PutTCP method onTrigger.

/**
 * 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) {
        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;
    }
    // can cast to a SocketChannelSender later in order to obtain the OutputStream
    if (!(sender instanceof SocketChannelSender)) {
        getLogger().error("Processor can only be used with a SocketChannelSender, but obtained: " + sender.getClass().getCanonicalName());
        context.yield();
        return;
    }
    boolean closeSender = isConnectionPerFlowFile(context);
    try {
        // We might keep the connection open across invocations of the processor so don't auto-close this
        final OutputStream out = ((SocketChannelSender) sender).getOutputStream();
        final String delimiter = getOutgoingMessageDelimiter(context, flowFile);
        final StopWatch stopWatch = new StopWatch(true);
        try (final InputStream rawIn = session.read(flowFile);
            final BufferedInputStream in = new BufferedInputStream(rawIn)) {
            IOUtils.copy(in, out);
            if (delimiter != null) {
                final Charset charSet = Charset.forName(context.getProperty(CHARSET).getValue());
                out.write(delimiter.getBytes(charSet), 0, delimiter.length());
            }
            out.flush();
        } catch (final Exception e) {
            closeSender = true;
            throw e;
        }
        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 (closeSender) {
            getLogger().debug("Closing sender");
            sender.close();
        } else {
            getLogger().debug("Relinquishing sender");
            relinquishSender(sender);
        }
    }
}
Also used : ProcessSession(org.apache.nifi.processor.ProcessSession) FlowFile(org.apache.nifi.flowfile.FlowFile) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ChannelSender(org.apache.nifi.processor.util.put.sender.ChannelSender) SocketChannelSender(org.apache.nifi.processor.util.put.sender.SocketChannelSender) Charset(java.nio.charset.Charset) SocketChannelSender(org.apache.nifi.processor.util.put.sender.SocketChannelSender) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) StopWatch(org.apache.nifi.util.StopWatch) BufferedInputStream(java.io.BufferedInputStream)

Example 3 with SocketChannelSender

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

the class AbstractPutEventProcessor method createSender.

/**
 * Helper for sub-classes to create a sender.
 *
 * @param protocol the protocol for the sender
 * @param host the host to send to
 * @param port the port to send to
 * @param timeout the timeout for connecting and communicating over the channel
 * @param maxSendBufferSize the maximum size of the socket send buffer
 * @param sslContext an SSLContext, or null if not using SSL
 *
 * @return a ChannelSender based on the given properties
 *
 * @throws IOException if an error occurs creating the sender
 */
protected ChannelSender createSender(final String protocol, final String host, final int port, final int timeout, final int maxSendBufferSize, final SSLContext sslContext) throws IOException {
    ChannelSender sender;
    if (protocol.equals(UDP_VALUE.getValue())) {
        sender = new DatagramChannelSender(host, port, maxSendBufferSize, getLogger());
    } else {
        // if an SSLContextService is provided then we make a secure sender
        if (sslContext != null) {
            sender = new SSLSocketChannelSender(host, port, maxSendBufferSize, sslContext, getLogger());
        } else {
            sender = new SocketChannelSender(host, port, maxSendBufferSize, getLogger());
        }
    }
    sender.setTimeout(timeout);
    sender.open();
    return sender;
}
Also used : DatagramChannelSender(org.apache.nifi.processor.util.put.sender.DatagramChannelSender) SSLSocketChannelSender(org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender) ChannelSender(org.apache.nifi.processor.util.put.sender.ChannelSender) SocketChannelSender(org.apache.nifi.processor.util.put.sender.SocketChannelSender) DatagramChannelSender(org.apache.nifi.processor.util.put.sender.DatagramChannelSender) SSLSocketChannelSender(org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender) SocketChannelSender(org.apache.nifi.processor.util.put.sender.SocketChannelSender) SSLSocketChannelSender(org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender)

Aggregations

ChannelSender (org.apache.nifi.processor.util.put.sender.ChannelSender)3 SocketChannelSender (org.apache.nifi.processor.util.put.sender.SocketChannelSender)3 DatagramChannelSender (org.apache.nifi.processor.util.put.sender.DatagramChannelSender)2 SSLSocketChannelSender (org.apache.nifi.processor.util.put.sender.SSLSocketChannelSender)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Charset (java.nio.charset.Charset)1 SSLContext (javax.net.ssl.SSLContext)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ProcessSession (org.apache.nifi.processor.ProcessSession)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 StopWatch (org.apache.nifi.util.StopWatch)1