Search in sources :

Example 1 with SocketChannelRecordReaderDispatcher

use of org.apache.nifi.record.listen.SocketChannelRecordReaderDispatcher in project nifi by apache.

the class ListenTCPRecord method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
    this.port = context.getProperty(PORT).evaluateAttributeExpressions().asInteger();
    final int readTimeout = context.getProperty(READ_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue();
    final int maxSocketBufferSize = context.getProperty(MAX_SOCKET_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    final int maxConnections = context.getProperty(MAX_CONNECTIONS).asInteger();
    final RecordReaderFactory recordReaderFactory = context.getProperty(RECORD_READER).asControllerService(RecordReaderFactory.class);
    // if the Network Interface Property wasn't provided then a null InetAddress will indicate to bind to all interfaces
    final InetAddress nicAddress;
    final String nicAddressStr = context.getProperty(NETWORK_INTF_NAME).evaluateAttributeExpressions().getValue();
    if (!StringUtils.isEmpty(nicAddressStr)) {
        NetworkInterface netIF = NetworkInterface.getByName(nicAddressStr);
        nicAddress = netIF.getInetAddresses().nextElement();
    } else {
        nicAddress = null;
    }
    SSLContext sslContext = null;
    SslContextFactory.ClientAuth clientAuth = null;
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    if (sslContextService != null) {
        final String clientAuthValue = context.getProperty(CLIENT_AUTH).getValue();
        sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.valueOf(clientAuthValue));
        clientAuth = SslContextFactory.ClientAuth.valueOf(clientAuthValue);
    }
    // create a ServerSocketChannel in non-blocking mode and bind to the given address and port
    final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    serverSocketChannel.configureBlocking(false);
    serverSocketChannel.bind(new InetSocketAddress(nicAddress, port));
    this.dispatcher = new SocketChannelRecordReaderDispatcher(serverSocketChannel, sslContext, clientAuth, readTimeout, maxSocketBufferSize, maxConnections, recordReaderFactory, socketReaders, getLogger());
    // start a thread to run the dispatcher
    final Thread readerThread = new Thread(dispatcher);
    readerThread.setName(getClass().getName() + " [" + getIdentifier() + "]");
    readerThread.setDaemon(true);
    readerThread.start();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketChannelRecordReaderDispatcher(org.apache.nifi.record.listen.SocketChannelRecordReaderDispatcher) NetworkInterface(java.net.NetworkInterface) SSLContext(javax.net.ssl.SSLContext) RecordReaderFactory(org.apache.nifi.serialization.RecordReaderFactory) SslContextFactory(org.apache.nifi.security.util.SslContextFactory) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) InetAddress(java.net.InetAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Aggregations

InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 NetworkInterface (java.net.NetworkInterface)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SSLContext (javax.net.ssl.SSLContext)1 OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)1 SocketChannelRecordReaderDispatcher (org.apache.nifi.record.listen.SocketChannelRecordReaderDispatcher)1 SslContextFactory (org.apache.nifi.security.util.SslContextFactory)1 RecordReaderFactory (org.apache.nifi.serialization.RecordReaderFactory)1 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)1 SSLContextService (org.apache.nifi.ssl.SSLContextService)1