Monday 6 February 2023

Micronaut: define event listener using @EventListener annotation

@EventListener annotation can be applied on a method, which in turn acts as event listener.

 

Example

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

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-event-listener-demo’.

 

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-event-listener-demo</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.2</micronaut.version>
		<slf4j.version>2.0.3</slf4j.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>
				<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: Define event classes.

 

ConnectionCreateEvent.java

package com.sample.app.event;

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.event;

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 4: Define event publisher class.

 

ConnectionEventPublisher.java

package com.sample.app.event.publishers;

import com.sample.app.event.ConnectionCloseEvent;
import com.sample.app.event.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 5: Define event listeners.

 

EventListeners.java

package com.sample.app.event.listeners;

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

import io.micronaut.runtime.event.annotation.EventListener;
import jakarta.inject.Singleton;

@Singleton
public class EventListeners {

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

}

 

Step 6: 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-event-listener-demo-1-jar-with-dependencies.jar’ in project target folder.

 

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

Execute below command to run the application.

java -jar ./target/micronaut-event-listener-demo-1-jar-with-dependencies.jar

$ java -jar ./target/micronaut-event-listener-demo-1-jar-with-dependencies.jar
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