Jay Ghiya
Jay Ghiya

Reputation: 504

Configuration Bean in Quarkus

This is regarding CDI spec of quarkus. Would want to understand is there a configuration bean for quarkus? How does one do any sort of configuration in quarkus?

Upvotes: 2

Views: 4145

Answers (2)

Jay Ghiya
Jay Ghiya

Reputation: 504

First of all reading how cdi spec of quarkus differs from spring is important.

Please refer this guide: https://quarkus.io/guides/cdi-reference

The learnings from this guide is there is @Produces which is an alternative to @Configuration bean in Quarkus.

Let us take an example for libs that might require a configuration through code. Example: Microsoft Azure IOT Service Client.

public class IotHubConfiguration {


    @ConfigProperty(name="iothub.device.connection.string")
    String connectionString;


    private static final Logger LOG = Logger.getLogger(IotHubConfiguration.class);

    @Produces
    public ServiceClient getIot() throws URISyntaxException, IOException {
        LOG.info("Inside Service Client bean");
        if(connectionString==null) {
            LOG.info("Connection String is null");
            throw new RuntimeException("IOT CONNECTION STRING IS NULL");
        }

        ServiceClient serviceClient = new ServiceClient(connectionString, IotHubServiceClientProtocol.AMQPS);
        serviceClient.open();
        LOG.info("opened Service Client Successfully");

        return serviceClient;
    }



For all libs vertically intergrated with quarkus application.properties can be used and then you will get a driver obj for that broker/dbs available directly through @Inject in your @applicationScoped/@Singleton bean So, Why is that?

  1. To Simplify and Unify Configuration
  2. To Make Sure no code is required for configuring anything i.e. database config, broker config , quarkus config etc.

This drastically reduces the amount of code written for configuring and also Junits needed to cover that code.

Let us take an example where kafka producer configuration needs to be added: in application.properties

kafka.bootstrap.servers=${KAFKA_BROKER_URL:localhost:9092}
mp.messaging.outgoing.incoming_kafka_topic_test.topic=${KAFKA_INPUT_TOPIC_FOR_IOT_HUB:input_topic1}
mp.messaging.outgoing.incoming_kafka_topic_test.connector=smallrye-kafka
mp.messaging.outgoing.incoming_kafka_topic_test.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
mp.messaging.outgoing.incoming_kafka_topic_test.key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
mp.messaging.outgoing.incoming_kafka_topic_test.health-readiness-enabled=true

For full blown project reference: https://github.com/JayGhiya/QuarkusExperiments/tree/initial_version_v1/KafkaProducerQuarkus

Quarkus References for Config:

https://quarkus.io/guides/config-reference

Example for reactive sql config: https://quarkus.io/guides/reactive-sql-clients

Now let us talk about a bonus feature that quarkus provides which improves developer experience by atleast an order of magnitude that is profile driven development and testing.

Quarkus provides three profiles:

  • dev - Activated when in development mode (i.e. quarkus:dev)

  • test - Activated when running tests

  • prod - The default profile when not running in development or test mode

Let us just say that in the given example you wanted to have different topics for development and different topics for production. Let us achieve that!

%dev.mp.messaging.outgoing.incoming_kafka_topic_test.topic=${KAFKA_INPUT_TOPIC_FOR_IOT_HUB:input_topic1}
%prod.mp.messaging.outgoing.incoming_kafka_topic_test.topic=${KAFKA_INPUT_TOPIC_FOR_IOT_HUB:prod_topic}

This is how simple it is. This is extremely useful in cases where your deployments run with ssl enabled brokers/dbs etc and for dev purposes you have unsecure local brokers/dbs. This is a game changer.

Upvotes: 0

Martin Kouba
Martin Kouba

Reputation: 1526

If I get it right the original question is about @Configuration classes that can contain @Bean definitions. If so then CDI producer methods and fields annotated with @javax.enterprise.inject.Produces are the corresponding alternative.

Application configuration is a completely different question though and Jay is right that the Quarkus configuration reference is the ultimate source of information ;-).

Upvotes: 3

Related Questions