Tuesday 18 April 2023

Micronaut: working with random properties

Micronaut support following built-in properties to work with random properties.

 

Property

Description

random.port

Return a random available port number.

random.int

random.integer

Return a random integer.

random.long

Return random long value.

random.float

Return random float number.

random.shortuuid

Return random short uuid of only 10 chars in length. As this has only 10 chars in length, collisions could occur.

random.uuid

Return random uuid with dashes

random.uuid2

Random UUID without dashes

 

Properties random.int, random.integer, random.long and random.float properties supports a range suffix.

 

Example

random.int(max): where max is an exclusive value

random.int[min,max]: where min being inclusive and max being exclusive values.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-random-properties’.

 

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-random-properties</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>io.micronaut</groupId>
			<artifactId>micronaut-validation</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-validation</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
randomPort: ${random.port}

randomInt1: ${random.int}
randomInt2: ${random.integer}
randomLong: ${random.long}
randomFloat: ${random.float}

randomShortUUID: ${random.shortuuid}
randomUUID1: ${random.uuid}
randomUUID2: ${random.uuid2}

randomIntRange1: ${random.int[5,10]}
randomIntRange2: ${random.int(100)}

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;

public class App {

	public static void main(String[] args) {

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

			Environment environment = applicationContext.getEnvironment();

			Integer randomPort = environment.getProperty("randomPort", Integer.class, 0);

			Integer randomInt1 = environment.getProperty("randomInt1", Integer.class, 0);
			Integer randomInt2 = environment.getProperty("randomInt2", Integer.class, 0);
			Long randomLong = environment.getProperty("randomLong", Long.class, 0l);
			Float randomFloat = environment.getProperty("randomFloat", Float.class, 0.0f);

			String randomShortUUID = environment.getProperty("randomShortUUID", String.class, "null");
			String randomUUID1 = environment.getProperty("randomUUID1", String.class, "null");
			String randomUUID2 = environment.getProperty("randomUUID2", String.class, "null");

			Integer randomIntRange1 = environment.getProperty("randomIntRange1", Integer.class, 0);
			Integer randomIntRange2 = environment.getProperty("randomIntRange2", Integer.class, 0);

			System.out.println("randomPort : " + randomPort);
			System.out.println("randomInt1 : " + randomInt1);
			System.out.println("randomInt2 : " + randomInt2);
			System.out.println("randomLong : " + randomLong);
			System.out.println("randomFloat : " + randomFloat);
			System.out.println("randomShortUUID : " + randomShortUUID);
			System.out.println("randomUUID1 : " + randomUUID1);
			System.out.println("randomUUID2 : " + randomUUID2);
			System.out.println("randomIntRange1 : " + randomIntRange1);
			System.out.println("randomIntRange2 : " + randomIntRange2);
		}

	}
}

 

Total project structure looks like below.

 


 

Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-random-properties-1-jar-with-dependencies.jar’ in project target folder.

 

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-random-properties-1-jar-with-dependencies.jar
micronaut-random-properties-1.jar
test-classes

 

Execute below command to run the application.

java -jar ./target/micronaut-random-properties-1-jar-with-dependencies.jar

$java -jar ./target/micronaut-random-properties-1-jar-with-dependencies.jar
randomPort : 63959
randomInt1 : -1271059038
randomInt2 : -829754997
randomLong : -6764892139580613691
randomFloat : 0.9386667
randomShortUUID : 4d3d3c2375
randomUUID1 : 53d68444-7b66-4297-a765-acc6d5c19f44
randomUUID2 : 297c7f10a425434d8d43d133930d2217
randomIntRange1 : 7
randomIntRange2 : 1

 

You can download this application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment