Observing Async Events
public void observer(@ObservesAsync MyEvent payload) {
System.out.println("Yay, I'm called async!");
}
Slide 15
Slide 15 text
How it works
@Observes @ObservesAsync
event.fire(…)
Sync call Not notified
event.fireAsync(…)
Not notified Async call
Slide 16
Slide 16 text
Asynchronous Events
• Exceptions and Synchronization with
CompletionStage
• Active scopes: Request, Application
• Custom scopes depend on the implementation
Slide 17
Slide 17 text
@Priority
Slide 18
Slide 18 text
Ordered Events
• Add @Priority to event observers
• Aids observer ordering
• Total global order determined when the event is fired
public void earlier(@Observes @Priority(2499) MyEvent event) {
// yay! I'm first
System.out.println("Notified before all other observers");
}
public void observer(@Observes MyEvent event) {
System.out.println("Default priority");
}
public void later(@Observes @Priority(2501) MyEvent event) {
System.out.println("Notified after all other observers");
}
Slide 21
Slide 21 text
public void earlier(@Observes @Priority(2499) MyEvent event) {
// yay! I'm first
System.out.println("Notified before all other observers");
}
public void observer(@Observes MyEvent event) {
System.out.println("Default priority");
}
public void later(@Observes @Priority(2501) MyEvent event) {
System.out.println("Notified after all other observers");
}
Slide 22
Slide 22 text
public void earlier(@Observes @Priority(2499) MyEvent event) {
// yay! I'm first
System.out.println("Notified before all other observers");
}
public void observer(@Observes MyEvent event) {
System.out.println("Default priority");
}
public void later(@Observes @Priority(2501) MyEvent event) {
System.out.println("Notified after all other observers");
}
Slide 23
Slide 23 text
public void earlier(@Observes @Priority(2499) MyEvent event) {
// yay! I'm first
System.out.println("Notified before all other observers");
}
public void observer(@Observes MyEvent event) {
System.out.println("Default priority");
}
public void later(@Observes @Priority(2501) MyEvent event) {
System.out.println("Notified after all other observers");
}
Slide 24
Slide 24 text
Java SE
Slide 25
Slide 25 text
Java SE
• Currently: Implementation-specific in Weld/
OpenWebBean
• Spec Goal: Bootstrap API
Meta-Data Builder API
• Standardized API
• CDI 1.0+: Very verbose to create
• AnnotatedTypes, Beans, BeanAttributes, InjectionPoints, and
ObserverMethods
• Builder and Configurator-style
Slide 33
Slide 33 text
public class MyExtension {
public void afterBeanDiscovery(
@Observes AfterBeanDiscovery event) {
event.addBean()
.beanClass(MyService.class)
.scope(RequestScoped.class)
.name("myservice");
}
}
Add a Bean
Slide 34
Slide 34 text
public class MyExtension {
public void afterBeanDiscovery(
@Observes AfterBeanDiscovery event) {
event.addBean()
.beanClass(MyService.class)
.scope(RequestScoped.class)
.name("myservice");
}
}
Add a Bean
Slide 35
Slide 35 text
public class MyExtension {
public void processAnnotatedType(@Observes
@WithAnnotations(UseCase.class) ProcessAnnotatedType pat) {
pat.configureAnnotatedType()
.methods()
.forEach(m -> m.add(CacheableLiteral.INSTANCE));
}
}
Add annotation to Methods
Slide 36
Slide 36 text
public class MyExtension {
public void processAnnotatedType(@Observes
@WithAnnotations(UseCase.class) ProcessAnnotatedType pat) {
pat.configureAnnotatedType()
.methods()
.forEach(m -> m.add(CacheableLiteral.INSTANCE));
}
}
Add annotation to Methods
Slide 37
Slide 37 text
AOP on produced Bean
• CDI 1.0+: Interceptor applied to producer method
• InterceptionFactory a possible answer
@Produces
@Transactional
public MyService produceService() {
...
}
Slide 38
Slide 38 text
AOP on produced Bean
@Produces
@RequestScoped
public MyService createTransactional(InterceptionFactory ipf) {
ipf.configure()
.filterMethods(m -> m.getJavaMember().getName().equals("save"))
.findFirst()
.ifPresent(m -> m.add(new AnnotationLiteral() {}));
return ipf.createInterceptedInstance(new MyService());
}
Slide 39
Slide 39 text
AOP on produced Bean
@Produces
@RequestScoped
public MyService createTransactional(InterceptionFactory ipf) {
ipf.configure()
.filterMethods(m -> m.getJavaMember().getName().equals("save"))
.findFirst()
.ifPresent(m -> m.add(new AnnotationLiteral() {}));
return ipf.createInterceptedInstance(new MyService());
}
Slide 40
Slide 40 text
Proxy matters
Slide 41
Slide 41 text
Proxy injection
@Inject
private MyService myService;
This is a proxy instance
Slide 42
Slide 42 text
Proxy injection
Slide 43
Slide 43 text
Proxy injection
Slide 44
Slide 44 text
Proxy injection
@Inject
@Named("myBean")
private ConcurrentHashMap myService;
/** Implementation for put and putIfAbsent */
final V putVal(K key, V value, boolean onlyIfAbsent)
Slide 45
Slide 45 text
Normal-scoped beans
• Must be proxyable
• accessible, no-args constructor
• non-final classes
• accessible, non-final instance methods