@Override public void start(ILifecycleProgressMonitor monitor) throws SiteWhereException { getLogger().info( "Producer connecting to Kafka: " + getMicroservice().getInstanceSettings().getKafkaBootstrapServers()); getLogger().info("Will be producing messages for: " + getTargetTopicName()); this.producer = new KafkaProducer<String, byte[]>(buildConfiguration()); this.kafkaAdmin = AdminClient.create(buildAdminConfiguration()); waitForKafkaAvailable(); }
@Override public Future<RecordMetadata> send(String key, byte[] message) throws SiteWhereException { ProducerRecord<String, byte[]> record = new ProducerRecord<String, byte[]>(getTargetTopicName(), key, message); try { return getProducer().send(record); } catch (IllegalStateException e) { throw new SiteWhereException("Producer unable to send record.", e); } catch (Throwable e) { throw new SiteWhereException("Unhandled exception in producer while sending record.", e); } }
/** * Build configuration settings used by producer. * * @return * @throws SiteWhereException */ protected Properties buildConfiguration() throws SiteWhereException { Properties config = new Properties(); config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, getMicroservice().getInstanceSettings().getKafkaBootstrapServers()); config.put(ProducerConfig.ACKS_CONFIG, getAckPolicy().getConfig()); config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName()); return config; }
/** * Block startup until Kafka is considered available. */ protected void waitForKafkaAvailable() { getLogger().info("Attempting to connect to Kafka..."); while (true) { try { getKafkaAdmin().listTopics().names().get(); getLogger().info("Kafka detected as available."); return; } catch (Throwable t) { getLogger().info("Kafka not detected. Will continue attempting to connect. (" + t.getMessage() + ")"); } try { Thread.sleep(KAFKA_RETRY_INTERVAL_MS); } catch (InterruptedException e) { getLogger().warn("Interrupted while waiting for Kafka to become available."); return; } } }
@Override public void stop(ILifecycleProgressMonitor monitor) throws SiteWhereException { if (getProducer() != null) { getProducer().close(); } }
/** * Build configuration settings used by admin client. * * @return * @throws SiteWhereException */ protected Properties buildAdminConfiguration() throws SiteWhereException { Properties config = new Properties(); config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, getMicroservice().getInstanceSettings().getKafkaBootstrapServers()); return config; }