Search code examples
javaapache-camel

How get all camel component poperties using java code


I am tring to get camel component properties using java code like:

Configuration: 
Java: 11
Apache Camel: 3.18.2

for Example: if pass Kafka component in java code, get all the latest properties from Apache camel:

get all properties of Kafka :

additionalProperties, brokers, clientId, headerFilterStrategy, reconnectBackoffMaxMs, shutdownTimeout, allowManualCommit, autoCommitEnable, autoCommitIntervalMs, autoOffsetReset, breakOnFirstError, checkCrcs, commitTimeoutMs, consumerRequestTimeoutMs, consumersCount, fetchMaxBytes, fetchMinBytes, fetchWaitMaxMs, groupId, groupInstanceId, headerDeserializer, heartbeatIntervalMs, keyDeserializer, maxPartitionFetchBytes, maxPollIntervalMs, maxPollRecords, offsetRepository, partitionAssignor, pollOnError, pollTimeoutMs, seekTo, sessionTimeoutMs, specificAvroReader, topicIsPattern, valueDeserializer, bridgeErrorHandler, exceptionHandler, exchangePattern, isolationLevel, kafkaManualCommitFactory, batchWithIndividualHeaders, bufferMemorySize, compressionCodec, connectionMaxIdleMs, deliveryTimeoutMs, enableIdempotence, headerSerializer, key, keySerializer, lingerMs, maxBlockMs, maxInFlightRequest, maxRequestSize, metadataMaxAgeMs, metricReporters, metricsSampleWindowMs, noOfMetricsSample, partitioner, partitionKey, producerBatchSize, queueBufferingMaxMessages, receiveBufferBytes, reconnectBackoffMs, recordMetadata, requestRequiredAcks, requestTimeoutMs, retries, retryBackoffMs, sendBufferBytes, valueSerializer, workerPool, workerPoolCoreSize, workerPoolMaxSize, lazyStartProducer, kafkaClientFactory, synchronous, schemaRegistryURL, interceptorClasses, kerberosBeforeReloginMinTime, kerberosInitCmd, kerberosPrincipalToLocalRules, kerberosRenewJitter, kerberosRenewWindowFactor, saslJaasConfig, saslKerberosServiceName, saslMechanism, securityProtocol, sslCipherSuites, sslContextParameters, sslEnabledProtocols, sslEndpointAlgorithm, sslKeymanagerAlgorithm, sslKeyPassword, sslKeystoreLocation, sslKeystorePassword, sslKeystoreType, sslProtocol, sslProvider, sslTrustmanagerAlgorithm, sslTruststoreLocation,
sslTruststorePassword, sslTruststoreType;

I have also tried the code but some classes are not found in camel 3.18.2.

import org.apache.camel.CamelContext;
import org.apache.camel.component.kafka.KafkaComponent;
import org.apache.camel.spi.ComponentConfiguration; // class not found
import org.apache.camel.spi.Registry;
import org.apache.camel.impl.DefaultCamelContext;

import java.util.Map;

public class CamelComponentProperties {

    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.start();
        Registry registry = camelContext.getRegistry();
        KafkaComponent kafkaComponent = registry.lookupByNameAndType("kafka", KafkaComponent.class);
        ComponentConfiguration componentConfiguration = kafkaComponent.createComponentConfiguration(); // facing error this line
        Map<String, Object> parameters = componentConfiguration.getParameterValues();
        for (String parameterName : parameters.keySet()) {
            System.out.println(parameterName + " = " + parameters.get(parameterName));
        }
        camelContext.stop();
    }
}

Solution

  • One way to do this would be to use the RuntimeCamelCatalog to get a JSON description of the component.

    RuntimeCamelCatalog catalog = context.getExtension(ExtendedCamelContext.class).getRuntimeCamelCatalog()
    String componentJson = catalog.componentJSonSchema("kafka");
    

    Then you can use your preferred JSON parser to inspect the properties & componentProperties fields.