[jboss-cvs] jmx/src/main/javax/management/timer ...
Dimitris Andreadis
dimitris.andreadis at jboss.com
Thu Jul 13 13:58:25 EDT 2006
User: dimitris
Date: 06/07/13 13:58:25
Modified: src/main/javax/management/timer Tag: Branch_3_2 Timer.java
Log:
JBAS-3281, don't apply this to 3.2.x, rollback to the original version except that the timer doesn't need to be Serializable
Revision Changes Path
No revision
No revision
1.10.4.4 +357 -384 jmx/src/main/javax/management/timer/Attic/Timer.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Timer.java
===================================================================
RCS file: /cvsroot/jboss/jmx/src/main/javax/management/timer/Attic/Timer.java,v
retrieving revision 1.10.4.3
retrieving revision 1.10.4.4
diff -u -b -r1.10.4.3 -r1.10.4.4
--- Timer.java 13 Jul 2006 13:22:00 -0000 1.10.4.3
+++ Timer.java 13 Jul 2006 17:58:25 -0000 1.10.4.4
@@ -32,7 +32,6 @@
import javax.management.NotificationBroadcasterSupport;
import javax.management.ObjectName;
-import org.jboss.logging.Logger;
import org.jboss.mx.util.RunnableScheduler;
import org.jboss.mx.util.SchedulableRunnable;
@@ -40,63 +39,90 @@
* The timer service.
*
* @author <a href="mailto:Adrian.Brock at HappeningTimes.com">Adrian Brock</a>
- * @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
- * @version $Revision: 1.10.4.3 $
+ * @version $Revision: 1.10.4.4 $
*/
public class Timer extends NotificationBroadcasterSupport
implements TimerMBean, MBeanRegistration
{
- // logging support
- private static Logger log = Logger.getLogger(Timer.class);
-
// Constants -----------------------------------------------------
- /** The number of milliseconds in one second. */
+ /**
+ * The number of milliseconds in one second.
+ */
public static final long ONE_SECOND = 1000;
- /** The number of milliseconds in one minute. */
+ /**
+ * The number of milliseconds in one minute.
+ */
public static final long ONE_MINUTE = ONE_SECOND * 60;
- /** The number of milliseconds in one hour. */
+ /**
+ * The number of milliseconds in one hour.
+ */
public static final long ONE_HOUR = ONE_MINUTE * 60;
- /** The number of milliseconds in one day. */
+ /**
+ * The number of milliseconds in one day.
+ */
public static final long ONE_DAY = ONE_HOUR * 24;
- /** The number of milliseconds in one week. */
+ /**
+ * The number of milliseconds in one week.
+ */
public static final long ONE_WEEK = ONE_DAY * 7;
- /** Don't send notifications at initial start up. */
+ /**
+ * Don't send notifications at initial start up.
+ */
private static final int SEND_NO = 0;
- /** Send all past notifications at initial start up. */
+ /**
+ * Send all past notifications at initial start up.
+ */
private static final int SEND_START = 1;
- /** Normal operation sending */
+ /**
+ * Normal operation sending
+ */
private static final int SEND_NORMAL = 2;
// Attributes ----------------------------------------------------
- /** The next notification id. */
+ /**
+ * The next notification id.
+ */
int nextId = 0;
- /** The next notification sequence number. */
+ /**
+ * The next notification sequence number.
+ */
long sequenceNumber = 0;
- /** The send past events attribute. */
+ /**
+ * The send past events attribute.
+ */
boolean sendPastNotifications = false;
- /** Whether the service is active. */
+ /**
+ * Whether the service is active.
+ */
boolean active = false;
- /** Our object name. */
+ /**
+ * Our object name.
+ */
ObjectName objectName;
- /** The registered notifications. */
+ /**
+ * The registered notifications.
+ */
HashMap notifications = new HashMap();
- /** The scheduler */
- private RunnableScheduler scheduler = new RunnableScheduler();
+ /**
+ * The scheduler.
+ * Not serializable.
+ */
+ private transient RunnableScheduler scheduler = new RunnableScheduler();
// Static --------------------------------------------------------
@@ -106,46 +132,22 @@
// TimerMBean implementation -------------------------------------
- public Integer addNotification(String type, String message, Object userData, Date date)
+ public Integer addNotification(String type, String message, Object userData,
+ Date date)
throws IllegalArgumentException
{
return addNotification(type, message, userData, date, 0);
}
- public Integer addNotification(String type, String message, Object userData, Date date, long period)
+ public Integer addNotification(String type, String message, Object userData,
+ Date date, long period)
throws IllegalArgumentException
{
return addNotification(type, message, userData, date, period, 0);
}
- public Integer addNotification(String type, String message, Object userData, Date date, long period, long occurences)
- throws IllegalArgumentException
- {
- return addNotification(type, message, userData, date, period, occurences, false);
- }
-
- /**
- * Creates a new timer notification with the specified type, message and userData and inserts it into the list of notifications with a given date, period and number of occurences.
- * <p/>
- * If the timer notification to be inserted has a date that is before the current date, the method behaves as if the specified date were the current date.
- * For once-off notifications, the notification is delivered immediately.
- * For periodic notifications, the first notification is delivered immediately and the subsequent ones are spaced as specified by the period parameter.
- * <p/>
- * Note that once the timer notification has been added into the list of notifications, its associated date, period and number of occurences cannot be updated.
- * <p/>
- * In the case of a periodic notification, the value of parameter fixedRate is used to specify the execution scheme, as specified in Timer.
- *
- * @param type The timer notification type.
- * @param message The timer notification detailed message.
- * @param userData The timer notification user data object.
- * @param date The date when the notification occurs.
- * @param period The period of the timer notification (in milliseconds).
- * @param nbOccurences The total number the timer notification will be emitted.
- * @param fixedRate If true and if the notification is periodic, the notification is scheduled with a fixed-rate execution scheme. If false and if the notification is periodic, the notification is scheduled with a fixed-delay execution scheme. Ignored if the notification is not periodic.
- * @return The identifier of the new created timer notification.
- * @throws IllegalArgumentException The period or the number of occurences is negative
- */
- public Integer addNotification(String type, String message, Object userData, Date date, long period, long nbOccurences, boolean fixedRate)
+ public Integer addNotification(String type, String message,
+ Object userData, Date date, long period, long occurences)
throws IllegalArgumentException
{
// Generate the next id.
@@ -155,7 +157,8 @@
// Validate and create the registration.
RegisteredNotification rn =
- new RegisteredNotification(id, type, message, userData, date, period, nbOccurences, fixedRate);
+ new RegisteredNotification(id, type, message, userData, date, period,
+ occurences);
// Add the registration.
synchronized(notifications)
@@ -203,23 +206,6 @@
return new Long(rn.occurences);
}
- /**
- * Gets a copy of the flag indicating whether a peridic notification is executed at fixed-delay or at fixed-rate.
- *
- * @param id The timer notification identifier.
- * @return A copy of the flag indicating whether a peridic notification is executed at fixed-delay or at fixed-rate.
- */
- public Boolean getFixedRate(Integer id)
- {
- // Make sure there is a registration
- RegisteredNotification rn = (RegisteredNotification) notifications.get(id);
- if (rn == null)
- return null;
-
- // Return a copy of the fixedRate
- return new Boolean(rn.fixedRate);
- }
-
public Vector getNotificationIDs(String type)
{
Vector result = new Vector();
@@ -322,9 +308,6 @@
public void removeNotification(Integer id)
throws InstanceNotFoundException
{
-
- log.debug("removeNotification: " + objectName + ",id=" + id);
-
// Check if there is a notification.
synchronized(notifications)
{
@@ -344,8 +327,6 @@
{
boolean found = false;
- log.debug("removeNotifications: " + objectName + ",type=" + type);
-
// Loop through the notifications removing the passed type.
synchronized(notifications)
{
@@ -370,7 +351,6 @@
public void setSendPastNotifications(boolean value)
{
- log.debug("setSendPastNotifications: " + objectName + ",value=" + value);
sendPastNotifications = value;
}
@@ -381,11 +361,9 @@
return;
active = true;
- log.debug("start: " + objectName + " at " + new Date());
-
// Perform the initial sends, for past notifications send missed events
// otherwise ignore them
- synchronized (notifications)
+ synchronized(notifications)
{
Iterator iterator = notifications.values().iterator();
while (iterator.hasNext())
@@ -410,8 +388,6 @@
if (active == false)
return;
- log.debug("stop: " + objectName + ",now=" + new Date());
-
// Stop the threads
active = false;
scheduler.stop();
@@ -471,17 +447,14 @@
if (rn.sendType != SEND_NO)
{
long seq = 0;
- synchronized (this)
+ synchronized(this)
{
seq = ++sequenceNumber;
}
-
- log.debug("sendNotification: " + rn);
- TimerNotification tn = new TimerNotification(rn.type, objectName,
- seq, rn.nextDate, rn.message, rn.id);
- tn.setUserData(rn.userData);
- sendNotification(tn);
+ sendNotification(new TimerNotification(rn.type, objectName, seq,
+ rn.nextDate, rn.message, rn.id, rn.userData));
}
+
// Calculate the next date.
// Except for when we are sending past notifications at start up,
// it cannot be in the future.
@@ -490,14 +463,13 @@
// If no next run, remove it sets the next date to zero.
if (rn.calcNextDate() == false)
{
- synchronized (notifications)
+ synchronized(notifications)
{
- log.debug("remove: " + rn);
notifications.remove(rn.id);
}
}
}
- while (isActive() && rn.sendType != SEND_START && rn.nextDate != 0
+ while (isActive() == true && rn.sendType != SEND_START && rn.nextDate != 0
&& rn.occurences == 0 && rn.nextDate < System.currentTimeMillis());
}
@@ -510,38 +482,54 @@
/**
* A registered notification. These run as separate threads.
*/
- private class RegisteredNotification extends SchedulableRunnable
+ private class RegisteredNotification
+ extends SchedulableRunnable
{
// Attributes ----------------------------------------------------
- /** The notification id. */
+ /**
+ * The notification id.
+ */
public Integer id;
- /** The notification type. */
+ /**
+ * The notification type.
+ */
public String type;
- /** The message. */
+ /**
+ * The message.
+ */
public String message;
- /** The user data. */
+ /**
+ * The user data.
+ */
public Object userData;
- /** The start date. */
+ /**
+ * The start date.
+ */
public long startDate;
- /** The period. */
+ /**
+ * The period.
+ */
public long period;
- /** The maximum number of occurences. */
+ /**
+ * The maximum number of occurences.
+ */
public long occurences;
- /** The flag to indicate fixedRate notifications, or fixedDelay (default) */
- public boolean fixedRate;
-
- /** The send type, no send, past notifications or normal */
+ /**
+ * The send type, no send, past notifications or normal
+ */
public int sendType = SEND_NORMAL;
- /** The next run date */
+ /**
+ * The next run date
+ */
public long nextDate = 0;
// Constructors --------------------------------------------------
@@ -553,22 +541,19 @@
* @param type the notification type.
* @param message the notification's message string.
* @param userData the notification's user data.
- * @param startDate the date/time the notification will occur.
+ * @param date the date/time the notification will occur.
* @param period the repeat period in milli-seconds. Passing zero means
* no repeat.
* @param occurences the maximum number of repeats. When the period is not
* zero and this parameter is zero, it will repeat indefinitely.
- * @param fixedRate If true and if the notification is periodic, the notification
- * is scheduled with a fixed-rate execution scheme. If false and if the notification
- * is periodic, the notification is scheduled with a fixed-delay execution scheme.
- * Ignored if the notification is not periodic.
- *
+ * @return the notification id for this notification.
* @exception IllegalArgumentException when the date is before the current
* date, the period is negative or the number of repeats is
* negative.
*/
- public RegisteredNotification(Integer id, String type, String message, Object userData,
- Date startDate, long period, long occurences, boolean fixedRate)
+ public RegisteredNotification(Integer id, String type, String message,
+ Object userData, Date startDate,
+ long period, long occurences)
throws IllegalArgumentException
{
// Basic validation
@@ -579,26 +564,24 @@
if (occurences < 0)
throw new IllegalArgumentException("Negative Occurences");
- this.startDate = startDate.getTime();
- if (startDate.getTime() < System.currentTimeMillis())
- {
- log.debug("startDate [" + startDate + "] in the past, set to now");
- this.startDate = System.currentTimeMillis();
- }
-
// Remember the values
this.id = id;
this.type = type;
this.message = message;
this.userData = userData;
+ this.startDate = startDate.getTime();
this.period = period;
this.occurences = occurences;
- this.fixedRate = fixedRate;
- this.nextDate = this.startDate;
-
- String msgStr = "new " + this.toString();
- log.debug(msgStr);
+ // Can we get a next date in the future
+ nextDate = this.startDate;
+ while (nextDate < System.currentTimeMillis())
+ {
+ // If we have no more occurences its an error
+ if (calcNextDate() == false)
+ throw new IllegalArgumentException("Requested notification(s) " +
+ "in the past.");
+ }
}
// Public --------------------------------------------------------
@@ -626,10 +609,7 @@
}
// Calculate the next occurence
- if (fixedRate)
nextDate += period;
- else // fixed delay
- nextDate = System.currentTimeMillis() + period;
return true;
}
@@ -644,12 +624,5 @@
// Send any notifications
sendNotifications(this);
}
-
- public String toString()
- {
- return " RegisteredNotification: [timer=" + objectName + ",id=" + id + ",startDate=" + new Date(startDate) +
- ",period=" + period + ",occurences=" + occurences + ",fixedRate=" + fixedRate +
- ",nextDate=" + new Date(nextDate) + "]";
- }
}
}
More information about the jboss-cvs-commits
mailing list