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
b6657b32ab
commit
daa5e53e8a
10 files changed
+125
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,14 @@
|
||||
package com.miti99.test.listener;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.A;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AListener {
|
||||
|
||||
public void onGenericEvent(@Observes GenericEvent<A> event) {
|
||||
System.out.println("AListener.onGenericEvent: " + event.data().getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.miti99.test.listener;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.A;
|
||||
import com.miti99.test.type.B;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
|
||||
@ApplicationScoped
|
||||
public class BListener {
|
||||
|
||||
public void onGenericEvent(@Observes GenericEvent<B> event) {
|
||||
System.out.println("BListener.onGenericEvent: " + event.data().getClass());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.miti99.test.listener;
|
||||
|
||||
import com.miti99.test.event.SimpleEvent;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SimpleListener {
|
||||
|
||||
public void onEvent(@Observes SimpleEvent event) {
|
||||
System.out.println("SimpleListener.onEvent");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.miti99.test.publisher;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.A;
|
||||
import jakarta.enterprise.event.Event;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
@Path("/A")
|
||||
public class APublisher {
|
||||
|
||||
@Inject Event<GenericEvent<A>> eventPublisher;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String hello() {
|
||||
GenericEvent<A> event = new GenericEvent<>(new A());
|
||||
eventPublisher.fire(event);
|
||||
return "fired A event";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.miti99.test.publisher;
|
||||
|
||||
import com.miti99.test.event.GenericEvent;
|
||||
import com.miti99.test.type.B;
|
||||
import jakarta.enterprise.event.Event;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
@Path("/B")
|
||||
public class BPublisher {
|
||||
|
||||
@Inject Event<GenericEvent<B>> eventPublisher;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String hello() {
|
||||
GenericEvent<B> event = new GenericEvent<>(new B());
|
||||
eventPublisher.fire(event);
|
||||
return "fired B event";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.miti99.test.publisher;
|
||||
|
||||
import com.miti99.test.event.SimpleEvent;
|
||||
import jakarta.enterprise.event.Event;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
|
||||
@Path("/Simple")
|
||||
public class SimplePublisher {
|
||||
|
||||
@Inject Event<SimpleEvent> eventPublisher;
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String hello() {
|
||||
SimpleEvent event = new SimpleEvent();
|
||||
eventPublisher.fire(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