mirror of
https://github.com/tiennm99/jvm-labs.git
synced 2026-10-11 03:13:36 +00:00
[Event] (Add) try handle generic event
This commit is contained in:
1 parent
daa5e53e8a
commit
6d2828c172
10 files changed
+139
No files matched your search
@@ -0,0 +1,3 @@
|
||||
package com.miti99.test.event;
|
||||
|
||||
public record GenericEvent<T>(T data) {}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.miti99.test.event;
|
||||
|
||||
public record SimpleEvent() {}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.miti99.test.listener;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.A;
|
||||
import io.micronaut.runtime.event.annotation.EventListener;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class AListener {
|
||||
|
||||
@EventListener
|
||||
public void onApplicationEvent(GenericEvent<A> event) {
|
||||
System.out.println("AListener.onApplicationEvent: " + event.data().getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.miti99.test.listener;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.B;
|
||||
import io.micronaut.runtime.event.annotation.EventListener;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class BListener {
|
||||
|
||||
@EventListener
|
||||
public void onApplicationEvent(GenericEvent<B> event) {
|
||||
System.out.println("BListener.onApplicationEvent: " + event.data().getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.miti99.test.listener;
|
||||
|
||||
import com.miti99.test.event.SimpleEvent;
|
||||
import io.micronaut.runtime.event.annotation.EventListener;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class SimpleListener {
|
||||
|
||||
@EventListener
|
||||
public void onApplicationEvent(SimpleEvent event) {
|
||||
System.out.println("SimpleListener.onApplicationEvent");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.miti99.test.publisher;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.A;
|
||||
import io.micronaut.context.event.ApplicationEventPublisher;
|
||||
import io.micronaut.http.MediaType;
|
||||
import io.micronaut.http.annotation.Controller;
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.micronaut.http.annotation.Produces;
|
||||
|
||||
@Controller("/A")
|
||||
public class APublisher {
|
||||
|
||||
private final ApplicationEventPublisher<GenericEvent<A>> eventPublisher;
|
||||
|
||||
public APublisher(ApplicationEventPublisher<GenericEvent<A>> eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Get
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String hello() {
|
||||
GenericEvent<A> event = new GenericEvent<>(new A());
|
||||
eventPublisher.publishEvent(event);
|
||||
return "fired A event";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.miti99.test.publisher;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.A;
|
||||
import com.miti99.test.type.B;
|
||||
import io.micronaut.context.event.ApplicationEventPublisher;
|
||||
import io.micronaut.http.MediaType;
|
||||
import io.micronaut.http.annotation.Controller;
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.micronaut.http.annotation.Produces;
|
||||
|
||||
@Controller("/B")
|
||||
public class BPublisher {
|
||||
|
||||
private final ApplicationEventPublisher<GenericEvent<B>> eventPublisher;
|
||||
|
||||
public BPublisher(ApplicationEventPublisher<GenericEvent<B>> eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Get
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String hello() {
|
||||
GenericEvent<B> event = new GenericEvent<>(new B());
|
||||
eventPublisher.publishEvent(event);
|
||||
return "fired B event";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.miti99.test.publisher;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.event.SimpleEvent;
|
||||
import com.miti99.test.type.B;
|
||||
import io.micronaut.context.event.ApplicationEventPublisher;
|
||||
import io.micronaut.http.MediaType;
|
||||
import io.micronaut.http.annotation.Controller;
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.micronaut.http.annotation.Produces;
|
||||
|
||||
@Controller("/Simple")
|
||||
public class SimplePublisher {
|
||||
|
||||
private final ApplicationEventPublisher<SimpleEvent> eventPublisher;
|
||||
|
||||
public SimplePublisher(ApplicationEventPublisher<SimpleEvent> eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Get
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String hello() {
|
||||
SimpleEvent event = new SimpleEvent();
|
||||
eventPublisher.publishEvent(event);
|
||||
return "fired simple event";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.miti99.test.type;
|
||||
|
||||
public class A {}
|
||||
@@ -0,0 +1,3 @@
|
||||
package com.miti99.test.type;
|
||||
|
||||
public class B {}
|
||||
Reference in new issue
Block a user