001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.txtimer;
023:
024: // $Id: FixedDelayRetryPolicy.java 57209 2006-09-26 12:21:57Z dimitris@jboss.org $
025:
026: import org.jboss.logging.Logger;
027: import org.jboss.system.ServiceMBeanSupport;
028:
029: import javax.ejb.Timer;
030:
031: /**
032: * This service implements a RetryPolicy that retries
033: * the call to ejbTimeout after a fixed delay.
034: *
035: * @author Thomas.Diesler@jboss.org
036: * @version $Revision: 57209 $
037: * @since 07-Apr-2004
038: */
039: public class FixedDelayRetryPolicy extends ServiceMBeanSupport
040: implements FixedDelayRetryPolicyMBean {
041: // logging support
042: private static Logger log = Logger
043: .getLogger(FixedDelayRetryPolicy.class);
044:
045: // the delay before retry
046: private long delay = 100;
047:
048: /**
049: * Get the delay for retry
050: *
051: * @return delay in ms
052: * @jmx.managed-attribute
053: */
054: public long getDelay() {
055: return this .delay;
056: }
057:
058: /**
059: * Set the delay for retry
060: *
061: * @param delay in ms
062: * @jmx.managed-attribute
063: */
064: public void setDelay(long delay) {
065: this .delay = delay;
066: }
067:
068: /**
069: * Invokes the ejbTimeout method on the TimedObject with the given id.
070: *
071: * @param invoker The invoker for the TimedObject
072: * @param timer the Timer that is passed to ejbTimeout
073: * @jmx.managed-operation
074: */
075: public void retryTimeout(TimedObjectInvoker invoker, Timer timer) {
076: // check if the delay is appropriate
077: if (timer instanceof TimerImpl) {
078: TimerImpl txTimer = (TimerImpl) timer;
079:
080: long periode = txTimer.getPeriode();
081: if (0 < periode && periode / 2 < delay)
082: log
083: .warn("A delay of "
084: + delay
085: + " ms might not be appropriate for a timer periode of "
086: + periode + " ms");
087: }
088:
089: new RetryThread(invoker, timer).start();
090: }
091:
092: /**
093: * The thread that does the actual invocation,
094: * after a short delay.
095: */
096: private class RetryThread extends Thread {
097: private TimedObjectInvoker invoker;
098: private Timer timer;
099:
100: public RetryThread(TimedObjectInvoker invoker, Timer timer) {
101: this .invoker = invoker;
102: this .timer = timer;
103: }
104:
105: public void run() {
106: try {
107: Thread.sleep(delay);
108: log.debug("Retry ejbTimeout: " + timer);
109: invoker.callTimeout(timer);
110: } catch (Exception ignore) {
111: ignore.printStackTrace();
112: }
113: }
114: }
115: }
|