Monday 8 May 2023

Micronaut: Readable bytes conversion

@ReadableBytes annotation can be used to process the strings in the form of kb, gb, mb etc.,

 

Following table summarizes how the readable bytes conversion is happening.

 

Configuration value

Resulting value

10mb

10  megabytes

10kb

10 kilobytes

10gb

10 gigabytes

1024

Raw byte length

 

Using ReadableBytes on setter method

public void setTenMB(@ReadableBytes Integer tenMB) {
	this.tenMB = tenMB;
}

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-readable-bytes’.

 

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-readable-bytes</artifactId>
	<version>1</version>

	<parent>
		<groupId>io.micronaut</groupId>
		<artifactId>micronaut-parent</artifactId>
		<version>3.7.3</version>
	</parent>


	<properties>
		<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</artifactId>
			<scope>compile</scope>
		</dependency>


		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject-java</artifactId>
		</dependency>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-runtime</artifactId>
		</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>
						</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:
    tenMB: 10mb
    tenKB: 10kb
    tenGB: 10gb

Step 4: Define AppConfig class.

AppConfig.java

package com.sample.app.configuration;

import io.micronaut.context.annotation.ConfigurationProperties;
import io.micronaut.core.convert.format.ReadableBytes;

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

	private Integer tenMB;

	private Integer tenKB;

	private Long tenGB;

	public Integer getTenMB() {
		return tenMB;
	}

	public void setTenMB(@ReadableBytes Integer tenMB) {
		this.tenMB = tenMB;
	}

	public Integer getTenKB() {
		return tenKB;
	}

	public void setTenKB(@ReadableBytes Integer tenKB) {
		this.tenKB = tenKB;
	}

	public Long getTenGB() {
		return tenGB;
	}

	public void setTenGB(@ReadableBytes Long tenGB) {
		this.tenGB = tenGB;
	}

}

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("10kb = " + appConfig.getTenKB() + " bytes");
			System.out.println("10mb = " + appConfig.getTenMB() + " bytes");
			System.out.println("10gb = " + appConfig.getTenGB() + " bytes");

		}

	}
}

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-readable-bytes-1-jar-with-dependencies.jar’ in project target folder.

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

Execute below command to run the application.

$java -jar ./target/micronaut-readable-bytes-1-jar-with-dependencies.jar
10kb = 10240 bytes
10mb = 10485760 bytes
10gb = 10737418240 bytes

You can download this application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment