Java Reference
In-Depth Information
It should be noted that by default timers operate within the time zone where the server
is running. To specify a different time zone, you'd use the timezone attribute and
specify a value from the IANA Time Zone Database. The valid values are specified in the
Zone Name column. You can also query the java.util.TimeZone class to find the
string representation. To put this in perspective, let's put declarative timers to use in Ac-
tionBazaar.
7.2.4. Declarative timer example
Let's explore the features of the declarative cron timer by using it to send out monthly and
holiday newsletters. The monthly newsletter highlights items that attracted heavy bidding
along with similar items and profiles of select sellers. In addition, a holiday newsletter will
be broadcast to coincide with major holidays and special events such as Black Friday. Both
tasks will make use of declarative cron timers.
To implement this feature, you'll add a new stateless session bean, Newsletter , to Ac-
tionBazaar. The method skeleton is shown in listing 7.2 . The Newsletter class will use
container-managed persistence. Because requesting the list of users is restricted to admin-
istrative users, the Newsletter bean will run as an administrator so that a security viola-
tion exception isn't thrown. Remember that timer methods execute as the nonauthenticated
user.
One important use case to consider is what happens in the event of a container crash while
the Newsletter bean is in the process of sending out newsletters. The container will at-
tempt to reexecute the method when the container is restarted. Emailing a large distribution
list will take a long time and the email service isn't transactional. To get around this lim-
itation, you can use JMS. As the service iterates over the users to be emailed, a new JMS
message is inserted into the queue. If for any reason the method fails, no emails are sent and
the transaction is rolled back. Additionally, with JMS beans sending out the actual emails,
the email load can be load-balanced with multiple message-driven bean instances process-
ing email requests. The code for this advanced behavior isn't shown in the following listing
but is available online. This demonstrates how the various aspects of EJB thus covered can
be used to build a robust service.
Search WWH ::




Custom Search