2 Writing the Application

The previous command createas a micronaut app with the default package example.micronaut in a folder named complete.

Due to the --features kotlin flag, it generates a Kotlin Micronaut app and it uses Gradle build system. However, you could use
other build tool such as Maven or other programming languages such as Java or Groovy.

If you are using Java or Kotlin and IntelliJ IDEA make sure you have enabled annotation processing.

Kotlin, Kapt and IntelliJ

As of this writing IntelliJ’s built-in compiler does not directly support Kapt and annotation processing. You must instead configure Intellij to run Gradle (or Maven) compilation as a build step before running your tests or application class.

First edit the run configuration for tests or for the application and select "Run Gradle task" as a build step:

Then add the classes task as task to execute for the application or for tests the testClasses task:

Now whenever you run tests or the application Micronaut classes will be generated at compilation time.

Second execution of 10 seconds job just 10 seconds after the first execution

4

Second execution of 45 seconds job just 45 seconds after the first execution

2.3 Business logic in dedicated Use Cases

Although the previous example is valid, usually you don’t want to put your business logic in a Job. A better approach is to create an additional bean which the Job invokes. This approach decouples your business logic from the scheduling logic. Moreover, it facilitates testing and maintenance. Let’s see an example:

To register a Singleton in Micronaut’s application context annotate your class with javax.inject.Singleton

2

Constructor injection.

3

Trigger the job once a day at 04:30 AM

4

Call the injected use case

2.4 Scheduling a Job Manually

Imagine the following scenario. You want to send every user an email 2 hours after they registered into your app. You want to ask him about his experiences during this first interaction with your application.

For this guide, we are going to schedule a Job to trigger after one minute.

To test it out, we are going to call twice a new use case named RegisterUseCase when the app starts.

Create a bean, BootStrap.kt, which listens to the application startup event.