KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > txtimer > FixedDelayRetryPolicy


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.ejb.txtimer;
23
24 // $Id: FixedDelayRetryPolicy.java 37858 2005-11-05 17:06:17Z dimitris $
25

26 import org.jboss.logging.Logger;
27 import org.jboss.system.ServiceMBeanSupport;
28
29 import javax.ejb.Timer JavaDoc;
30
31 /**
32  * This service implements a RetryPolicy that retries
33  * the call to ejbTimeout after a fixed delay.
34  *
35  * @author Thomas.Diesler@jboss.org
36  * @version $Revision: 37858 $
37  * @since 07-Apr-2004
38  */

39 public class FixedDelayRetryPolicy extends ServiceMBeanSupport implements FixedDelayRetryPolicyMBean
40 {
41    // logging support
42
private static Logger log = Logger.getLogger(FixedDelayRetryPolicy.class);
43
44    // the delay before retry
45
private long delay = 100;
46
47    /**
48     * Get the delay for retry
49     *
50     * @return delay in ms
51     * @jmx.managed-attribute
52     */

53    public long getDelay()
54    {
55       return this.delay;
56    }
57
58    /**
59     * Set the delay for retry
60     *
61     * @param delay in ms
62     * @jmx.managed-attribute
63     */

64    public void setDelay(long delay)
65    {
66       this.delay = delay;
67    }
68
69    /**
70     * Invokes the ejbTimeout method on the TimedObject with the given id.
71     *
72     * @param invoker The invoker for the TimedObject
73     * @param timer the Timer that is passed to ejbTimeout
74     * @jmx.managed-operation
75     */

76    public void retryTimeout(TimedObjectInvoker invoker, Timer JavaDoc timer)
77    {
78       // check if the delay is appropriate
79
if (timer instanceof TimerImpl)
80       {
81          TimerImpl txTimer = (TimerImpl)timer;
82
83          long periode = txTimer.getPeriode();
84          if (0 < periode && periode / 2 < delay)
85             log.warn("A delay of " + delay + " ms might not be appropriate for a timer periode of " + periode + " ms");
86       }
87
88       new RetryThread(invoker, timer).start();
89    }
90
91    /**
92     * The thread that does the actual invocation,
93     * after a short delay.
94     */

95    private class RetryThread extends Thread JavaDoc
96    {
97       private TimedObjectInvoker invoker;
98       private Timer JavaDoc timer;
99
100       public RetryThread(TimedObjectInvoker invoker, Timer JavaDoc timer)
101       {
102          this.invoker = invoker;
103          this.timer = timer;
104       }
105
106       public void run()
107       {
108          try
109          {
110             Thread.sleep(delay);
111             log.debug("Retry ejbTimeout: " + timer);
112             invoker.callTimeout(timer);
113          }
114          catch (Exception JavaDoc ignore)
115          {
116             ignore.printStackTrace();
117          }
118       }
119    }
120 }
121
Popular Tags