this.kafkaResource = this.buildKafkaResource(context, session); boolean processed = this.rendezvousWithKafka(context, session); session.commit(); if (processed) { this.postCommit(context); } else { context.yield(); this.acceptTask = false; session.rollback(true); this.getLogger().error("{} failed to process due to {}; rolling back session", new Object[] { this, e }); } finally { synchronized (this) { if (this.taskCounter.decrementAndGet() == 0 && !this.acceptTask) { this.close(); this.acceptTask = true; this.getLogger().debug("Task was not accepted due to the processor being in 'reset' state. It will be re-submitted upon completion of the reset."); context.yield();
/** * Will unsubscribe form {@link KafkaConsumer} delegating to 'super' to do * the rest. */ @Override @OnStopped public void close() { if (this.kafkaResource != null) { try { this.kafkaResource.unsubscribe(); } finally { // in the event the above fails super.close(); } } }
/** * Will call {@link Closeable#close()} on the target resource after which * the target resource will be set to null. Should only be called when there * are no more threads being executed on this processor or when it has been * verified that only a single thread remains. * * @see KafkaPublisher * @see KafkaConsumer */ @OnStopped public void close() { try { if (this.kafkaResource != null) { try { this.kafkaResource.close(); } catch (Exception e) { this.getLogger().warn("Failed while closing " + this.kafkaResource, e); } } } finally { this.kafkaResource = null; } }
/** * Builds Kafka {@link Properties} */ Properties buildKafkaProperties(ProcessContext context) { Properties properties = new Properties(); for (PropertyDescriptor propertyDescriptor : context.getProperties().keySet()) { if (propertyDescriptor.equals(SSL_CONTEXT_SERVICE)) { // Translate SSLContext Service configuration into Kafka properties final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class); buildSSLKafkaProperties(sslContextService, properties); continue; } String pName = propertyDescriptor.getName(); String pValue = propertyDescriptor.isExpressionLanguageSupported() ? context.getProperty(propertyDescriptor).evaluateAttributeExpressions().getValue() : context.getProperty(propertyDescriptor).getValue(); if (pValue != null) { if (pName.endsWith(".ms")) { // kafka standard time notation pValue = String.valueOf(FormatUtils.getTimeDuration(pValue.trim(), TimeUnit.MILLISECONDS)); } properties.setProperty(pName, pValue); } } return properties; }