Skip to content

• Micronaut

This guide was built based on the Micronaut Cassandra Guide


Micronaut is a modern, JVM-based, full stack Java framework designed for building modular, easily testable JVM applications with support for Java, Kotlin, and Groovy. Micronaut is developed by the creators of the Grails framework and takes inspiration from lessons learnt over the years building real-world applications from monoliths to microservices using Spring, Spring Boot and Grails. For more information refer to the user guide

The micronaut-cassandra module includes support for integrating Micronaut services with Cassandra.



You can find a working sample here

✅ Step 1: Create your project

  • To create a micronaut project and CLI mn is provided. You can install it using sdkman as describe in the doc
#Download SDKMan
curl -s | bash

#Setup SDKMan
source "$HOME/.sdkman/bin/"

#Download Micronaut
sdk install micronaut
  • To generate a new project use mn create-app adding the feature cassandra
mn create-app astra-todo-micronaut --features cassandra
  • Notice that in your pom.xml you now have the following

✅ Step 2: Setup your project

All configuration of your project will be defined in application.yaml in src/main/resources. The module is clever enough to load all properties as if it was the driver configuration file.

You can defined multiple profiles and each profile will be identified with key cassandra.${profile_name}. There is a default profile. In the following sample file we provide 2 profiles one for local and one for Astra. There is no extra code needed, simply configuration.

      session-keyspace: micronaut
        - "localhost:9042"
        local-datacenter: datacenter1
        timeout: 5 seconds
        consistency: LOCAL_QUORUM
        page-size: 5000
      session-keyspace: micronaut
        secure-connect-bundle: /Users/cedricklunven/Downloads/
        class: PlainTextAuthProvider
        username: token
        password: "AstraCS:blahblahblah"
        init-query-timeout: 10 seconds
        set-keyspace-timeout: 10 seconds
      control-connection.timeout: 10 seconds

✅ Step 3: Application Startup

At startup you may want create the different tables needed for you application. In Astra you can only create keyspaces from the devops API or the user interface..

To enable content at startup simple implement ApplicationEventListener<ServiceReadyEvent> as shown below

public class TodoApplicationStartup  implements ApplicationEventListener<ServiceReadyEvent> {

    /** Logger for the class. */
    private static final Logger LOGGER = LoggerFactory.getLogger(TodoApplicationStartup.class);

    @Property(name = "todo.cassandra.create_schema", defaultValue="false")
    private boolean createTable;

    private CqlSession cqlSession;

    /** {@inheritDoc} */
    public void onApplicationEvent(final ServiceReadyEvent event) {"Startup Initialization");
        if (createTable) {
  "+ Table TodoItems created if needed.");

✅ Step 4: Use Cassandra

To use Cassandra you will reuse the CqlSession from the DataStax drivers. You can simply inject it where you needed as shown in this sample code

public class TodoRestController {

    /** Logger for our Client. */
    private static final Logger LOGGER = LoggerFactory.getLogger(TodoRestController.class);

    /** CqlSession initialized from application.yaml */
    private CqlSession cqlSession;

Happy coding.

Last update: 2023-10-13