<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Possible Bug with EJB 3 Timers
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/andy.miller%40jboss.com">Andrig Miller</a> in <i>EJB 3.0</i> - <a href="http://community.jboss.org/message/540195#540195">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>I have recently added an EJB 3 Timer to my application, and in testing it out, the timer never expired, and called the method in the stateless session bean that has the @Timeout annotation.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Here is the code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>package services.ejb;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import java.math.BigDecimal;<br/>import java.util.Calendar;<br/>import java.util.Date;<br/>import java.util.List;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import javax.annotation.Resource;<br/>import javax.ejb.Stateless;<br/>import javax.ejb.Timeout;<br/>import javax.ejb.Timer;<br/>import javax.ejb.TimerService;<br/>import javax.persistence.EntityManager;<br/>import javax.persistence.PersistenceContext;<br/>import javax.persistence.Query;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>import services.ejb.OrderManager;<br/>import services.entities.Address;<br/>import services.entities.Customer;<br/>import services.entities.Order;<br/>import services.entities.OrderLine;<br/>import services.exceptions.CreateDataException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@Stateless<br/>public class OrderManagerBean implements OrderManager {</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160; @Resource<br/>&#160;&#160;&#160; TimerService timerService;<br/>&#160;&#160;&#160; <br/>&#160;&#160;&#160; @PersistenceContext (unitName="services")<br/>&#160;&#160;&#160; protected EntityManager entityManager;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>...</p><p>public void createOrderPurgeTimer(boolean runWithTimer) throws CreateDataException {<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; for (Object object : timerService.getTimers()) {<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Timer timer = (Timer) object;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timer.cancel();<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (runWithTimer) {<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; long twentyFourHours = 1000 * 60 * 60 * 24;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timerService.createTimer(twentyFourHours, twentyFourHours, null);<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160; }<br/>&#160;&#160;&#160; <br/>&#160;&#160;&#160; @Timeout<br/>&#160;&#160;&#160; public void purgeOldOrders(Timer timer) {<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Delete the previous days worth of orders<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Query query = entityManager.createNativeQuery("delete from Order where orderDate &lt; :date");<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; Calendar now = Calendar.getInstance();<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; // Back up to the top of the hour, so we only delete orders from the previous period'<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; now.set(Calendar.MINUTE, 0);<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; now.set(Calendar.SECOND, 0);<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; query.setParameter("date", new Date(now.getTimeInMillis()));<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; query.executeUpdate();<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; return;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br/>&#160;&#160;&#160; }</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In the above stateless session bean, I have a method to create the timer, and the one that is the callback when the timer should expire.&#160; It creates a single timer that should expire every twenty four hours, so the initial expiration, and the interval are both set to 24 hours.&#160; I have a servlet that calls the method to create the timer, along with other reference data needed for the application, and I can see the timer has been persisted in the database, and here is what it looks like:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>TIMERID = 1272464496401, TARGETID = [target=jboss.j2ee:ear=OrderManagerApp.ear,jar=OrderManagerEJB.jar,name=OrderManagerBean,service=EJB3], INITIALDATE=2010-04-29 08:26:29, TIMERINTERVAL=86400000, INSTANCEPK=NULL, INFO=NULL</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So, I created the timer through the createOrderPurgeTimer method on the OrderManagerBean, and everything looks correct.&#160; The INITIALDATE is 24 hours after I created it, and the TIMERINTERVAL is 24 hours in milliseconds, as I would expect.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>So, I was expecting, after allowing the application to run all day and night, that at 8:26:49 this morning (the INITIALDATE) the timer would expire, and call the purgeOldOrders method on the OrderManagerBean.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I know this didn't happen, because there is no error created from the method (maybe it failed), and because in looking at the invocation statistics from the JMX Console I see the following:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>InvocationStatistics concurrentCalls='0'<br/>method name='createOrderPurgeTimer' count='1' minTime='78' maxTime='78' totalTime='78' <br/>method name='createOrder' count='15' minTime='0' maxTime='4' totalTime='18'</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>As you can see, the only two methods called on the bean are the createOrderPurgeTimer, and the createOrder (I didn't include that method in the above code snippet).&#160; No call to the purgeOldOrders method at all, and its more than 2 hours after the INITIALDATE that is in the TIMER table.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I don't believe I have done anything wrong in the code. </p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/540195#540195">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB 3.0 at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>