The document discusses plans for Contexts and Dependency Injection (CDI) 2.0. Main topics for CDI 2.0 include improving the event system by adding support for asynchronous events and event ordering. It also aims to define bootstrapping for CDI in Java SE applications and improve aspects of CDI like AOP. The specification work incorporates community feedback and aims to support new Java 8 features.
8. CDI2.0 • Work on CDI 2.0 is still in progress
• No final decision yet
• Everything might change
@struberg @thjanssen123CDI 2.0 Deep Dive
9. MainTopics • Improve the event system
• Bootstrapping for Java SE
• Modularity
• Improve AOP
• Enhance SPI and Contexts
• Support Java 8 features
@struberg @thjanssen123CDI 2.0 Deep Dive
10. MainTopics • Defined by
• Existing entries in Jira
• Requirements by other specs
• Input from former expert group members
• Community survey
@struberg @thjanssen123CDI 2.0 Deep Dive
11. CommunitySurvey • June 2014
• 260 participants
• 20 features to rate
• Asynchronous events
• Bootstrapping outside of Java EE
• AOP for produced or custom beans
• Observer ordering
@struberg @thjanssen123CDI 2.0 Deep Dive
13. Events • One of the bigger topics in CDI 2.0
• High demand by community
• Features
• Asynchronous events
• Ordering of events
@struberg @thjanssen123CDI 2.0 Deep Dive
14. SynchronousEvents • Fire a synchronous event
@Inject
Event<UserEvent> userEvent;
…
userEvent.fire (new UserEvent(…));
• Observe a synchronous event
public void handleUserEvent(@Observes UserEvent e)
{…}
@struberg @thjanssen123CDI 2.0 Deep Dive
15. AsynchronousEvents • Fire an asynchronous event
@Inject
Event<UserEvent> userEvent;
…
userEvent.fireAsync(new UserEvent(…));
• Observe an asynchronous event
public void handleUserEvent(@ObserveAsync UserEvent e)
{…}
@struberg @thjanssen123CDI 2.0 Deep Dive
16. AsynchronousEvents • Result and exception handling
event.fireAsync(new UserEvent(…))
.whenComplete((event, throwable) -> {
if (throwable != null) {
logger.error(“Error during processing of
UserEvent” +
throwable.getMessage());
} else {
logger.info(“Processing successful”);
}
});
@struberg @thjanssen123CDI 2.0 Deep Dive
17. AsynchronousEvents • What could possibly go wrong?
• Thread executor group might get blocked by slow observers
• Mutable non-threadsafe payload
e.g. visitor pattern with ArrayList in event payload
• @SessionScoped doesn‘t get propagated between threads
• @RequestScoped doesn‘t get propagated between threads
e.g. @Inject Principal currentUser
@struberg @thjanssen123CDI 2.0 Deep Dive
18. AsynchronousEvents • What could possibly go wrong?
• ThreadLocals of all kind don‘t get propagated
e.g. log4j MappedDiagnosticContext
• Transactions don‘t get propagated
TransactionSynchronisationRegistry is basically a ThreadLocal
• New EntityManager and @PersistenceContext for each thread
• @Observes(during=TransactionPhase.AFTER_SUCCESS)
• Each async observer gets ist own transaction!
@struberg @thjanssen123CDI 2.0 Deep Dive
19. AsynchronousEvents • All these problems are caused by multi-threading behavior
in Java EE and not CDI specific
• Very same problems occure with EJB @Asynchronous
and Concurrency-Utils for Java EE
@struberg @thjanssen123CDI 2.0 Deep Dive
20. AsynchronousEvents • Introduces new, asynchronous kind of events
• CompletionStage<U> fireAsync(U event)
• @ObservesAsync
Event method @Observes notified @ObservesAsync
notified
fire() Yes No
fireAsync() No Yes
@struberg @thjanssen123CDI 2.0 Deep Dive
21. OrderingofEvents • Define the order of event observers
public void handleUserEvent(
@Observe @Priority(1000) UserEvent e) {…}
• Call smallest priority first
• Uses default priority if not defined
• Order undefined for Observer with same priority
@struberg @thjanssen123CDI 2.0 Deep Dive
23. Bootstrapping • Define a standard way to boot the CDI container in Java SE
• Already part of Weld and Open Web Beans
• First API proposed in EDR 1
@struberg @thjanssen123CDI 2.0 Deep Dive
24. Bootstrapping • API proposal in EDR1
public static void main(String... args) {
try(CDI<Object> cdi =
CDI.getCDIProvider().initialize()) {
// start the container,
// retrieve a bean and do work with it
MyBean myBean = cdi.select(MyBean.class).get();
myBean.doWork();
}
// shuts down automatically after
// the try with resources block.
}
https://docs.jboss.org/cdi/spec/2.0.EDR1/cdi-spec.html#bootstrap-
se
@struberg @thjanssen123CDI 2.0 Deep Dive
25. Bootstrapping • API currently under discussion
• Context control not defined yet
• Bean discovery mode still under discussion
• Provide an option to choose implementation
@struberg @thjanssen123CDI 2.0 Deep Dive
26. CdiCtrlCdiContainer • Allows to boot embedded EE containers with a vendor
independent API
• Implementations for:
• Apache OpenWebBeans
• JBoss Weld
• JBoss WildFly in the making
• Apache OpenEJB (TomEE embedded)
• add your own
• Simply replace the impl jar to switch!
@struberg @thjanssen123CDI 2.0 Deep Dive
27. CdiCtrl-ContainerBootstrap • Very usable for unit tests, batches or other standalone Java
processes:
CdiContainer cdiContainer =
CdiContainerLoader.getCdiContainer();
cdiContainer.boot();
cdiContainer.getContextControl().startContexts();
…
cdiContainer.shutdown();
@struberg @thjanssen123CDI 2.0 Deep Dive
28. CdiCtrl-ContextControl • Also usable in EE containers
• Usage:
@Inject ContextControl ctxCtrl;
• Allows to attach dummy RequestContext, SessionContext
etc to the current Thread.
• Usable for Quartz extensions or any other async work
@struberg @thjanssen123CDI 2.0 Deep Dive
30. AOP • Work on AOP improvements just started
• Topics
• Improve handling of UnproxyableResolutionException
• Interceptors and Decorators on produced and custom
beans
• Support AOP on inner calls
@struberg @thjanssen123CDI 2.0 Deep Dive
31. ClassProxies • No Java SE support so far!
• All done in a container depending own way
• Most times uses bytecode libraries like javassist, ASM, cglib,
bcel or serp
• Create a sub-class of the given type
• Override all public methods
@struberg @thjanssen123CDI 2.0 Deep Dive
32. UnproxyAble? • CDI throws an UnproxyAbleResolutionException for all
classes which have
• No default constructor
• final, non-static and non-private methods
e.g. ConcurrentHashMap
www.thoughts-on-java.org
33. SubclassProxyExample1/2 • The business class
@SessionScoped
public class User {
private String name;
public String getName() {
return name;
}
}
@struberg @thjanssen123CDI 2.0 Deep Dive
34. SubclassProxyExample2/2 public class User$$Proxy extends User {
@Override
public String getName() {
return getInstance().getName();
}
private T getInstance() {
beanManager.getContext().get(...);
}
}
@struberg @thjanssen123CDI 2.0 Deep Dive
38. InterceptorsonProducers • Long discussions…
• Probably solvable by introducing javax.proxy.ProxyFactory?
• Like java.lang.reflect.Proxy but with subclassing
see DeltaSpike PartialBean
@struberg @thjanssen123CDI 2.0 Deep Dive
40. Java8 • Java 8 was released after Java EE 7
• Support for Java 8 features
• All new APIs will use Java 8
• Changes of existing APIs are still under discussion
@struberg @thjanssen123CDI 2.0 Deep Dive
41. Java8 • Asynchronous events
fireAsync methods return new CompletionStage
public <U extends T> CompletionStage<U> fireAsync(U event);
public <U extends T> CompletionStage<U> fireAsync(U event,
Executor executor);
@struberg @thjanssen123CDI 2.0 Deep Dive
42. Java8 • Repeatable qualifiers and interceptor bindings
• Proposed by RI Weld, not specified so far
@Produces
@Language(„english“)
@Language(„german“)
public Translator createTranslator() { … }
@struberg @thjanssen123CDI 2.0 Deep Dive