Monday, 6 February 2023

Micronaut: Asynchronously listening for Events with @EventListener

By applying @Async annotation on a listener, we can run the operation on a separate thread:

 

Step 1: Annotate an event listener with @Async annotation.

@EventListener
@Async
public void onCreateEvent(ConnectionCreateEvent event) {
	sleepRandTime();
	System.out.println(Thread.currentThread().getName() + " received event : '" + event.getMessage() + "'");
}

 

Step 2: Since The event listener by default runs on the scheduled executor. You can configure this thread pool as required in application.yml file.

micronaut:
  executors:
    scheduled:
      type: scheduled
      core-pool-size: 30

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-async-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-async-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 class.

 

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;
	}
}

Step 4: Define event listeners.

 

EventListeners.java

package com.sample.app.event.listeners;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.sample.app.event.ConnectionCreateEvent;

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

@Singleton
public class EventListeners {

	private static void sleepRandTime() {
		Random random = new Random();
		int n = random.nextInt(10);
		try {
			TimeUnit.MICROSECONDS.sleep(n * 100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@EventListener
	@Async
	public void onCreateEvent(ConnectionCreateEvent event) {
		sleepRandTime();
		System.out.println(Thread.currentThread().getName() + " received event : '" + event.getMessage() + "'");
	}

}

Step 5: Define event publisher.

 

ConnectionEventPublisher.java

package com.sample.app.event.publishers;

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;

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

}

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);

			int counter = 0;

			for (int i = 0; i < 10; i++) {
				connectionCreateEventPublisher.publishCreateEvent("Connection " + ++counter);
			}

		}

	}
}

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

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

Execute below command to run the application.

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

$ java -jar ./target/micronaut-async-event-listener-demo-1-jar-with-dependencies.jar
scheduled-executor-thread-6 received event : 'Connection 6'
scheduled-executor-thread-2 received event : 'Connection 2'
scheduled-executor-thread-5 received event : 'Connection 5'
scheduled-executor-thread-9 received event : 'Connection 9'
scheduled-executor-thread-4 received event : 'Connection 4'
scheduled-executor-thread-1 received event : 'Connection 1'
scheduled-executor-thread-7 received event : 'Connection 7'
scheduled-executor-thread-3 received event : 'Connection 3'
scheduled-executor-thread-10 received event : 'Connection 10'
scheduled-executor-thread-8 received event : 'Connection 8'

You can download the application from this link.



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment