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();
}
}
}
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);
}
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;
}
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;
}
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);
}
Aggregations