Search This Blog

Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

2009-12-23

Java EE 6 - CDI (JSR 299) - my first impressions

Hi there!

After taking some time to go through various documents (see link list below) and do some coding samples about the topic CDI, the following things came into my mind:
"lots of annotations", "very abstract specification", "what role will CDI play in JavaEE within the next years?", "can become quite powerful", "I need some best practices".

The specification of CDI (CDI stands for Context and Dependency Injection) with its 92 pages is in my opinion not the right place to start if you want to learn CDI. It is an abstract specification, which shows all the elements a CDI capable container must implement. I really like the specification, but as the code samples didn't really help me (due to missing explanations), I think it is just not the best place to start.
Weld, the reference CDI implementation, offers a great documentation (see link below).
By the way, there are other implementations, too: Resin Candi and Apache Open Web Beans.
If you use Glassfish V3, Weld is included and if you develop using the WebProfile (see earlier post), Weld is even included.

But what is CDi all about? CDI offers..
- an improved lifecycle for stateful objects, bound to well-defined contexts
- a typesafe approach to dependency injection
- object interaction via an event notification facility
- a better approach to binding interceptors to objects, along with a new kind of interceptor, called a decorator, that is more appropriate for use in solving business problems
- an SPI for developing portable extensions to the container.

Gavin King mentioned in the interview:
Originally JSR 299 was called Web Beans, to emphasize its role in making the web technologies of Java EE 6 work better with the transactional access technologies. Originally, one of the key problems we wanted to solve was how can we build an application that uses JSF very easily together with technologies like EJB. So it was originally called Web Beans...It over‑emphasized that this was a web technology, when if you actually look at CDI, it's actually something much more general than that. So after much discussion, the name of the spec ended up being changed to “Context and Dependency Injection."

One of the main buzz words is (Managed) Bean. We have been using beans in Java a lot before. There are Enterprise Java Beans, Message Driven Beans or JSF managed beans. Now, in JavaEE 6 the word bean finally gets a well defined definition.
Managed Beans are defined as container managed objects which support a small set of basic services, such as resource injection, lifecycle callbacks and interceptors
With very few exceptions, almost every concrete Java class (also known as pojo) that has a constructor with no parameters (or a constructor designated with the annotation @Inject) is a bean. This includes every JavaBean and every EJB session bean.

To give you a short impression about what a Managed Bean can look like see my BeyonceBean :)

@Named
@SessionScoped
public class BeyonceBean implements Serializable {

@PrivateMusicLabel
@Inject
private Label label;

@Produces
@Named(value = "singBeyonce")
public String singBeyonce() {
return "Everywhere I am looking now, I am surrounded by your embrace...Halo Halo Halo";
}

public String getInjectedMusicLabelInfo() {
return label.toString();
}
}

If you want to learn CDI you should stick to the Weld Documentation provided by JBoss. There are many well described code samples. Maybe this code sample explains what first came into my mind: " lots of annotations" . I am sure it'll take some time to get used to this way of Java enterprise coding. With CDI you really focus on creating/using lots of annotations, either for type safety reasons, for interceptors or decorators.
UPDATE 01-23-2010
Reza Rahman, a Resin team member, provides some perfect articles for CDI beginners. I really like the way he explains the CDI features. So this link might be a perfect if you are new to CDI.

Another reflection I would like to share with you is my CDI experience with Netbeans 6.8 and Glassfish V3.
- If you don't annotate your Bean with @ManagedBean you don't get the JSF code completion feature. This is very inconvenient. But I am sure the Netbeans developers and community will provide a solution soon.
However, there is actually a more general problem with the @ManagedBeanAnnotation. The article Is @javax.faces.bean.ManagedBean Dead on Arrival?. describes that there are 3 different ways in JavaEE6 of how to define a ManagedBean in the source code: with JSR 316,250/JEE6Spec,AnnotationSpec javax.annotation.ManagedBean, with JSR 314/JSF2Spec javax.faces.bean.ManagedBean and with JSR299/CDISpec javax.inject.Named. So which one should we use? Use the @Named annotation!
- Then I had some problems with the Deploy on Save. When I changed the scope of the bean from request to session, I had to do a clean, otherwise I got a deployment error "Managed bean declaring a passivating scope must be passivation capable" -- which means must implement serializable (even though I added the Serializable interface).
I am going to take a closer look at this and see if there already are some open issues. Otherwise I will submit this problem.

My conclusion:
As I haven't figured out the full capacity of CDI and as it seems to have the power to change the way of writing Java EE applications essentially (or may even become the new JEE6 core programming model), I am very interested in CDI and I will continue reading about it.. As soon as ICEFaces 2.0 will be released I will update my training application from JavaEE5 to EE6 and focus on applying CDI features. As I will have to wait for at least another month I will keep an eye on how CDI is evolving.

Here are the links:
Specification - JCP 299 CDI
Weld documentation - JBOSS about CDI
Gavin King on CDI - tech discussion @ javalobby
CDI capable servers and deployment - see here

UPDATED 01-20-2010:
Java EE 6 Codecamp - Here you can do some CDI homework examples. A great way to learn Java EE.

2009-12-13

Java EE 6 - The Overview

Hi altogether,

While enjoying my christmas holidays in Sweden I have enough time to look at some of the new features Java EE 6 offers.
Here's the overview:

- JavaTM Platform, Enterprise Edition 6 (Java EE 6) Specification, JSR 316
- Contexts and Dependency Injection for the Java EE Platform (CDI), JSR 299
- Enterprise JavaBeans 3.1 (No Interface, Singelton, asynchronous EJBs vs MDBs, Timer), JSR 318
- Java API for RESTful Web Services (JAX-RS), JSR 311
- JPA 2.0 (Support for Pessimistic Locking), JSR 317
- Dependency Injection for Java, JSR 330:
- Bean Validation, JSR 303
- Servlet 3.0, JSR 315
- JSF 2.0, JSR 314
- new Deployment possibilities
- improved/simplified testing

Right now I am focussing on CDI (JSR 299), which is a pretty big new part in Java EE 6. It'll take some time to summarize. In the meantime, I hope you're enjoying your free days over christmas.

So God Jul, Merry Christmas, Frohe Weihnacht, Feliz navidad, Joyeux Noël! or if you need some more translations...

2009-12-10

Waiting for JavaEE 6

Hi alltogether,

After the approval of the Java Enterprise Edition 6 (Java EE 6) Specification we are very close to the final release.
Roberto Chinnici writes in his blog that the release date will be december 10, 2009, when Glassfish V3 will be available.
I am really looking forward to these releases. However, as a user of the ICEFaces Framework, I still have to wait until ICEFaces 2.0 is going to be released. ICEFaces 2.0 will be the JSF2.0 compatible version. Right now there's only a alpha version available.

So, if you like to read more about the new features of JavaEE6 and Glassfish V3, check out the following links:
- JavaEE6 @Sun
- GlassFish V3

2009-11-30

Netbeans 6.8 Release Candidate 1 - my first impression

Hi there,

from now on you can download the release candidate 1 for Netbeans 6.8. download it here
For those who don't know anything about the build types for Netbeans releases here are the possible enums :)
- Milestone
- Early Access
- Beta
- Release Candidate
- Release

I was very curious about this release candidate. The most important feature I am missing / waiting for is the Compile on Save feature for Maven Java EE Projects (vote for it here) However, this feature hasn't been implemented yet. In my opinion, this is a must have feature if you develop javaEE projects with maven and netbeans. So hopefully it will be implemented in the 6.8 release version. Let's wait and see.

Another problem I faced with Netbeans 6.8 M2 was the very frequent Glassfish PermGen Out of memory exception. I've been getting it whenever I published too often (means after about 20 deployments of the ear project within an hour).
While working with this release candidate for the last hour this OutOfMemory Glassfish problem hasn't occured. Good news.

Another problem also vanished. Netbeans sometimes crashed when I was trying to insert jsf value binding in xhtml files.

All in all the Netbeans developers are doing a great work. Keep on developing!!! Thanks a lot for this great IDE.