Tuesday, 2 May 2023

Micronaut: map a property to Duration

Micronaut support the conversion a property value to Duration object. Following table summarize the example.

 

Configuration value

Duration

30ms

Duration of 30 milliseconds

30m

Duration of 30 minutes

30s

Duration of 30 seconds

30d

Duration of 30 days

30h

Duration of 30 hours

30ns

Duration of 30 nanoseconds

PT30M

Duration of 30 minutes using ISO-8601 format

 

For example, below snippet configure application sleep time and connection timeouts. 

my:
  app:
    sleepTime: 15s
    connectionTimeout: 5m

 

We can read sleepTime and connectionTimeout values as Duration objects like below.

@ConfigurationProperties("my.app")
public class AppConfig {

	private Duration sleepTime;
	private Duration connectionTimeout;

	...........
	...........
}

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-duration-conversion’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>micronaut-duration-conversion</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.3</micronaut.version>
		<slf4j.version>2.0.3</slf4j.version>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

		<maven.compiler.target>15</maven.compiler.target>
		<maven.compiler.source>15</maven.compiler.source>
	</properties>

	<dependencies>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject-java</artifactId>
			<version>${micronaut.version}</version>
		</dependency>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-runtime</artifactId>
			<version>${micronaut.version}</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.version}</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven-compiler-plugin.version}</version>
				<configuration>
					<annotationProcessorPaths>
						<path>
							<groupId>io.micronaut</groupId>
							<artifactId>micronaut-inject-java</artifactId>
							<version>${micronaut.version}</version>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>

				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>
</project>

 

Step 3: Create application.yml file under src/main/resources folder.

 

application.yml

my:
  app:
    sleepTime: 15s
    connectionTimeout: 5m

Step 4: Define AppConfig class.

 

AppConfig.java

package com.sample.app.configuration;

import java.time.Duration;

import io.micronaut.context.annotation.ConfigurationProperties;

@ConfigurationProperties("my.app")
public class AppConfig {

	private Duration sleepTime;
	private Duration connectionTimeout;

	public Duration getSleepTime() {
		return sleepTime;
	}

	public void setSleepTime(Duration sleepTime) {
		this.sleepTime = sleepTime;
	}

	public Duration getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(Duration connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

}

Step 5: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.configuration.AppConfig;

import io.micronaut.context.ApplicationContext;

public class App {

	public static void main(String[] args) {

		try (ApplicationContext applicationContext = ApplicationContext.run()) {

			AppConfig appConfig = applicationContext.getBean(AppConfig.class);

			System.out.println("sleepTime : " + appConfig.getSleepTime().getSeconds());
			System.out.println("connectionTimeout : " + appConfig.getConnectionTimeout().getSeconds());

		}

	}
}

Total project structure looks like below.




 

How to run the application?

Right click on App.java -> Run As -> Java Application.

 



You will see below messages in the console.

sleepTime : 15
connectionTimeout : 300

You can download this application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment