@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.initialize(monitor); // Initialize the wrapped connector component. initializeNestedComponent(getOutboundConnector(), monitor, true); }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { getLogger().info( "Consumer connecting to Kafka: " + getMicroservice().getInstanceSettings().getKafkaBootstrapServers()); getLogger().info("Will be consuming messages from: " + getSourceTopicNames()); this.consumer = new KafkaConsumer<>(buildConfiguration()); this.executor = Executors.newSingleThreadExecutor(new MicroserviceConsumerThreadFactory()); executor.execute(new MessageConsumer()); }
getConsumer().subscribe(getSourceTopicNames()); break; } catch (SiteWhereException e) { getLogger().error("Unable to subscribe to topics.", e); } catch (Throwable e) { getLogger().error("Unhandled exception while subscribing to topics.", e); ConsumerRecords<String, byte[]> records = getConsumer().poll(Duration.ofMillis(Long.MAX_VALUE)); for (TopicPartition topicPartition : records.partitions()) { try { List<ConsumerRecord<String, byte[]>> topicRecords = records.records(topicPartition); process(topicPartition, topicRecords); } catch (Throwable e) { getLogger().error("Unhandled exception in consumer processing.", e); getLogger().info("Consumer thread received shutdown request."); getConsumer().unsubscribe(); } finally { getConsumer().close();
/** * Build configuration settings used by Kafka streams. * * @return * @throws SiteWhereException */ protected Properties buildConfiguration() throws SiteWhereException { Properties config = new Properties(); config.put(ConsumerConfig.CLIENT_ID_CONFIG, getConsumerId()); config.put(ConsumerConfig.GROUP_ID_CONFIG, getConsumerGroupId()); config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, getMicroservice().getInstanceSettings().getKafkaBootstrapServers()); config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class.getName()); config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); config.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false); return config; }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { // Create step that will start components. ICompositeLifecycleStep start = new CompositeLifecycleStep("Start " + getComponentName()); // Start device state processing logic. start.addStartStep(this, getDeviceStateProcessingLogic(), true); // Execute startup steps. start.execute(monitor); super.start(monitor); }
@Override public void stop(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.stop(monitor); if (getBatchProcessors() != null) { getBatchProcessors().shutdown(); try { getBatchProcessors().awaitTermination(10, TimeUnit.SECONDS); } catch (InterruptedException e) { getLogger().error("Batch processors for connector did not terminate within timout period."); } } // Stop the wrapped connector component. stopNestedComponent(getOutboundConnector(), monitor); }
@Override public void stop(ILifecycleProgressMonitor monitor) throws SiteWhereException { if (getConsumer() != null) { getConsumer().wakeup(); } if (executor != null) { executor.shutdown(); } }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.start(monitor); // Start the wrapped connector component. startNestedComponent(getOutboundConnector(), monitor, true); int numThreads = getOutboundConnector().getNumProcessingThreads(); getLogger().info(String.format("Connector host starting connector with pool of %d %s.", numThreads, numThreads == 1 ? "thread" : "threads")); this.batchProcessors = Executors.newFixedThreadPool(getOutboundConnector().getNumProcessingThreads(), new EventPayloadProcessorThreadFactory()); }
@Override public void stop(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.stop(monitor); // Create step that will stop components. ICompositeLifecycleStep stop = new CompositeLifecycleStep("Stop " + getComponentName()); // Stop inbound processing logic. stop.addStopStep(this, getInboundPayloadProcessingLogic()); // Execute shutdown steps. stop.execute(monitor); }
@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { // Create step that will start components. ICompositeLifecycleStep start = new CompositeLifecycleStep("Start " + getComponentName()); // Start inbound processing logic. start.addStartStep(this, getInboundPayloadProcessingLogic(), true); // Execute startup steps. start.execute(monitor); getLogger().info("Allocating " + getInboundProcessingConfiguration().getProcessingThreadCount() + " threads for inbound event processing."); super.start(monitor); }
@Override public void stop(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.stop(monitor); // Create step that will stop components. ICompositeLifecycleStep stop = new CompositeLifecycleStep("Stop " + getComponentName()); // Stop device state logic. stop.addStopStep(this, getDeviceStateProcessingLogic()); // Execute shutdown steps. stop.execute(monitor); }
@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.initialize(monitor); // Create step that will initialize components. ICompositeLifecycleStep init = new CompositeLifecycleStep("Initialize " + getComponentName()); // Initialize inbound processing logic. init.addInitializeStep(this, getInboundPayloadProcessingLogic(), true); // Execute initialization steps. init.execute(monitor); }
@Override public void initialize(ILifecycleProgressMonitor monitor) throws SiteWhereException { super.initialize(monitor); // Create processing logic component. this.deviceStateProcessingLogic = new DeviceStateProcessingLogic(); // Create step that will initialize components. ICompositeLifecycleStep init = new CompositeLifecycleStep("Initialize " + getComponentName()); // Initialize device state processing logic. init.addInitializeStep(this, getDeviceStateProcessingLogic(), true); // Execute initialization steps. init.execute(monitor); }