Friday, 26 May 2023

Micronaut: Method adapter advice example

A method Adapter advice applicable to a method that will create an entirely new bean definition that delegates to the annotated method.

 

For example the following snippet runs the logic contained within the method when the ConnectionCreateEvent is published.

@MyEventListener
public void onCreateEvent(ConnectionCreateEvent event) {
	System.out.println("create event received : '" + event.getMessage() + "'");
}

 

Definition of MyEventListener is given below.

@Documented
@Retention(RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Adapter(ApplicationEventListener.class)
@Indexed(ApplicationEventListener.class)
@Inherited
public @interface MyEventListener {

}

 

The presence of the @MyEventListener annotation causes the Micronaut to create a new class that implements ApplicationEventListener and invokes the onCreateEvent method defined in the bean above.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-method-adapter-advice’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>micronaut-method-adapter-advice</artifactId>
	<version>0.1</version>
	<packaging>jar</packaging>

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

	<properties>
		<packaging>jar</packaging>
		<jdk.version>11</jdk.version>
		<release.version>11</release.version>
		<micronaut.version>3.7.3</micronaut.version>
		<micronaut.runtime>netty</micronaut.runtime>
		<exec.mainClass>com.sample.app.App</exec.mainClass>
	</properties>

	<repositories>
		<repository>
			<id>central</id>
			<url>https://repo.maven.apache.org/maven2</url>
		</repository>
	</repositories>

	<dependencies>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-client</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-http-server-netty</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-jackson-databind</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>jakarta.annotation</groupId>
			<artifactId>jakarta.annotation-api</artifactId>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>io.micronaut.test</groupId>
			<artifactId>micronaut-test-junit5</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>io.micronaut.build</groupId>
				<artifactId>micronaut-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<!-- Uncomment to enable incremental compilation -->
					<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->

					<annotationProcessorPaths
						combine.children="append">
						<path>
							<groupId>io.micronaut</groupId>
							<artifactId>micronaut-http-validation</artifactId>
							<version>${micronaut.version}</version>
						</path>
					</annotationProcessorPaths>

				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

 

Step 3: Define MyEventListener.

 

MyEventListener.java

 

package com.sample.app.event.annotation;

import io.micronaut.aop.Adapter;
import io.micronaut.context.event.ApplicationEventListener;
import io.micronaut.core.annotation.Indexed;

import java.lang.annotation.*;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Retention(RUNTIME)
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Adapter(ApplicationEventListener.class)
@Indexed(ApplicationEventListener.class)
@Inherited
public @interface MyEventListener {
	
}

 

Step 4: Define events.

 

ConnectionCreateEvent.java

package com.sample.app.events;

public class ConnectionCreateEvent {
	private String message;

	public ConnectionCreateEvent(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}

 

ConnectionCloseEvent.java

package com.sample.app.events;

public class ConnectionCloseEvent {
	private String message;

	public ConnectionCloseEvent(String message) {
		this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}

 

Step 5: Define EventListeners class.

 

EventListeners.java

 

package com.sample.app.listeners;

import com.sample.app.event.annotation.MyEventListener;
import com.sample.app.events.ConnectionCloseEvent;
import com.sample.app.events.ConnectionCreateEvent;

import jakarta.inject.Singleton;

@Singleton
public class EventListeners {

	@MyEventListener
	public void onCreateEvent(ConnectionCreateEvent event) {
		System.out.println("create event received : '" + event.getMessage() + "'");
	}

	@MyEventListener
	public void onCloseEvent(ConnectionCloseEvent event) {
		System.out.println("close event received : '" + event.getMessage() + "'");
	}

}

 

Step 6: Define ConnectionEventPublisher class.

 

ConnectionEventPublisher.java

package com.sample.app.event.publishers;

import com.sample.app.events.ConnectionCloseEvent;
import com.sample.app.events.ConnectionCreateEvent;

import io.micronaut.context.event.ApplicationEventPublisher;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class ConnectionEventPublisher {

	@Inject
	ApplicationEventPublisher<ConnectionCreateEvent> createEventPublisher;

	@Inject
	ApplicationEventPublisher<ConnectionCloseEvent> closeEventPublisher;

	public void publishCreateEvent(String message) {
		createEventPublisher.publishEvent(new ConnectionCreateEvent(message));
	}

	public void publishCloseEvent(String message) {
		closeEventPublisher.publishEvent(new ConnectionCloseEvent(message));
	}
}

Step 7: Define main application class.

 

App.java

package com.sample.app;

import com.sample.app.event.publishers.ConnectionEventPublisher;

import io.micronaut.context.ApplicationContext;

public class App {

	public static void main(String[] args) {
		try (ApplicationContext applicationContext = ApplicationContext.run()) {
			ConnectionEventPublisher connectionCreateEventPublisher = applicationContext
					.getBean(ConnectionEventPublisher.class);

			connectionCreateEventPublisher.publishCreateEvent("Connection1 created");
			connectionCreateEventPublisher.publishCloseEvent("Connection1 closed");
		}
	}
}

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-method-adapter-advice-0.1.jar’ in project target folder.

 

$ ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-method-adapter-advice-0.1.jar
original-micronaut-method-adapter-advice-0.1.jar
test-classes

 

Execute below command to run the application.

java -jar ./target/micronaut-method-adapter-advice-0.1.jar

 

You will see below messages in the console.

create event received : 'Connection1 created'
close event received : 'Connection1 closed

 

You can download this application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment