Thursday Jul 17, 2014

Contexts and Dependency Injection for Java 2.0 (CDI 2.0)

CDI (Context and Dependency Injection) is a 'recent' specification. CDI 1.0 has been introduced in Java EE 6 and has been slightly revised (CDI 1.1) for Java EE 7. In just a few years, CDI has become a key technology for the Java EE platform. CDI is becoming increasingly important as different parts of the platform are leveraging CDI more and more (e.g. JTA 1.2 @Transactional, Beans Validation 1.1 method level validations, etc.). And it is expected that CDI adoption within Java EE will only continue to grow as, for example, some upcoming Java EE 8 specifications are considering leveraging CDI too (e.g. JMS 2.1). 

Red Hat, the CDI Specification Lead, has just submitted to the JCP a JSR proposal for CDI 2.0. CDI 2.0 would be a major evolution of the current CDI specification, with 2 main focus : Java SE support and modularity.
  • Java SE support : Define the behaviour of CDI in a pure Java SE environment. 
  • Modularity : Define a granular, modular CDI model to help other Java EE specifications to better integrate with it. This would also probably be useful when CDI will be used in a pure Java SE context.
CDI 2 will be targeted for inclusion in Java EE 8. The Java SE features of CDI 2.0 would work on Java SE 8. In terms ot timing, CDI 2.0 is aligned with the current Java EE 8 roadmap. The CDI 2.0 JSR proposal will soon be posted on the JCP site for a 2 weeks JSR review period, the JSR approval ballot should then follow. 

In the meantime, you can read the CDI 2.0 JSR proposal here and for some technical background on what CDI 2.0 might be, you should check Antoine Sabot Durand (CDI 2.0 Specification Lead) 'Forward CDI 2.0' piece.

Monday Jul 14, 2014

VRaptor 4: A CDI Based MVC Framework for Java EE

As you may know one of the surprising results of the Java EE 8 survey was the desire for an action oriented MVC framework for Java EE. What many folks may not be aware is that there are actually a few action oriented web framework solutions out there that are already very aligned with Java EE. One such solution that really stands out is VRaptor.

Built on top of CDI, it has some very interesting defaults geared towards ease-of-use/productivity. It also supports key Java EE APIs such as JPA and JTA. To build a bit more awareness for the web framework, key commiter Rodrigo Turini recently wrote an excellent article on InfoQ. It is a very well-written article that serves as a good getting started guide with the innovative framework. Rodrigo will also be talking about VRaptor 4 at JavaOne 2014.

Jersey MVC has also long provided a very similar action oriented solution based on top of JAX-RS. All of these solutions could certainly be used now and serve as valuable input to any future effort to standardize an action oriented web framework in Java EE.

Tuesday Apr 15, 2014

Asynchronous CDI Events?

CDI events are a very powerful feature that enable loose coupling in a very simple and type-safe way. One aspect of CDI events that surprises many folks is the fact that they are synchronous rather than asynchronous. The surprise is perhaps fairly natural as similar event systems are usually asynchronous. Piotr Nowicki explores the issue in a very well thought out post exploring the various options for making CDI events asynchronous using plain Java EE features such as EJB @Asynchronous, Singleton locks and JMS. Exploring this issue further is probably very timely with the gradual rise of node.js/Reactive Programming.

Very encouragingly CDI 2 specification lead Antoine Sabot-Durand himself mentioned asynchronous CDI events as a possibility in his personal CDI 2 wish list. He suggests adding an asynchronous flag to the @Observes annotation. A possible downside to this is that the consumer rather than the producer makes the decision to go asynchronous. Another possibility is putting the asynchronous marker on the producer possibly using the type annotations facility introduced in Java SE 8:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public class EventProducer {
@Inject Event event;
public void doSomething() {
MyEvent e = new @Async MyEvent(); = "This is a test event";
e.eventTime = new Date();;
public class EventConsumer {
public void afterMyEvent(@Observes MyEvent event) {
// .. Insert event logic here
view raw hosted with ❤ by GitHub

What do you think about the issue and the possibilities? This is the time to sound off as the horizons for CDI 2 are emerging.

Tuesday Apr 08, 2014

New book: EJB 3 in Action, Second Edition

'EJB3 in Action' is one of the reference books when it comes to this important area of Java EE. The second edition covering EJB 3.2 has just been published.

Over the years, the EJB specification has come a long way and has been greatly simplified but nevertheless EJB is still a large specification to grasp. This book is a great (and up-to-date!) introduction to the EJB Component Model. The book goes beyond just introducing EJBs in details as it also explains technologies related to EJB such as CDI and JPA (the older amongst us will remember that EJB, back in the days, used to also handle persistence). The book has also a full chapter devoted to WebSocket, a new important Java EE 7 technology. The final chapter of the book is devoted to a critical, some time over looked, topics: testing.

This is not a pure theoretical book as it really gives 2 views, it first introduce and explains EJBs but also JPA, CDI, etc. The book then explain how to use EJBs and the other technologies, how to put them in action! All in all, a great book to add to any Java EE 7 bookshelf!

Disclaimer: 'EJB 3 in Action' was co-authored by our own Reza Rahman. But to be honest, I was already praising the first edition of the book when Reza wasn't yet working for Oracle.

Friday Mar 21, 2014

Code PaLOUsa 2014 Trip Report

Code PaLOUsa 2014 took place 24-26 February in Louisville, Kentucky. Code PaLOUsa is a fairly unassuming conference with great quality farther away from the beaten paths. Topics covered included Java, .NET, JavaScript, mobile, methodology and Big Data/NoSQL. On my way to Kentucky, I was able to stop by at the Montgomery County (Maryland) JUG for a Java EE 7 session.

At Code PaLOUsa I delivered a talk on aligning Java EE with NoSQL as well as a talk on the Cargo Tracker Java EE/Domain-Driven Design Blue Prints project (this talk was recorded by InfoQ). More details, including slide decks and code, posted on my personal blog.

Sunday Feb 16, 2014

Inject Properties using CDI

One of the principal goals of CDI is to make the Java EE programming model far more extensible - from adding basic functionality to full-scale integration with third-party software. Piotr Nowicki demonstrates this well by doing something pretty simple but common and useful - injecting a value from a Java .properties file into any managed bean using CDI @Produces and InjectionPoint. Note that the Core module of the Apache DeltaSpike project also includes similar functionality. In case you are unaware of Apache DeltaSpike, it is the de-facto collection of CDI portable extensions (it is the successor to both Seam 3 and Apache CODI).

Both Piotr's code and Apache DeltaSpike inject values from property files only. One useful addition could be to also support Java environment variables. Perhaps this inspires you enough to create your own CDI portable extension for injecting properties or contribute to DeltaSpike :-)? Interestingly, there is a similar discussion on JAVAEE_SPEC-19. Perhaps this is an area for the emergent Java EE Configuration JSR to look into?

Wednesday Feb 05, 2014

InvokeDynamic and Java EE?

“Man cannot discover new oceans unless he has the courage to lose sight of the shore.”
(André Gide)

JSR 292 (Supporting Dynamically Typed Languages on the Java Platform), introduced in Java SE 7, is about simplifying and improving the support of dynamically typed languages on the Java Platform. JSR 292 defines a new bytecode, i.e. InvokeDynamic, that allows the execution of method invocations in the absence of static type information. So it is clearly the first bytecode that has been designed for dynamically typed language and not for the Java language.

Project Nashorn, Java 8's new JavaScript engine relies heavily on InvokeDynamic. In Fact, originally Nashorn 'was just' an InvokeDynamic Proof of Concept. The PoC was successful and Nashorn has, since then, evolved into a high-performance 100% ECMAScript compliant engine.

Despite the fact that InvokeDynamic was originally conceived for dynamically typed languages, it turns out that this bytecode could also be useful for the Java language itself. For example, Java SE 8's Lambdas are using, under the hood, InvokeDynamic.

Antoine Sabot-Durand, CDI co-spec lead, is currently conducting some experimentations around using InvokeDynamic to replace Weld's proxies. At this stage, it's just an initial Proof of Concept but it works! If his efforts goes to fruition and since Weld is the CDI Reference Implementation and is used by different Application Servers (JBoss AS, WildFly, GlassFish, WebLogic...), who knows, we might get, and not just for Lambdas support, InvokeDynamic under the hood of some Java EE Application Servers. Only the future will tell us!

Monday Dec 16, 2013

Java EE@Pune JUG

Pune JUG organized a meetup at the Symbiosis Institute of Computer Studies and Research (SICRSR) college campus on December 14th. Led by Harshad Oak (Java Champion and organizer of the IndicThreads conference), Pune JUG is the oldest JUG in India and boasts a vibrant developer community. This month's meetup was focused on the Java EE 7 platform and the ecosystem that is building up around it. The meeting was led by Oracle India's own Vijay Nair. Vijay is a good friend and a lead developer with Oracle's FLEXCUBE development group in India. Among many other things like evangelizing Java EE at local Java user groups and conferences, Vijay contributes heavily to the Cargo Tracker Java EE Blue Prints project in his spare time.

 The event saw an attendance of close to around 100 people with a lot of demos/code and high quality interactions with the audience. Vijay presented no less than three sessions at the meetup:

  • JavaEE.Next(): Java EE 7, 8, and Beyond - This is basically our flagship talk at the moment. The talk briefly covers the evolution of the platform, details the changes in Java EE 7 and invites developers to help shape Java EE 8. The materials for the talk is posted here.
  • Android and iOS Development with Java EE 7 - This was a fairly popular technical session at JavaOne San Francisco 2013 led by me, Ryan Cuprak and Bala Muthuvarathan. The session demonstrates how Java EE 7 acts as an excellent platform for building "headless" back-ends for native mobile applications with REST and WebSocket as the client communication protocols. The materials for the talk is posted here. The code is posted on GitHub.
  • Using NoSQL with JPA, EclipseLink and Java EE - This session explores the current state of the NoSQL landscape and how Java EE can be utilized to access NoSQL solutions such as Oracle NoSQL, MongoDB, Cassandra, etc utilizing JPA style facades or plain CDI. The materials for the talk is posted here. The code is posted on GitHub.

Overall, this was a very positive experience with developers giving two thumbs way up to the Java EE 7 platform!

Thursday Sep 19, 2013

CDI Alignment in Bean Validation 1.1

Although it's a point release Bean Validation 1.1 brings a number of important features to Java EE 7. While the most significant of these features is clearly method validation, another very important feature is further alignment with CDI. In Java EE 6 there were a few corner cases left were Bean Validation did not work well with CDI. The most prominent of these gaps was the inability to do injection into custom constraint validators. Projects like Apache CODI and Seam 3 stepped in to fill these gaps. In Java EE 7/Bean Validation 1.1 this issue was properly addressed by making all bean validation artifacts injection officially capable. Romain Manni-Bucau of the Apache TomEE community explains the feature well in a code driven post. There's also the 15-minute Bean Validation 1.1 presentation by specification lead Emmanuel Bernard on the GlassFish videos YouTube channel:

You can also check out the very well written official specification or try things out with the Java EE 7 SDK.

Monday Sep 09, 2013

CDI Scopes One Pager

While figuring out what CDI scope to use for a particular bean, it is sometimes helpful to have a brief one pager like the one put together by Goncalo Marques. The page lists out various CDI scopes, very briefly explains each as well as making mention of proxies and serialization. Although the page is mostly geared towards Java EE 6 and does not yet mention the changes in Java EE 7 such as CDI compatible @ViewScoped, @FlowScoped and @TransactionScoped, the page is still worth bookmarking.

More generally speaking for CDI, there are a number of very good references available such as the very well written Weld reference guide, the CDI portion of the official Java EE Tutorial as well as the spec itself (perhaps one of the most readable Java EE specs around).

Friday Jun 07, 2013

What's New in CDI 1.1?

Introduced in Java EE 6, CDI is clearly a key API to standardize dependency injection in enterprise Java. A lot of the work related to CDI in Java EE now involves treating CDI as an effective mechanism for unifying the Java EE component model and aligning others specs around it to make for a more cohesive platform. Examples of this on-going effort in Java EE 7 includes aligning the JSF component model/scoping to CDI, decoupling declarative transactions from the EJB component model through the JTA 1.2 @Transactional CDI interceptor, modernizing the JMS 2 API utilizing CDI, better support for CDI in Bean Validation 1.1 and many others.

CDI 1.1 itself is a point release that has a relatively long list of smaller but nonetheless important fixes and enhancements. In a recent blog post, CDI 1.1 specification lead Pete Muir does a good job of listing out the changes. The post is especially valuable for the referenced links that you can dig into to see if any of the changes are immediately useful to you if you have adopted CDI already.

Friday May 31, 2013

Java EE@JeeConf 2013

JeeConf 2013 was held in historic Kiev on May 24-25. JeeConf is the most significant Java conference in the Ukraine. The conference attracted a bevy of local and international speakers including Venkat Subramaniam, Yakov Fain, Talip Ozturk, Jacek Laskowski, Mohamed Taman and Sander Mak. Topics included Java SE, NoSQL, cloud, HTML5/JavaScript, Arquillian and of course Java EE.

I delivered two very well attended talks - my talk on Java EE 7 (our flagship talk) and a talk on using NoSQL databases in Java EE. More details on the sessions and JeeConf, including the slide decks and code, posted on my personal blog.

Monday May 06, 2013

Integrating WebSockets and JMS with CDI Events in Java EE 7

One of the first things you will want to know about Java EE 7, is how you can integrate some of the new APIs like WebSockets, with existing ones like JMS. That's why I decided to write this article "Integrating WebSockets and JMS with CDI Events in Java EE 7". What I try to demonstrate here, is how two distinct APIs can be used together to deliver something that is not explicitely defined in the specifications, but with some cool integration using CDI Events, an async server-side data push can be implemented with no more than the standards at your hand.

The article will show you how to code your first WebSocket server endpoint, then how by using a Stateless SessionBean you can send messages to a JMS Queue, and finally, how to receive them again by using a MessageDriven Bean that fires CDI events with the payload back to the WebSocket. Simple, straightforward and small code, that takes you through several specifications and releases available starting from now, the Productivity++ Java EE 7.

Thursday Mar 07, 2013

Java EE@DevNexus 2013

DevNexus 2013 was held in Atlanta on February 18-19. For those unaware, Organized by the Atlanta JUG, DevNexus is the most significant Java conference in the South Eastern US. As usual, DevNexus attracted a bevy of world class speakers including Ben Evans, Neal Ford, David Geary and Venkat Subramaniam.

I delivered two full-house Java EE sessions - one on Java EE 7 (our flagship talk) as well as a session on using NoSQL in Java EE via JPA and CDI. More details on the sessions and DevNexus, including slide decks and code examples, posted on my blog.

Wednesday Jan 23, 2013

Is it really EJB vs CDI?

With the advent of CDI into Java EE 6 and subsequent greater alignment of Java EE and EJB with CDI in Java EE 7, an interesting set of questions keep coming up every now and then: when do I use CDI? When do I use EJB? Do you need EJBs any more? Are EJBs more heavyweight than CDI?

David Blevins (the powerhouse developer behind OpenEJB and TomEE) provided his insightful and eloquent thoughts on these questions. David really knows what he is talking about since in addition to OpenEJB, he is also a core committer on the Apache CDI implementation, OpenWebBeans. We would even go so far as to say it is required reading for anyone interested in Java EE, EJB or CDI. Enjoy!