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, href="http://en.wikipedia.org/wiki/Second-system_effect">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
javax.context.SessionScoped—that's where the classes
were way back when
- Be sure to add an empty
- Make your bean serializable
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
@ConversationScoped. Add a field
private @Current Conversation conversation;
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
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:
- JBoss 5.1.0 includes a more modern version of WebBeans than Glassfish
- WebBeans support seems pretty complete, much more than in Glassfish
- JBoss startup is s l o w compared to Glassfish
- Hot deployment is lukewarm in JBoss. I had to restart the server more
often than with Glassfish
- The logs are at
server/default/log/server.log, and they
don't show up as nicely in Eclipse as the Glassfish logs
- You can use JSF 2.0 with JBoss 5.x simply by replacing the jsf-api.jar
and jsf-impl.jar in the
server/default/deploy/jbossweb.sar/jsf-libsdirectory with the
Mojarra 2.0 beta
- The Eclipse adapter for JBoss 5.0 almost works with 5.1, but it doesn't
import the JSR299 API. I had to manually add the JAR file to the project.
fellow has a remedy.