12.07.2015 Views

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

Beginning Java EE 6 with GlassFish 3, Second Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 1 ■ JAVA <strong>EE</strong> 6 AT A GLANCEJAX-RS specification. As shown in Listing 1-3, a RESTful web service is an annotated <strong>Java</strong> class thatresponds to HTTP actions. You will learn more about JAX-RS in Chapter 15.Listing 1-3. A RESTful Web Service@Path("books")public class BookResource {@PersistenceContext(unitName = "chapter01PU")private EntityManager em;}@GET@Produces({"application/xml", "application/json"})public List getAllBooks() {Query query = em.createNamedQuery("findAllBooks");List books = query.getResultList();return books;}The new version of the persistence API (JPA 2.0) is improved by adding collections of simple datatypes (String, Integer, etc.), pessimistic locking, a richer JPQL syntax, a brand-new Criteria API, andsupport for a caching API. JPA is discussed in Chapter 2 through Chapter 5.EJBs are easier to develop (<strong>with</strong> optional interfaces) and to package (in a war file), but they also havenew features, such as the possibility to use asynchronous calls or a richer timer service for schedulingtasks. There is also a new singleton session bean component. As shown in Listing 1-4, a singleannotation can turn a <strong>Java</strong> class into a container-managed singleton (one instance of the component perapplication). You will learn more about these new features in Chapter 6 through Chapter 9.Listing 1-4. A Singleton Session Bean@Singletonpublic class CacheEJB {private Map cache = new HashMap();public void addToCache(Long id, Object object) {if (!cache.containsKey(id))cache.put(id, object);}}public Object getFromCache(Long id) {if (cache.containsKey(id))return cache.get(id);elsereturn null;}17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!