WebBeans in Glassfish v3

As I am updating my share of chapters in the Core JavaServer Faces book (with the hard parts fortunately being tackled by my coauthor, David Geary), I started playing with WebBeans, erm, Java Contexts and Dependency Injection. I'll keep calling it WebBeans—the alternative JavaCandi is more than I can take. There are two features that are crucial for JSF users: Conversation scope and access to stateful session beans from JSF pages. (Seam users know all about this and can skip this blog.)

I have been using Glassfish for most of my test programs, mostly because I am used to it. But today, as I did some experiments with JBoss, I found another reason to like Glassfish—it boots a lot faster. (I remember a rather dismal Java One session with Jerome Dochez a couple of years ago where he demoed the embryonic Glassfish v3 and how it was wonderfully modular, and I thought “Oh boy, Second System Syndrome”. Well, I was wrong, and Glassfish v3 is wonderfully fast.)

The current Glassfish v3 Preview has some support for WebBeans. As of today, it contains a 2-month old version of WebBeans and a more recent WebBeans integration module (look for Glassfish JCDI in the update tool).

I got my first trivial app to work after paying attention to these points:

Then simply replace @ManagedBean @SessionScoped with @Named @SessionScoped, and you are good to go.

Of course, that's just another way of achieving what you can already do with JSF 2.0. For something strictly better, turn to conversation scope. In JSF, you have the uncomfortable choice between request scope (which is too short) and session scope (which is too long). JSF 2.0 gives us view scope and a flash object, neither of which is all that helpful in general. Conversation scope is what you want. You turn it on when you want to start a sequence of interactions with the user, then turn it off when that interaction has finished. The user can open two browser tabs with different conversations.

It's easy with WebBeans, and it works with the current Glassfish v3. Change @SessionScoped to @ConversationScoped. Add a field

private @Current Conversation conversation;

and calls

conversation.begin();

and

conversation.end();

into suitable action methods. The life of the bean is extended from request to conversation scope when you call begin, and it ends when you call end.

Perhaps this should have been a feature of JSF 2.0, but I take it any way I can get it.

The other key feature is the ability to skip managed beans altogether and to invoke stateful session beans from your JSF pages. This does not seem to work in the current Glassfish, which is too bad. I get a warning

INFO: Web Beans 1.0.0.PREVIEW1
INFO: Transactional services not available. Transactional observers will be invoked synchronously.
INFO: EJB services not available. Session beans will be simple beans, injection into non-contextual EJBs, injection of @EJB in simple beans, injection of Java EE resources and JMS resources will not be available.
INFO: JPA services not available. Injection of @PersistenceContext will not occur. Entity beans will be discovered as simple beans.
INFO: @Resource injection not available.

That's when I installed JBoss 5.1.0GA. Here are a few things I learned: