📄 session5.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Using the Timer Service</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="Session4.html" /> <link rel="Next" href="Session6.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="Session4.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="Session6.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp80525"> </a><h2 class="pHeading1">Using the Timer Service</h2><a name="wp80526"> </a><p class="pBody">Applications that model business work-flows often rely on timed notifications. The timer service of the EJB container enables you to schedule timed notifications for all types of enterprise beans except for stateful session beans. You can schedule a timed notification to occur at a specific time, after a duration of time, or at timed intervals. For example, you could set timers to go off at 10:30 AM on May 23, in 30 days, or every 12 hours. </p><a name="wp80527"> </a><p class="pBody">When a timer expires (goes off), the EJB container calls the <code class="cCode">ejbTimeout</code> method of the bean's implementation class. The <code class="cCode">ejbTimeout</code> method contains the business logic that handles the timed event. Because <code class="cCode">ejbTimeout</code> is defined by the <code class="cCode">javax.ejb.TimedObject</code> interface, the bean class must implement <code class="cCode">TimedObject</code>.</p><a name="wp80528"> </a><p class="pBody">There are four interfaces in the <code class="cCode">javax.ejb</code> package that are related to timers:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80529"> </a><div class="pSmartList1"><li><code class="cCode">TimedObject</code></li></div><a name="wp80530"> </a><div class="pSmartList1"><li><code class="cCode">Timer</code></li></div><a name="wp80531"> </a><div class="pSmartList1"><li><code class="cCode">TimerHandle</code></li></div><a name="wp80532"> </a><div class="pSmartList1"><li><code class="cCode">TimerService</code></li></div></ul></div><a name="wp80533"> </a><h3 class="pHeading2">Creating Timers</h3><a name="wp80534"> </a><p class="pBody">To create a timer, the bean invokes one of the <code class="cCode">createTimer</code> methods of the <code class="cCode">TimerService</code> interface. (For details on the method signatures, see the <code class="cCode">TimerService</code> API documentation.) When the bean invokes <code class="cCode">createTimer</code>, the timer service begins to count down the timer duration. </p><a name="wp80538"> </a><p class="pBody">The bean described in <a href="Session5.html#wp80560">The TimerSessionEJB Example</a> creates a timer as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">TimerService timerService = context.getTimerService();Timer timer = timerService.createTimer(intervalDuration, "created timer");<a name="wp80539"> </a></pre></div><a name="wp80540"> </a><p class="pBody">In the <code class="cCode">TimerSessionEJB</code> example, <code class="cCode">createTimer</code> is invoked in a business method, which is called by a client. An entity bean can also create a timer in a business method. If you want to create a timer for each instance of an entity bean, you could code the <code class="cCode">createTimer</code> call in the bean's <code class="cCode">ejbCreate</code> method.</p><a name="wp80541"> </a><p class="pBody">Timers are persistent. If the server is shut down (or even crashes), timers are saved and will become active again when the server is restarted. If a timer expires while the server is down, the container will call <code class="cCode">ejbTimeout</code> when the server is restarted. </p><a name="wp80542"> </a><p class="pBody">A timer for an entity bean is associated with the bean's identity--that is, with a particular instance of the bean. If an entity bean sets a timer in <code class="cCode">ejbCreate</code>, for example, each bean instance will have its own timer. In contrast, stateless session and message-driven beans do not have unique timers for each instance. </p><a name="wp80543"> </a><p class="pBody">The <code class="cCode">Date</code> and <code class="cCode">long</code> parameters of the <code class="cCode">createTimer</code> methods represent time with the resolution of milliseconds. However, because the timer service is not intended for real-time applications, a callback to <code class="cCode">ejbTimeout</code> might not occur with millisecond precision. The timer service is for business applications, which typically measure time in hours, days, or longer durations.</p><a name="wp80544"> </a><h3 class="pHeading2">Cancelling and Saving Timers</h3><a name="wp80545"> </a><p class="pBody">Timers may cancelled by the following events:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80546"> </a><div class="pSmartList1"><li>When a single-event timer expires, the EJB container calls <code class="cCode">ejbTimeout</code> and then cancels the timer.</li></div><a name="wp80547"> </a><div class="pSmartList1"><li>When an entity bean instance is removed, the container cancels the timers associated with the instance.</li></div><a name="wp80548"> </a><div class="pSmartList1"><li>When the bean invokes the <code class="cCode">cancel</code> method of the <code class="cCode">Timer</code> interface, the container cancels the timer.</li></div></ul></div><a name="wp80549"> </a><p class="pBody">If a method is invoked on a cancelled timer, the container throws the <code class="cCode">javax.ejb.NoSuchObjectLocalException</code>.</p><a name="wp80550"> </a><p class="pBody">To save a <code class="cCode">Timer</code> object for future reference, invoke its <code class="cCode">getHandle</code> method and store the <code class="cCode">TimerHandle</code> object in a database. (A <code class="cCode">TimerHandle</code> object is serializable.) To re-instantiate the <code class="cCode">Timer</code> object, retrieve the handle from the database and invoke <code class="cCode">getTimer</code> on the handle. A <code class="cCode">TimerHandle</code> object cannot be passed as an argument of a method defined in a remote or Web service interface. In other words, remote clients and Web service clients cannot access a bean's <code class="cCode">TimerHandle</code> object. Local clients, however, do not have this restriction. </p><a name="wp80551"> </a><h3 class="pHeading2">Getting Timer Information</h3><a name="wp80552"> </a><p class="pBody">In addition to defining the <code class="cCode">cancel</code> and <code class="cCode">getHandle</code> methods, the <code class="cCode">Timer</code> interface also defines methods for obtaining information about timers:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public long getTimeRemaining();public java.util.Date getNextTimeout();public java.io.Serializable getInfo();<a name="wp80553"> </a></pre></div><a name="wp80554"> </a><p class="pBody">The <code class="cCode">getInfo</code> method returns the object that was the last parameter of the <code class="cCode">createTimer</code> invocation. For example, in the <code class="cCode">createTimer</code> code snippet of the preceding section, this information parameter is a <code class="cCode">String</code> object with the value, <code class="cCode">created timer</code>.</p><a name="wp80555"> </a><p class="pBody">To retrieve all of a bean's active timers, call the <code class="cCode">getTimers</code> method of the <code class="cCode">TimerService</code> interface. The <code class="cCode">getTimers</code> method returns a collection of <code class="cCode">Timer</code> objects. </p><a name="wp80556"> </a><h3 class="pHeading2">Transactions and Timers</h3><a name="wp80557"> </a><p class="pBody">An enterprise bean usually creates a timer within a transaction. If this transaction is rolled back, the timer creation is also rolled back. Similarly, if a bean cancels a timer within a transaction that gets rolled back, the timer cancellation is rolled back. In this case, the timer's duration is reset as if the cancellation had never occurred.</p><a name="wp80558"> </a><p class="pBody">In beans with container-managed transactions, the <code class="cCode">ejbTimeout</code> method usually has the <code class="cCode">RequiresNew</code> transaction attribute. With this attribute, the EJB container begins the new transaction before calling <code class="cCode">ejbTimeout</code>. If the transaction is rolled back, the container will try to call <code class="cCode">ejbTimeout</code> at least one more time.</p><a name="wp80560"> </a><h3 class="pHeading2">The TimerSessionEJB Example</h3><a name="wp80561"> </a><p class="pBody">The source code for this example is in the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/ejb/timersession/src/</code> directory.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -