ottobackwards commented on code in PR #958: URL: https://github.com/apache/plc4x/pull/958#discussion_r1209474104
########## plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/Plc4xListenRecordProcessorTest.java: ########## @@ -0,0 +1,80 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.plc4x.nifi; + +import org.apache.nifi.avro.AvroRecordSetWriter; +import org.apache.nifi.reporting.InitializationException; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.apache.plc4x.nifi.address.AddressesAccessUtils; +import org.apache.plc4x.nifi.util.Plc4xCommonTest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class Plc4xListenRecordProcessorTest { + + private TestRunner testRunner; + private static int NUMBER_OF_CALLS = 5; + + private final AvroRecordSetWriter writerService = new AvroRecordSetWriter(); + + @BeforeEach + public void init() throws InitializationException { + testRunner = TestRunners.newTestRunner(Plc4xListenRecordProcessor.class); + testRunner.setIncomingConnection(false); + testRunner.setValidateExpressionUsage(false); + + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_FUTURE_TIMEOUT_MILISECONDS, "30000"); + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_CONNECTION_STRING, "simulated://127.0.0.1"); + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_SCHEMA_CACHE_SIZE, "1"); + + testRunner.addConnection(Plc4xListenRecordProcessor.REL_SUCCESS); + + testRunner.addControllerService("writer", writerService); + testRunner.enableControllerService(writerService); + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_RECORD_WRITER_FACTORY.getName(), "writer"); + } + Review Comment: This can be changed this to @Disabled ########## plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/main/java/org/apache/plc4x/nifi/Plc4xListenRecordProcessor.java: ########## @@ -0,0 +1,295 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.plc4x.nifi; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.apache.nifi.annotation.behavior.InputRequirement; +import org.apache.nifi.annotation.behavior.WritesAttribute; +import org.apache.nifi.annotation.behavior.WritesAttributes; +import org.apache.nifi.annotation.configuration.DefaultSchedule; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.SeeAlso; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnScheduled; +import org.apache.nifi.annotation.lifecycle.OnStopped; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.processor.ProcessContext; +import org.apache.nifi.processor.ProcessSession; +import org.apache.nifi.processor.ProcessorInitializationContext; +import org.apache.nifi.processor.Relationship; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +import org.apache.nifi.serialization.RecordSetWriterFactory; +import org.apache.nifi.serialization.record.RecordSchema; +import org.apache.nifi.util.StopWatch; +import org.apache.plc4x.java.api.messages.PlcSubscriptionEvent; +import org.apache.plc4x.java.api.model.PlcTag; +import org.apache.plc4x.java.api.types.PlcValueType; +import org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent; +import org.apache.plc4x.nifi.subscription.Plc4xListenerDispatcher; +import org.apache.plc4x.nifi.subscription.Plc4xSubscriptionType; +import org.apache.plc4x.nifi.record.Plc4xWriter; +import org.apache.plc4x.nifi.record.RecordPlc4xWriter; + +@DefaultSchedule(period="0.1 sec") +@Tags({"plc4x", "get", "input", "source", "listen", "record"}) +@SeeAlso({Plc4xSourceRecordProcessor.class, Plc4xSinkRecordProcessor.class}) +@InputRequirement(InputRequirement.Requirement.INPUT_FORBIDDEN) +@CapabilityDescription("Processor able to read data from industrial PLCs using Apache PLC4X subscriptions") +@WritesAttributes({ + @WritesAttribute(attribute = Plc4xListenRecordProcessor.RESULT_ROW_COUNT, description = "Number of rows written into the output FlowFile"), + @WritesAttribute(attribute = Plc4xListenRecordProcessor.RESULT_LAST_EVENT, description = "Time elapsed from last subscription event") + }) +public class Plc4xListenRecordProcessor extends BasePlc4xProcessor { + + public static final String RESULT_ROW_COUNT = "plc4x.listen.row.count"; + public static final String RESULT_LAST_EVENT = "plc4x.listen.lastEvent"; + + protected Plc4xSubscriptionType subscriptionType = null; + protected Long cyclingPollingInterval = null; + protected final BlockingQueue<PlcSubscriptionEvent> events = new LinkedBlockingQueue<>(); + protected Plc4xListenerDispatcher dispatcher; + protected RecordSchema recordSchema; + protected Thread readerThread; + final StopWatch executeTime = new StopWatch(false); + + public static final PropertyDescriptor PLC_RECORD_WRITER_FACTORY = new PropertyDescriptor.Builder() + .name("plc4x-record-writer") + .displayName("Record Writer") + .description("Specifies the Controller Service to use for writing results to a FlowFile. The Record Writer may use Inherit Schema to emulate the inferred schema behavior, i.e. " + + "an explicit schema need not be defined in the writer, and will be supplied by the same logic used to infer the schema from the column types.") + .identifiesControllerService(RecordSetWriterFactory.class) + .required(true) + .build(); + + public static final PropertyDescriptor PLC_SUBSCRIPTION_TYPE = new PropertyDescriptor.Builder() + .name("plc4x-subscription-type") + .displayName("Subscription Type") + .description("Sets the subscription type. The subscritpion types available for each driver are stated in the documentation.") + .allowableValues(Plc4xSubscriptionType.values()) + .required(true) + .defaultValue(Plc4xSubscriptionType.CHANGE.name()) + .build(); + + public static final PropertyDescriptor PLC_SUBSCRIPTION_CYCLIC_POLLING_INTERVAL = new PropertyDescriptor.Builder() + .name("plc4x-subscription-cyclic-polling-interval") + .displayName("Cyclic polling interval") + .description("In case of Cyclic subscription type a time interval must be provided.") + .dependsOn(PLC_SUBSCRIPTION_TYPE, Plc4xSubscriptionType.CYCLIC.name()) + .required(true) + .addValidator(StandardValidators.POSITIVE_INTEGER_VALIDATOR) + .addValidator(new Validator() { + @Override + public ValidationResult validate(String subject, String input, ValidationContext context) { + if (context.getProperty(PLC_FUTURE_TIMEOUT_MILISECONDS).asLong() > Long.valueOf(input)) { + return new ValidationResult.Builder().valid(true).build(); + } else { + return new ValidationResult.Builder() + .valid(false) + .input(input) + .subject(PLC_SUBSCRIPTION_CYCLIC_POLLING_INTERVAL.getDisplayName()) + .explanation(String.format("it must me smaller than the value of %s", PLC_FUTURE_TIMEOUT_MILISECONDS.getDisplayName())) + .build(); + } + } + }) + .defaultValue("10000") + .build(); + + @Override + protected void init(final ProcessorInitializationContext context) { + super.init(context); + final Set<Relationship> relationships = new HashSet<>(); + relationships.add(REL_SUCCESS); + this.relationships = Collections.unmodifiableSet(relationships); + + final List<PropertyDescriptor> pds = new ArrayList<>(); + pds.addAll(super.getSupportedPropertyDescriptors()); + pds.add(PLC_RECORD_WRITER_FACTORY); + pds.add(PLC_SUBSCRIPTION_TYPE); + pds.add(PLC_SUBSCRIPTION_CYCLIC_POLLING_INTERVAL); + this.properties = Collections.unmodifiableList(pds); + } + + @Override + @OnScheduled + public void onScheduled(final ProcessContext context) { + super.onScheduled(context); + subscriptionType = Plc4xSubscriptionType.valueOf(context.getProperty(PLC_SUBSCRIPTION_TYPE).getValue()); + cyclingPollingInterval = context.getProperty(PLC_SUBSCRIPTION_CYCLIC_POLLING_INTERVAL).asLong(); + addressMap = getPlcAddressMap(context, null); + + createDispatcher(events); + } + + protected void createDispatcher(final BlockingQueue<PlcSubscriptionEvent> events) { + if (readerThread != null) { + return; + } + + // create the dispatcher and calls open() to start listening to the plc subscription + dispatcher = new Plc4xListenerDispatcher(timeout, subscriptionType, cyclingPollingInterval, getLogger(), events); + try { + dispatcher.open(getConnectionString(), addressMap); + } catch (Exception e) { + if (debugEnabled) { + getLogger().debug("Error creating a the subscription event dispatcher"); + e.printStackTrace(); + } + throw new ProcessException(e); + } + + if (dispatcher.isRunning()) { + readerThread = new Thread(dispatcher); + readerThread.setName(getClass().getName() + " [" + getIdentifier() + "]"); + readerThread.setDaemon(true); + readerThread.start(); + } + } + + @OnStopped + public void closeDispatcher() throws ProcessException { + executeTime.stop(); + if (readerThread != null) { + readerThread.interrupt(); Review Comment: maybe this should join() the thread to be sure ########## plc4j/integrations/apache-nifi/nifi-plc4x-processors/src/test/java/org/apache/plc4x/nifi/Plc4xListenRecordProcessorTest.java: ########## @@ -0,0 +1,80 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. + */ +package org.apache.plc4x.nifi; + +import org.apache.nifi.avro.AvroRecordSetWriter; +import org.apache.nifi.reporting.InitializationException; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.apache.plc4x.nifi.address.AddressesAccessUtils; +import org.apache.plc4x.nifi.util.Plc4xCommonTest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class Plc4xListenRecordProcessorTest { + + private TestRunner testRunner; + private static int NUMBER_OF_CALLS = 5; + + private final AvroRecordSetWriter writerService = new AvroRecordSetWriter(); + + @BeforeEach + public void init() throws InitializationException { + testRunner = TestRunners.newTestRunner(Plc4xListenRecordProcessor.class); + testRunner.setIncomingConnection(false); + testRunner.setValidateExpressionUsage(false); + + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_FUTURE_TIMEOUT_MILISECONDS, "30000"); + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_CONNECTION_STRING, "simulated://127.0.0.1"); + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_SCHEMA_CACHE_SIZE, "1"); + + testRunner.addConnection(Plc4xListenRecordProcessor.REL_SUCCESS); + + testRunner.addControllerService("writer", writerService); + testRunner.enableControllerService(writerService); + testRunner.setProperty(Plc4xListenRecordProcessor.PLC_RECORD_WRITER_FACTORY.getName(), "writer"); + } + Review Comment: And remove the todo -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@plc4x.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org