JavaOne Day 2: Hardcore Java and Java EE 5
While Day 1 saw an overdose of scripting, sanity made a return on Day 2. Although JavaScript technology (whoever came up with that) sessions continued, there was a good supply of deep-dive Java and Java EE sessions to keep me busy. And there was also some WADLing.
Generics and Collections
Generics have been around for a while and most Java programmers have some understanding of it. There have been many criticisms against the erasure based implementation of generics - since parameter types are not reified, constructs that require type information at runtime don't work well. However erasures allow 2 most important benefits - migration compatibility and piecewise generification of existing code. Just these benefits make erasures a necessary bane.
Huge additions have been made to the Collections framework in Java 5 and 6. So much so that many recommend programmers to never use arrays - Collections all the way!
Concurrency Practices
The concurrency classes introduced in Java 5 are a great new toolset for the Java programmer. New concurrency related annotations such as @ThreadSafe
, @GuardedBy
, etc. are being considered. One important aspect to keep in mind - imposing locking requirements on external code is asking for trouble. Ensure that you make your code threadsafe yourself. Performance penalties incurred by synchronized
constructs are overblown.
Immutable objects are your friends - final
is the new private
! They are automatically threadsafe. Object creation is cheap. Aim for less and less mutable state.
Performance is now a function of parallelization - write code such that it can use more cores to get the job done faster. So to improve scalability find serialization in your code and break it up:
- Hold locks for less time
- Use
AtomicInteger
for counters - Use more than one lock
- Consider using
ConcurrentMap
- Consider
ThreadLocal
for heavyweight mutable objects that don't need to be shared
Builder pattern allows you to construct objects cleanly in a valid state with both required and optional parameters. The basic premise can be expressed in code as such:
MyObject myobj = new MyObject.Builder(requiredParam1,requiredParam2).optionalParam1(opt1).optionalParam2(opt2).build();
Generics - avoid raw types in new code. Don't ignore compiler warnings. Annotate local variables with
@SuppressWarnings
rather than the entire method / class. Use bounded wildcards to increase applicability of APIs. 3 take home points for paramterized methods:- Parameterize using
<? extends T>
for reading from a Collection - Parameterize using
<? super T>
for writing to a Collection - Parameterize using
<T>
for methods that both read and write
Java EE 5 Blueprints
Filter.doFilter()
may run in a different thread than the Servlet.service()
method. The Servlet API does not use NIO, however it is possible to implement it yourself. New annotations may make web.xml
optional.The JSP and JSF EL have been unified.
#
is now reserved in JSP 2.1. The javax.faces.ViewState
hidden field (for client side state) will be standardized. Include this field in your AJAX postback. Application-wide configuration of JSF resource bundles will be possible in faces-config.xml
. The <f:verbatim>
tag is no longer needed to interleave HTML and JSF content. @PostConstruct
and @PreDestroy
annotations will be supported for JSF managed beans.WADL
WADL - Web Application Description Language. As a colleague of mine put it - it's WSDL for REST! And IMO that's almost what it is. It's all in good intent to introduce WADL - a formal definition of your REST resources, allows you to automatically stub out code in your favorite language, aids testing. However WADL has many rough edges and I don't see myself using it anytime soon until those have been addressed - overloaded / loosely typed query parameters, security, non-standard content negotiation, et al.
No comments:
Post a Comment