Search in sources :

Example 36 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class ListenSyslog method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
    final int port = context.getProperty(PORT).evaluateAttributeExpressions().asInteger();
    final int bufferSize = context.getProperty(RECV_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    final int maxChannelBufferSize = context.getProperty(MAX_SOCKET_BUFFER_SIZE).asDataSize(DataUnit.B).intValue();
    final int maxMessageQueueSize = context.getProperty(MAX_MESSAGE_QUEUE_SIZE).asInteger();
    final String protocol = context.getProperty(PROTOCOL).getValue();
    final String nicIPAddressStr = context.getProperty(NETWORK_INTF_NAME).evaluateAttributeExpressions().getValue();
    final String charSet = context.getProperty(CHARSET).evaluateAttributeExpressions().getValue();
    final String msgDemarcator = context.getProperty(MESSAGE_DELIMITER).getValue().replace("\\n", "\n").replace("\\r", "\r").replace("\\t", "\t");
    messageDemarcatorBytes = msgDemarcator.getBytes(Charset.forName(charSet));
    final int maxConnections;
    if (UDP_VALUE.getValue().equals(protocol)) {
        maxConnections = 1;
    } else {
        maxConnections = context.getProperty(MAX_CONNECTIONS).asLong().intValue();
    }
    bufferPool = new LinkedBlockingQueue<>(maxConnections);
    for (int i = 0; i < maxConnections; i++) {
        bufferPool.offer(ByteBuffer.allocate(bufferSize));
    }
    parser = new SyslogParser(Charset.forName(charSet));
    syslogEvents = new LinkedBlockingQueue<>(maxMessageQueueSize);
    InetAddress nicIPAddress = null;
    if (!StringUtils.isEmpty(nicIPAddressStr)) {
        NetworkInterface netIF = NetworkInterface.getByName(nicIPAddressStr);
        nicIPAddress = netIF.getInetAddresses().nextElement();
    }
    // create either a UDP or TCP reader and call open() to bind to the given port
    final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    channelDispatcher = createChannelReader(context, protocol, bufferPool, syslogEvents, maxConnections, sslContextService, Charset.forName(charSet));
    channelDispatcher.open(nicIPAddress, port, maxChannelBufferSize);
    final Thread readerThread = new Thread(channelDispatcher);
    readerThread.setName("ListenSyslog [" + getIdentifier() + "]");
    readerThread.setDaemon(true);
    readerThread.start();
}
Also used : SyslogParser(org.apache.nifi.processors.standard.syslog.SyslogParser) SSLContextService(org.apache.nifi.ssl.SSLContextService) RestrictedSSLContextService(org.apache.nifi.ssl.RestrictedSSLContextService) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 37 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled 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)

Example 38 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class LookupAttribute method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) {
    // Load up all the dynamic properties once for use later in onTrigger
    final Map<PropertyDescriptor, PropertyValue> dynamicProperties = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> e : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = e.getKey();
        if (descriptor.isDynamic()) {
            final PropertyValue value = context.getProperty(descriptor);
            dynamicProperties.put(descriptor, value);
        }
    }
    this.dynamicProperties = Collections.unmodifiableMap(dynamicProperties);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) PropertyValue(org.apache.nifi.components.PropertyValue) HashMap(java.util.HashMap) Map(java.util.Map) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 39 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class ParseCEF method OnScheduled.

@OnScheduled
public void OnScheduled(final ProcessContext context) {
    // Configure jackson mapper before spawning onTriggers
    final SimpleModule module = new SimpleModule().addSerializer(MacAddress.class, new MacAddressToStringSerializer());
    this.mapper.registerModule(module);
    this.mapper.setDateFormat(this.simpleDateFormat);
    switch(context.getProperty(TIME_REPRESENTATION).getValue()) {
        case LOCAL_TZ:
            // set the mapper TZ to local TZ
            this.mapper.setTimeZone(TimeZone.getDefault());
            tzId = TimeZone.getDefault().getID();
            break;
        case UTC:
            // set the mapper TZ to local TZ
            this.mapper.setTimeZone(TimeZone.getTimeZone(UTC));
            tzId = UTC;
            break;
    }
}
Also used : SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 40 with OnScheduled

use of org.apache.nifi.annotation.lifecycle.OnScheduled in project nifi by apache.

the class GetSolr method clearState.

@OnScheduled
public void clearState(final ProcessContext context) throws IOException {
    if (clearState.getAndSet(false))
        context.getStateManager().clear(Scope.CLUSTER);
    final Map<String, String> stateMap = new HashMap<String, String>();
    stateMap.putAll(context.getStateManager().getState(Scope.CLUSTER).toMap());
    final AtomicBoolean stateMapHasChanged = new AtomicBoolean(false);
    if (stateMap.get(STATE_MANAGER_CURSOR_MARK) == null) {
        stateMap.put(STATE_MANAGER_CURSOR_MARK, "*");
        stateMapHasChanged.set(true);
    }
    if (stateMap.get(STATE_MANAGER_FILTER) == null) {
        final String initialDate = context.getProperty(DATE_FILTER).getValue();
        if (StringUtils.isBlank(initialDate))
            stateMap.put(STATE_MANAGER_FILTER, "*");
        else
            stateMap.put(STATE_MANAGER_FILTER, initialDate);
        stateMapHasChanged.set(true);
    }
    if (stateMapHasChanged.get())
        context.getStateManager().setState(stateMap, Scope.CLUSTER);
    id_field = null;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Aggregations

OnScheduled (org.apache.nifi.annotation.lifecycle.OnScheduled)59 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)12 ProcessException (org.apache.nifi.processor.exception.ProcessException)12 IOException (java.io.IOException)10 HashMap (java.util.HashMap)10 ComponentLog (org.apache.nifi.logging.ComponentLog)8 SSLContextService (org.apache.nifi.ssl.SSLContextService)7 Map (java.util.Map)6 SSLContext (javax.net.ssl.SSLContext)6 StateMap (org.apache.nifi.components.state.StateMap)5 FileInputStream (java.io.FileInputStream)4 PropertyValue (org.apache.nifi.components.PropertyValue)4 RestrictedSSLContextService (org.apache.nifi.ssl.RestrictedSSLContextService)4 InetAddress (java.net.InetAddress)3 CacheLoader (com.google.common.cache.CacheLoader)2 SslContextBuilder (io.netty.handler.ssl.SslContextBuilder)2 File (java.io.File)2 InputStream (java.io.InputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 NetworkInterface (java.net.NetworkInterface)2