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();
e.data = "This is a test event";
e.eventTime = new Date();
event.fire(e);
}
}
 
public class EventConsumer {
 
public void afterMyEvent(@Observes MyEvent event) {
// .. Insert event logic here
}
}
view raw CdiAsyncEvents.java 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!

Monday Nov 26, 2012

Pete Muir Interview on CDI 1.1

The 109th episode of the Java Spotlight podcast features an interview with CDI 1.1 spec lead Pete Muir of JBoss/Red Hat. Pete talks with Roger Brinkley about the backdrop to CDI, his work at JBoss, the features in CDI 1.1 and what to expect in the future.

What's going on behind the scenes and the possible contents for CDI 1.1+ are particularly insightful. You can listen to the full interview here.

Monday Nov 19, 2012

CDI 1.1 Public Review and Feedback

CDI 1.1 is humming along nicely and recently released it's public review draft. Although it's just a point release, CDI 1.1 actually has a lot in it. Some the changes include:

  • The CDI class, which provides programmatic access to CDI facilities from outside a managed bean
  • Ability to veto beans declaratively using @Vetoed
  • Conversations in Servlet requests
  • Application lifecycle events in Java EE
  • Injection of Bean metadata into bean instances
  • Programmatic access to a container provided Producer, InjectionTarget, AnnotatedType
  • Ability to override attributes of a Bean via BeanAttributes
  • Ability to process modules via ProcessModule
  • Ability to wrap the InjectionPoint
  • Honor WEB-INF/classes/META-INF/beans.xml to activate WEB-INF/classes in a bean archive
  • Global ordering and enablement of interceptors and decorators
  • Global selection of alternatives
  • @New deprecated
  • Clarify interceptors and decorators must be implemented using proxying
  • Allow multiple annotated types per Java class
  • Allow Extensions to specify the annotations that they are interested in

The CDI 1.1 expert group has a number of open issues that they would like immediate feedback on. These include critical issues like bean visibility, startup events and restricting CDI scans. Read the details here and let your voice be heard!