The 8 Nights of Java – Night 6

Continuing on our 8 Nights of Java series, we move on to Java 6. Java 6 was about making Java “more”. As a result, a lot of the changes were important, but didn’t affect a developers day-to-day tasks. For example, annotations affected tool and library developers but were mostly unused for regular developers. Java 6 was initially released by Sun Microsystems, and later taken over by Oracle as part of the acquisition. This was also the last version of Java in which the Professional certification (SCJP renamed to OCP) could be completed with a single exam. Starting with Java 7 and continuing in Java 8, two exams are required to obtain an OCP certification.

Jump to: [Night 1 | Night 2 | Night 3 | Night 4 | Night 5 | Night 6 | Night 7 | Night 8]

Java 6 Notable Features
Sun released Java 6 (codename Mustang) on December 11, 2006. Notice they dropped “.0” from the version number, going from Java 5.0 to 6? This would be the last version of Java released by Sun, with Oracle taking over the Java 6 updates in 2010. Of all the versions of Java, version 6 was out there the longest, with 5 years (and 131 updates) before the release of Java 7.  Key new functionality included:

  • Renaming J2SE to Java SE
  • Common annotations support
  • JavaScript Rhino support
  • Performance improvements
  • JDBC 4.0

From Jeanne:

I have no memories of Java 6. I remember using it. While it was nice to finally ditch the “2” from the name of J2SE, I can’t think of a single thing that was new in it that I cared about. That said, the commons annotations support was a big deal. It let libraries like JUnit and Spring use custom annotations to be more expressive and easy to use. I do appreciate the feature and know it came from language updates. But since I used it when the libraries came out rather than when Java 6 came out, I don’t intuitively think of it as a Java 6 feature.

This does seem like a good place to reflect on the change in stewardship. We’ve been hearing about Oracle’s takeover spelling the death of Java since at least 2009. That’s right. The worries started even before Oracle acquired Sun. The rumors are still with us and stronger. They’ve moved more to the Java EE space more than in the core Java space. Oracle did rename the certifications from things like SCEA (Sun Certified Enterprise Architect) to OCEA (Oracle Certified Enterprise Architect). No surprise there. They also controversially added a requirement to take a class to a few certs to be more consistent with the database cert. Luckily, this requirement does not exist for the certifications Scott and I have written books about!

From Scott:

I have to agree with Jeanne on this. While Java 6 was a maturing release, with lots of new methods and features built on top of existing APIs, it paled in comparison to the Java 5.0 release in terms of scope. In other words, there wasn’t a compelling reason to go out and install Java 6 on all of your servers and desktops. As Jeanne started, the biggest change was really about Oracle’s acquisition of Java and handover of the platform and staff (remember Sun employees overnight became Oracle employees!). Java 7 was delayed almost 5 years because of this transition. As I said on Night 1, people have been predicting the death of Java since the first year it existed and the acquisition by Oracle was no different. While Oracle did make changes to Java, especially to the certifications, it’s fair to say they kept most things intact, helping Java to continue to thrive today.

One other important note. Java 6 had the most number of updates and patches, in part because so many “zero day” bugs were discovered. I’m not sure this was a deficit in Java, so much as this was Java spreading as a server platform across the web. Any server technology must be constantly kept up-to-date, as exploits are discovered all the time in the wild. As of April 2015, Oracle no longer publicly supports Java 6. Any servers running Java 6 must be updated and any that are not are prime targets for hackers.

Live from TSSJS – Lightweight J2EE with Adam

For my final TheServerSide Java Symposium breakout session for the day, I am attending “Lightweight Application Development with Java EE 6” presented by Adam Bien.  Adam’s talk is in part an attempt to bring former J2EE developers (myself included) back to the table who may have been driven off by previous, heavy-weight J2EE implementations, such as J2EE 1.4.

Adam spends most of his presentation actually building a simple Java EE 6 application on the fly, using Casino/Police/Mafia metaphor (since this conference is in Las Vegas).  I’ll try to capture the highlights in this blog, but the code itself says volumes about how simple to use Java EE 6 has become.  He develops in NetBeans IDe 6.9.1 with Glassfish as a web server, deploying everything as a WAR.

1.  Simplifying Code with Annotations
Java EE 6, like Java EE 5 before it, solves a lot of complexity and deployment issues through Java 1.5’s creation of annotations.  Adam shows a code example of a Stateless session bean is essentially just a POJO with the @Stateless annotation.  The only required import is “javax.ejb.Stateless” for the annotation to reference.

2.  Interceptors
Java EE 6 supports @Interceptors that redirect business methods for a better separation of POJOs and business logic.  Serves as a filter for business methods.

3.  Dependency Injection
The @Inject tag allows you create instances of related objects that live as long as the parent module.  Easy to create events and inject them into classes.

4.  Qualifiers
Adapted from Spring, helps control event ordering as far as which observer gets the message.  The annotation @Qualifier is used on the class level with a @Retention and @Target parameters.  The listener can then be updated to only listen to events of particular types or values.  Furthermore, they can be use for filtering on events which is useful for batch processing.

5.  Messaging
Java EE 6 supports @Asynchronous annotation for creating messaging handlers.

6.  Interfaces
Similar to previous versions of J2EE, you can define an interface for a session bean and use references to the interface instead of the concrete class.  Compiler throws an error if you try to inject an interface with no concrete implementation.

7.  J2EE and Spring
Adam recommends not mixing the two, and building entirely in Spring xor J2EE 5 or 6.  The features are very similar between the two and mixing them can be complicated.  One of the major new features of Java EE 6 is REST integration.

Conclusion
Adam did not present any slides, but built a quite impressive Java EE 6 application on the fly with very little code.  Everything he built deployed instanteously on his MacBook, and was accessible from the web browser right away.  Adam pointed out not all computers or operating systems build as quickly.

I asked Adam if he would be posting his code online since it was quite educational.  He said he will once he is back in Germany.

EJB3 – annotations vs xml

Scott’s recent post on EJB3 got me thinking about annotations as a “replacement” for XML.

By now, we all know why shoving everything in XML isn’t the best of ideas.  I think shoving everything in Java code is bad too.  In particular deployment time concerns (like security) shouldn’t require a recompile.

JEE 5 offers the ability to choose whether to use all annotations, all XML or a mix of annotations and a partial deployment descriptor.  Yet most of the articles and books I’ve seen encourage using annotations for everything and the deployment descriptor as some kind of legacy practice or anti-pattern.  This reminds me of the situation where most people agree code in a JSP is bad practice and yet it keeps coming up due the vast quantity of beginner books with code in the JSP.  From reading the books, it sounds like EJB deployment descriptor XML = bad regardless of whether that is the case in practice.

I do think the annotation approach is fine for the mapping – unless you are developing a common component that will be deployed to different schema definitions.  Most of the time, the schema is stable as Scott noted.

Let’s look at some of the things in an EJB deployment descriptor (for a session bean):

  • Bean Type – This is a coding concern and as such fits well in the Java code.  If my bean changes from stateful to stateless, I should be looking at my code.
  • Security Settings -This is a deployment time concern.  There’s no reason a role change should mandate a redeployment.  Or that the developers know this information.  The application assembler or deployer could add this in.  As such, the security settings are well suited to being in an XML file.  This also has the advantage of a reusable component provider being able to provide generic information and the integrating applications specify XML info specific to their application.
  • Transaction Settings – This one could be argued either way.  If certain settings such as “required new” are needed, it makes sense to specify in the code to hint at this.  At the same time, the correct transaction setting could depend on the integrating application.  I think transaction settings could be a use case for specifying as a Java annotation and allowing/suggesting the integrating application override in XML.
  • Resource References -Resources are both a coding concern and a deployment time concern.  The code certainly cares that the correct resources exist.  And the deployer cares that they are linked correctly.  Luckily, this scenario has existed for years and there already exists an approach.  The reference name and link to the JNDI can be specified as Java annotations since they are coding concerns and likely stable.  The deployer has always been responsible for setting up the correct resource in the JNDI.

None of these are hard and fast rules.  They are just meant to get people thinking about when to use Java annotations vs XML for the deployment descriptors.  We don’t want to just use Java annotations blindly because they are there.

As more people migrate to EJB 3, I think we are going to see some of a “I’m not going to have a deployment descriptor at all now that I can do everything in Java” mentality.  We’ll have to see if it is going to take a swing to far in the other direction (no XML) before people realize some things belong in Java while others belong in XML.