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 SERVICEinitialized, the method gets the container context (EJBContainer.getContext(), which returns ajavax.naming.Context) to look up the ItemEJB (using the portable global JNDI name syntax).Note that ItemEJB (shown earlier in Listing 7-1) is a stateless session bean exposing businessmethods through a no-interface view. It uses injection, container-managed transactions, and a JPA Bookentity. The embeddable container takes care of injecting an entity manager and committing or rollingback any transaction. The closeContainer() method invokes the EJBContainer.close() method to shutdown the embeddable container instance.I’ve used a test class in this example to show you how to use an embeddable EJB container. But bearin mind that EJBs can now be used in any kind of <strong>Java</strong> SE environment: from test classes to Swingapplications, or even just a Main class <strong>with</strong> a public static void main().The Timer ServiceSome <strong>Java</strong> <strong>EE</strong> applications need to schedule tasks in order to get notified at certain times. For example,the CD-BookStore application needs to send a birthday e-mail to its customers every year, print monthlystatistics about the items sold, generate nightly reports about inventory levels, and refresh a technicalcache every 30 seconds.As a result, EJB 2.1 introduced a scheduling facility called the timer service because clients couldn’tuse the Thread API directly. Compared <strong>with</strong> other proprietary tools or frameworks (the Unix cron utility,Quartz, etc.), the timer service was less feature-rich. The EJB specification had to wait until the 3.1version to see a drastic improvement of the timer service. It took inspiration from Unix cron and othersuccessful tools, and today competes <strong>with</strong> the other products as it responds to most scheduling usecases.The EJB timer service is a container service that allows EJBs to be registered for callback invocation.EJB notifications may be scheduled to occur according to a calendar-based schedule, at a specific time,after a specific elapsed duration, or at specific recurring intervals. The container keeps a record of all thetimers, and invokes the appropriate bean instance method when a timer has expired. Figure 7-7 showsthe two steps involving the timer service. First, the EJB needs to create a timer (automatically orprogrammatically) and get registered for callback invocation, and then the timer service triggers theregistered method on the EJB instance.Figure 7-7. Interaction between the timer service and the session beanTimers are intended for long-lived business processes and are by default persistent. This means theysurvive server shutdowns, and, once the server starts again, the timers are executed as if no shutdownhad happened. Optionally, you can specify timers to be nonpersistent.230

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

Saved successfully!

Ooh no, something went wrong!