Search in sources :

Example 1 with ChannelSender

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

the class AbstractPutEventProcessor method closeSenders.

@OnStopped
public void closeSenders() {
    if (senderPool != null) {
        ChannelSender sender = senderPool.poll();
        while (sender != null) {
            sender.close();
            sender = senderPool.poll();
        }
    }
}
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) OnStopped(org.apache.nifi.annotation.lifecycle.OnStopped)

Example 2 with ChannelSender

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

the class AbstractPutEventProcessor method pruneIdleSenders.

/**
 * Close any senders that haven't been active with in the given threshold
 *
 * @param idleThreshold the threshold to consider a sender as idle
 * @return the number of connections that were closed as a result of being idle
 */
protected PruneResult pruneIdleSenders(final long idleThreshold) {
    int numClosed = 0;
    int numConsidered = 0;
    long currentTime = System.currentTimeMillis();
    final List<ChannelSender> putBack = new ArrayList<>();
    // if a connection hasn't been used with in the threshold then it gets closed
    ChannelSender sender;
    while ((sender = senderPool.poll()) != null) {
        numConsidered++;
        if (currentTime > (sender.getLastUsed() + idleThreshold)) {
            getLogger().debug("Closing idle connection...");
            sender.close();
            numClosed++;
        } else {
            putBack.add(sender);
        }
    }
    // re-queue senders that weren't idle, but if the queue is full then close the sender
    for (ChannelSender putBackSender : putBack) {
        boolean returned = senderPool.offer(putBackSender);
        if (!returned) {
            putBackSender.close();
        }
    }
    return new PruneResult(numClosed, numConsidered);
}
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) ArrayList(java.util.ArrayList)

Example 3 with ChannelSender

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

the class AbstractPutEventProcessor method acquireSender.

/**
 * Helper method to acquire an available ChannelSender from the pool. If the pool is empty then the a new sender is created.
 *
 * @param context
 *            - the current process context.
 *
 * @param session
 *            - the current process session.
 * @param flowFile
 *            - the FlowFile being processed in this session.
 *
 * @return ChannelSender - the sender that has been acquired or null if no sender is available and a new sender cannot be created.
 */
protected ChannelSender acquireSender(final ProcessContext context, final ProcessSession session, final FlowFile flowFile) {
    ChannelSender sender = senderPool.poll();
    if (sender == null) {
        try {
            getLogger().debug("No available connections, creating a new one...");
            sender = createSender(context);
        } catch (IOException e) {
            getLogger().error("No available connections, and unable to create a new one, transferring {} to failure", new Object[] { flowFile }, e);
            session.transfer(flowFile, REL_FAILURE);
            session.commit();
            context.yield();
            sender = null;
        }
    }
    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) IOException(java.io.IOException)

Example 4 with ChannelSender

use of org.apache.nifi.processor.util.put.sender.ChannelSender 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 5 with ChannelSender

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

the class PutSyslog method pruneIdleSenders.

private PruneResult pruneIdleSenders(final long idleThreshold) {
    int numClosed = 0;
    int numConsidered = 0;
    long currentTime = System.currentTimeMillis();
    final List<ChannelSender> putBack = new ArrayList<>();
    // if a connection hasn't been used with in the threshold then it gets closed
    ChannelSender sender;
    while ((sender = senderPool.poll()) != null) {
        numConsidered++;
        if (currentTime > (sender.getLastUsed() + idleThreshold)) {
            getLogger().debug("Closing idle connection...");
            sender.close();
            numClosed++;
        } else {
            putBack.add(sender);
        }
    }
    // re-queue senders that weren't idle, but if the queue is full then close the sender
    for (ChannelSender putBackSender : putBack) {
        boolean returned = senderPool.offer(putBackSender);
        if (!returned) {
            putBackSender.close();
        }
    }
    return new PruneResult(numClosed, numConsidered);
}
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) ArrayList(java.util.ArrayList)

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