1.2 Upgrading to Micronaut 2.x
This section covers the steps required to upgrade a Micronaut 1.x application to Micronaut 2.x.
Thread Pool Selection Now Manual
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 @ExecuteOn 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
import io.micronaut.http.annotation.*;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
@Controller("/executeOn/people")
public class PersonController {
private final PersonService personService;
PersonController(
PersonService personService) {
this.personService = personService;
}
@Get("/{name}")
@ExecuteOn(TaskExecutors.IO) (1)
Person byName(String name) {
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 final PersonService personService
PersonController(
PersonService personService) {
this.personService = personService
}
@Get("/{name}")
@ExecuteOn(TaskExecutors.IO) (1)
Person byName(String name) {
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)
}
}
1 | The @ExecuteOn annotation is used to execute the operation on the I/O thread pool |
If you wish to return to the previous behaviour as defined in Micronaut 1.x you can set micronaut.server.thread-selection to AUTO in configuration. |
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")
<dependency>
<groupId>io.micronaut.cache</groupId>
<artifactId>micronaut-cache-caffeine</artifactId>
</dependency>
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 edu.umd.cs.findbugs.annotations
from Spot Bugs.
New Group IDs
Some dependencies have new Maven Group IDs so you may need to update your dependency. The following table summarizes changes to group IDs:
Previous ID | New ID |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
Package | New Dependency |
---|---|
|
|
|
|
|
|
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>
Transaction Management
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 Java EE 7 javax.transaction.Transactional annotation 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")
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-processor</artifactId>
</dependency>
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>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
</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:
import javax.transaction.Transactional;
import io.micronaut.transaction.annotation.ReadOnly;
...
..
.
@Singleton
public class GenreRepositoryImpl implements GenreRepository {
...
..
.
@ReadOnly
public Optional<Genre> findById(@NotNull Long id) {
...
..
}
@Transactional
public Genre save(@NotBlank String name) {
...
..
}
}
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.
Some features of Micronaut 2 for Groovy users are currently in preview status including GORM 7.1 and Spock 2 support as these frameworks do not yet have stable releases. |
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.
Other Breaking Changes
If the above cases don’t cover your use case see the section on Breaking Changes for a list of other changes that are regarded as breaking in this release.