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 7 ■ SESSION BEANS AND THE TIMER SERVICEListing 7-8. A Singleton Session Bean Not Allowing Concurrency@Singletonpublic class CacheEJB {private Map cache = new HashMap();@AccessTimeout(0)public void addToCache(Long id, Object object) {if (!cache.containsKey(id))cache.put(id, object);}public void removeFromCache(Long id) {if (cache.containsKey(id))cache.remove(id);}}public Object getFromCache(Long id) {if (cache.containsKey(id))return cache.get(id);elsereturn null;}Session Bean ModelSo far you’ve seen examples of code using the easiest programming model for session beans: anannotated POJO <strong>with</strong> no interface. But, depending on your needs, session beans can give you a muchricher model, allowing you to perform remote calls, dependency injection, or asynchronous calls.Interfaces and Bean ClassUntil now you’ve seen session beans composed only of a bean class. In fact, they can be richer and madeof the following elements:• Business interfaces: These interfaces contain the declaration of business methodsthat are visible to the client and implemented by the bean class. A session beancan have local interfaces, remote interfaces, or no interface at all (a no-interfaceview <strong>with</strong> local access only).• A bean class: The bean class contains the business method implementation andcan implement zero or several business interfaces. The session bean must beannotated <strong>with</strong> @Stateless, @Stateful, or @Singleton depending on its type.As shown in Figure 7-5, a client application can access a session bean by one of its interfaces (localor remote) or directly by invoking the bean class itself.214

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

Saved successfully!

Ooh no, something went wrong!