If your application does blocking I/O such as communication via JDBC or JPA you should annotate all controllers that interact with these blocking resources with the annotation specifying either the default I/O thread pool or a thread pool that you configure an manage to deal with these blocking interactions. For example:
Using @ExecuteOn
Using @ExecuteOn
import io.micronaut.http.annotation.*
import io.micronaut.scheduling.TaskExecutors
import io.micronaut.scheduling.annotation.ExecuteOn
@Controller("/executeOn/people")
class PersonController {
private final PersonService personService
PersonController(
PersonService personService) {
this.personService = personService
}
@Get("/{name}")
@ExecuteOn(TaskExecutors.IO) (1)
return personService.findByName(name)
}
}
Using @ExecuteOn
import io.micronaut.http.annotation.*
import io.micronaut.scheduling.TaskExecutors
import io.micronaut.scheduling.annotation.ExecuteOn
@Controller("/executeOn/people")
class PersonController (private val personService: PersonService) {
@Get("/{name}")
@ExecuteOn(TaskExecutors.IO) (1)
fun byName(name: String): Person {
return personService.findByName(name)
}
}
Compilation Error with classes in io.micronaut.cache
package
If your application fails to compile due to missing classes in the io.micronaut.cache
you need to add a dependency on a cache implementation, for example the default cache implementation was Caffeine and this can be restored with the following dependency:
implementation("io.micronaut.cache:micronaut-cache-caffeine")
Compilation Error with classes in javax.annotation
package
If you application fails to compile referencing classes in the javax.annotation
package such as @Nullable
and @Nonnull
you should update your project to instead import from Spot Bugs.
Some dependencies have new Maven Group IDs so you may need to update your dependency. The following table summarizes changes to group IDs:
AWS Module Changes
The AWS functionality (including support for AWS ParameterStore, AWS Route53 and AWS Lambda Function Client) has been moved out of the Micronaut core therefore if you need AWS related functionality you should use the necessary module.
Kubernetes Discovery Service deprecation
The class io.micronaut.discovery.kubernetes.KubernetesDiscoveryClient
, that was deprecated in Micronaut 1.2, has now been removed from Micronaut 2.0. The replacement is the new Micronaut Kubernetes module, with an improved support for running Micronaut applications in a Kubernetes cluster, including support for Kubernetes’ ConfigMap
s, Secret
s and more.
To use the new module, you need to add the following dependency:
implementation("io.micronaut.kubernetes:micronaut-kubernetes-discovery-client")
<dependency>
<groupId>io.micronaut.kubernetes</groupId>
<artifactId>micronaut-kubernetes-discovery-client</artifactId>
</dependency>
For Micronaut 2.0, we recommend you to use the native Micronaut-based transaction management instead of other alternatives such as Spring Transaction Management.
You will use and io.micronaut.transaction.annotation.ReadOnly to mark your transaction demarcations.
To use those annotations, you have to include the micronaut-data-processor
dependency in your annotation processor configuration:
annotationProcessor("io.micronaut.data:micronaut-data-processor")
If you use Hibernate/JPA, you will need to add as well:
implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
<dependency>
<groupId>io.micronaut.data</groupId>
</dependency>
Then, code such as:
import io.micronaut.spring.tx.annotation.Transactional;
...
..
.
@Singleton
public class GenreRepositoryImpl implements GenreRepository {
...
..
.
@Transactional(readOnly = true)
public Optional<Genre> findById(@NotNull Long id) {
...
..
}
@Transactional
public Genre save(@NotBlank String name) {
...
..
}
}
becomes:
Micronaut 2 for Groovy Users
Micronaut 2 defaults to Groovy 3 and Spock 2 both of which include significant changes at the language and testing framework level.
In the case of Spock 2 the most important change is that Spock 2 deprecates support for JUnit 4 and the associated JUnit 4 test runner and replaces it with JUnit 5 Platform.
In the case of a Gradle build this change means that when upgrading to Micronaut 2 with Spock 2 you may find your tests don’t execute at all which can give you the false sense of security that the upgrade was successful.
To ensure your Spock 2 tests run in a Micronaut 2 Gradle build you must add the following configuration to your build.gradle
to enable JUnit 5 platform:
Using JUnit Platform
test {
useJUnitPlatform()
With this configuration is place your Spock 2 tests will execute correctly.