Date post: | 10-Feb-2017 |
Category: |
Software |
Upload: | thorben-janssen |
View: | 2,594 times |
Download: | 2 times |
CDI 2.0 Deep Dive
Mark
Stru
berg • Member Apache Software Foundation
• VP, Apache OpenWebBeans
• CDI expert group member
• Twitter: @struberg• Blog: struberg.wordpress.com
@struberg @thjanssen123CDI 2.0 Deep Dive
Thor
ben J
anss
en • Independent author and trainer• Senior developer and architect @ Qualitype GmbH
• CDI 2.0 expert group member
• Twitter: @thjanssen123• Blog: www.thoughts-on-java.org
@struberg @thjanssen123CDI 2.0 Deep Dive
CDI? What‘s that?
@struberg @thjanssen123CDI 2.0 Deep Dive
Wha
t isC
DI?
• Contexts and Dependency Injection for JavaTM 2.0 (JSR 365)
• Provides
• Contexts
• Dependency Injection
• Events
• Interceptors and decorators
• Extensions
@struberg @thjanssen123CDI 2.0 Deep Dive
Hist
oryo
fCDI
• CDI 1.0 (Java EE 6) December 2009
• CDI 1.1 (Java EE 7) June 2013
• CDI 1.2 April 2014
• CDI 2.0 Start September 2014
• CDI 2.0 Early Draft Release 2015
• CDI 2.0 Release 1st half of 2016
• CDI 2.1 Start after 2.0 Release
@struberg @thjanssen123CDI 2.0 Deep Dive
CDI 2.0
@struberg @thjanssen123CDI 2.0 Deep Dive
CDI 2
.0• Work on CDI 2.0 is still in progress
• No final decision yet
• Everything might change
@struberg @thjanssen123CDI 2.0 Deep Dive
Main
Topi
cs• 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
Main
Topi
cs• Defined by
• Existing entries in Jira
• Requirements by other specs
• Input from former expert group members
• Community survey
@struberg @thjanssen123CDI 2.0 Deep Dive
Comm
unity
Sur
vey • 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
Events
@struberg @thjanssen123CDI 2.0 Deep Dive
Even
ts• 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
Sync
hron
ousE
vent
s • 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
Asyn
chro
nous
Even
ts • 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
Asyn
chro
nous
Even
ts • 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
Asyn
chro
nous
Even
ts • What could possibly go wrong?
• Thread executor group might get blocked by slow observers
• Mutable non-threadsafe payloade.g. visitor pattern with ArrayList in event payload
• @SessionScoped doesn‘t get propagated between threads
• @RequestScoped doesn‘t get propagated between threadse.g. @Inject Principal currentUser
@struberg @thjanssen123CDI 2.0 Deep Dive
Asyn
chro
nous
Even
ts • What could possibly go wrong?
• ThreadLocals of all kind don‘t get propagatede.g. log4j MappedDiagnosticContext
• Transactions don‘t get propagatedTransactionSynchronisationRegistry 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
Asyn
chro
nous
Even
ts • All these problems are caused by multi-threading behaviorin Java EE and not CDI specific
• Very same problems occure with EJB @Asynchronousand Concurrency-Utils for Java EE
@struberg @thjanssen123CDI 2.0 Deep Dive
Asyn
chro
nous
Even
ts • Introduces new, asynchronous kind of events
• CompletionStage<U> fireAsync(U event)
• @ObservesAsync
Event method @Observes notified @ObservesAsyncnotified
fire() Yes No
fireAsync() No Yes
@struberg @thjanssen123CDI 2.0 Deep Dive
Orde
ringo
fEve
nts • 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
Bootstrapping
@struberg @thjanssen123CDI 2.0 Deep Dive
Boot
stra
pping
• 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
Boot
stra
pping
• 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
Boot
stra
pping
• 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
CdiC
trlCd
iCon
taine
r • Allows to boot embedded EE containers with a vendorindependent 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
CdiC
trl-C
onta
iner B
oots
trap • 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
CdiC
trl-C
onte
xtCo
ntro
l• Also usable in EE containers
• Usage:@Inject ContextControl ctxCtrl;
• Allows to attach dummy RequestContext, SessionContextetc to the current Thread.
• Usable for Quartz extensions or any other async work
@struberg @thjanssen123CDI 2.0 Deep Dive
AOP
@struberg @thjanssen123CDI 2.0 Deep Dive
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
Clas
s Pro
xies
• 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
Unpr
oxyA
ble?
• CDI throws an UnproxyAbleResolutionException for all classes which have
• No default constructor
• final, non-static and non-private methodse.g. ConcurrentHashMap
www.thoughts-on-java.org
Subc
lassP
roxy
Exam
ple1
/2 • The business class
@SessionScoped
public class User {
private String name;
public String getName() {
return name;
}
}
@struberg @thjanssen123CDI 2.0 Deep Dive
Subc
lassP
roxy
Exam
ple2
/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
Allow
Prox
ying
• Proposed solution
• Annotation @AllowProxying
• beans.xml <allowProxying>
@struberg @thjanssen123CDI 2.0 Deep Dive
Allow
Prox
ying
• Proposed solution 1
• Annotation @AllowProxying
Public class Owner {
@Produces
@RequestScoped
@TenantA
@AllowProxying
ConcurrentHashMap createConfigBase() {…}
}
@struberg @thjanssen123CDI 2.0 Deep Dive
Allow
Prox
ying
• Proposed solution
• beans.xml <allowProxying>
<allowProxying>
<class>java.util.concurrent.ConcurrentHashMap</class>
</allowProxying>
@struberg @thjanssen123CDI 2.0 Deep Dive
Inte
rcep
tors
on Pr
oduc
ers • Long discussions…
• Probably solvable by introducing javax.proxy.ProxyFactory?
• Like java.lang.reflect.Proxy but with subclassingsee DeltaSpike PartialBean
@struberg @thjanssen123CDI 2.0 Deep Dive
Java 8
@struberg @thjanssen123CDI 2.0 Deep Dive
Java
8• 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
Java
8• 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
Java
8• 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
Get in touch
@struberg @thjanssen123CDI 2.0 Deep Dive
Geti
n tou
ch• Website: http://www.cdi-spec.org
• Blog: http://www.cdi-spec.org/news/
• Twitter: @cdispec
• Mailing list: https://lists.jboss.org/mailman/listinfo/cdi-dev
• JIRA: https://issues.jboss.org/
@struberg @thjanssen123CDI 2.0 Deep Dive
Geti
n tou
chMark StrubergTwitter: @strubergBlog: struberg.wordpress.com
Thorben JanssenTwitter: @thjanssen123Blog: www.thoughts-on-java.org
@struberg @thjanssen123CDI 2.0 Deep Dive
Reso
urce
s• CDI website:
http://www.cdi-spec.org• JSR 365 – CDI 2.0:
https://jcp.org/en/jsr/detail?id=365• CDI 2.0 EDR1:
https://docs.jboss.org/cdi/spec/2.0.EDR1/cdi-spec.html• The path to CDI 2.0 by Antoine Sabot-Durand:
https://www.youtube.com/watch?v=RynnZPsQdxMhttp://de.slideshare.net/antoinesd/the-path-to-cdi-20
• Blog Weld – CDI reference implementation: http://weld.cdi-spec.org/news/
• Apache OpenWebBeans:http://openwebbeans.apache.org
@struberg @thjanssen123CDI 2.0 Deep Dive