Skip to content

Commit f03412a

Browse files
committed
Add observer priority example
1 parent 36e46f8 commit f03412a

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventReciever4.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.readlearncode.async.AuditEvent;
44

5+
import javax.annotation.Priority;
56
import javax.enterprise.context.ApplicationScoped;
67
import javax.enterprise.event.ObservesAsync;
78

@@ -15,7 +16,7 @@
1516
public class AuditEventReciever4 {
1617

1718
// Order: Doesn't fire
18-
public void receive(@ObservesAsync AuditEvent auditEvent) {
19+
public void receive(@ObservesAsync @Priority(1) AuditEvent auditEvent) {
1920
System.out.println("Priority: no (ObservesAsync) " +
2021
auditEvent.getPriority() + " " + auditEvent.getMessage());
2122
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.readlearncode.priority;
2+
3+
/**
4+
* Source code github.com/readlearncode
5+
*
6+
* @author Alex Theedom www.readlearncode.com
7+
* @version 1.0
8+
*/
9+
public class ObserverEvent {
10+
11+
/*
12+
The CDI 2.0 API introduces the @Priority annotation that allows
13+
the order of observer methods.
14+
15+
When the fire method is called in what order are the observer
16+
methods executed.
17+
*/
18+
19+
20+
// // Order: 2nd
21+
// public void observe1(@Observes @Priority(1000) AuditEvent auditEvent) {}
22+
//
23+
// // Order: 1st
24+
// public void observe2(@Observes @Priority(100) AuditEvent auditEvent) {}
25+
//
26+
// // Order: 3rd
27+
// public void observe3(@Observes @Priority(APPLICATION + 500) AuditEvent auditEvent) {}
28+
//
29+
// // Order: Doesn't fire
30+
// public void observe4(@ObservesAsync @Priority(1) AuditEvent auditEvent) {}
31+
//
32+
// // Order: 4th
33+
// public void observe5(@Observes @Priority(5000) AuditEvent auditEvent) {}
34+
}

0 commit comments

Comments
 (0)