jdk/src/java.management/share/classes/javax/management/timer/Timer.java
changeset 43235 da1786d695b6
parent 29927 9cc3e111a1d8
equal deleted inserted replaced
43234:cb2a6851b837 43235:da1786d695b6
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    31 import java.util.HashMap;
    31 import java.util.HashMap;
    32 import java.util.Map;
    32 import java.util.Map;
    33 import java.util.Set;
    33 import java.util.Set;
    34 import java.util.TreeSet;
    34 import java.util.TreeSet;
    35 import java.util.Vector;
    35 import java.util.Vector;
    36 import java.util.logging.Level;
    36 import java.lang.System.Logger.Level;
    37 
    37 
    38 // jmx imports
    38 // jmx imports
    39 //
    39 //
    40 import javax.management.InstanceNotFoundException;
    40 import javax.management.InstanceNotFoundException;
    41 import javax.management.MBeanNotificationInfo;
    41 import javax.management.MBeanNotificationInfo;
   219      *
   219      *
   220      * @exception java.lang.Exception if something goes wrong
   220      * @exception java.lang.Exception if something goes wrong
   221      */
   221      */
   222     public void preDeregister() throws java.lang.Exception {
   222     public void preDeregister() throws java.lang.Exception {
   223 
   223 
   224         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   224         TIMER_LOGGER.log(Level.TRACE, "stop the timer");
   225                 "preDeregister", "stop the timer");
       
   226 
   225 
   227         // Stop the timer.
   226         // Stop the timer.
   228         //
   227         //
   229         stop();
   228         stop();
   230     }
   229     }
   271      * If the timer notification date remains earlier than the current date, this notification is just removed
   270      * If the timer notification date remains earlier than the current date, this notification is just removed
   272      * from the list of notifications.
   271      * from the list of notifications.
   273      */
   272      */
   274     public synchronized void start() {
   273     public synchronized void start() {
   275 
   274 
   276         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   275         TIMER_LOGGER.log(Level.TRACE, "starting the timer");
   277                 "start", "starting the timer");
       
   278 
   276 
   279         // Start the TimerAlarmClock.
   277         // Start the TimerAlarmClock.
   280         //
   278         //
   281         if (isActive == false) {
   279         if (isActive == false) {
   282 
   280 
   321 
   319 
   322             // Set the state to ON.
   320             // Set the state to ON.
   323             //
   321             //
   324             isActive = true;
   322             isActive = true;
   325 
   323 
   326             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   324             TIMER_LOGGER.log(Level.TRACE, "timer started");
   327                     "start", "timer started");
       
   328         } else {
   325         } else {
   329             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   326             TIMER_LOGGER.log(Level.TRACE, "the timer is already activated");
   330                     "start", "the timer is already activated");
       
   331         }
   327         }
   332     }
   328     }
   333 
   329 
   334     /**
   330     /**
   335      * Stops the timer.
   331      * Stops the timer.
   336      */
   332      */
   337     public synchronized void stop() {
   333     public synchronized void stop() {
   338 
   334 
   339         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   335         TIMER_LOGGER.log(Level.TRACE, "stopping the timer");
   340                 "stop", "stopping the timer");
       
   341 
   336 
   342         // Stop the TimerAlarmClock.
   337         // Stop the TimerAlarmClock.
   343         //
   338         //
   344         if (isActive == true) {
   339         if (isActive == true) {
   345 
   340 
   368 
   363 
   369             // Set the state to OFF.
   364             // Set the state to OFF.
   370             //
   365             //
   371             isActive = false;
   366             isActive = false;
   372 
   367 
   373             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   368             TIMER_LOGGER.log(Level.TRACE, "timer stopped");
   374                     "stop", "timer stopped");
       
   375         } else {
   369         } else {
   376             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   370             TIMER_LOGGER.log(Level.TRACE, "the timer is already deactivated");
   377                     "stop", "the timer is already deactivated");
       
   378         }
   371         }
   379     }
   372     }
   380 
   373 
   381     /**
   374     /**
   382      * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
   375      * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
   442         // Update the date if it is before the current date.
   435         // Update the date if it is before the current date.
   443         //
   436         //
   444         if (currentDate.after(date)) {
   437         if (currentDate.after(date)) {
   445 
   438 
   446             date.setTime(currentDate.getTime());
   439             date.setTime(currentDate.getTime());
   447             if (TIMER_LOGGER.isLoggable(Level.FINER)) {
   440             if (TIMER_LOGGER.isLoggable(Level.TRACE)) {
   448                 TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   441                 TIMER_LOGGER.log(Level.TRACE,
   449                         "addNotification",
       
   450                         "update timer notification to add with:" +
   442                         "update timer notification to add with:" +
   451                         "\n\tNotification date = " + date);
   443                         "\n\tNotification date = " + date);
   452             }
   444             }
   453         }
   445         }
   454 
   446 
   484         obj[TIMER_PERIOD_INDEX] = (Object) period;
   476         obj[TIMER_PERIOD_INDEX] = (Object) period;
   485         obj[TIMER_NB_OCCUR_INDEX] = (Object) nbOccurences;
   477         obj[TIMER_NB_OCCUR_INDEX] = (Object) nbOccurences;
   486         obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
   478         obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
   487         obj[FIXED_RATE_INDEX] = Boolean.valueOf(fixedRate);
   479         obj[FIXED_RATE_INDEX] = Boolean.valueOf(fixedRate);
   488 
   480 
   489         if (TIMER_LOGGER.isLoggable(Level.FINER)) {
   481         if (TIMER_LOGGER.isLoggable(Level.TRACE)) {
   490             StringBuilder strb = new StringBuilder()
   482             StringBuilder strb = new StringBuilder()
   491             .append("adding timer notification:\n\t")
   483             .append("adding timer notification:\n\t")
   492             .append("Notification source = ")
   484             .append("Notification source = ")
   493             .append(notif.getSource())
   485             .append(notif.getSource())
   494             .append("\n\tNotification type = ")
   486             .append("\n\tNotification type = ")
   501             .append(period)
   493             .append(period)
   502             .append("\n\tNotification nb of occurrences = ")
   494             .append("\n\tNotification nb of occurrences = ")
   503             .append(nbOccurences)
   495             .append(nbOccurences)
   504             .append("\n\tNotification executes at fixed rate = ")
   496             .append("\n\tNotification executes at fixed rate = ")
   505             .append(fixedRate);
   497             .append(fixedRate);
   506             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   498             TIMER_LOGGER.log(Level.TRACE, strb::toString);
   507                     "addNotification", strb.toString());
       
   508         }
   499         }
   509 
   500 
   510         timerTable.put(notifID, obj);
   501         timerTable.put(notifID, obj);
   511 
   502 
   512         // Update and start the TimerAlarmClock.
   503         // Update and start the TimerAlarmClock.
   520           {
   511           {
   521             timer.schedule(alarmClock, alarmClock.timeout);
   512             timer.schedule(alarmClock, alarmClock.timeout);
   522           }
   513           }
   523         }
   514         }
   524 
   515 
   525         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   516         TIMER_LOGGER.log(Level.TRACE, "timer notification added");
   526                 "addNotification", "timer notification added");
       
   527         return notifID;
   517         return notifID;
   528     }
   518     }
   529 
   519 
   530     /**
   520     /**
   531      * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
   521      * Creates a new timer notification with the specified <CODE>type</CODE>, <CODE>message</CODE>
   675             alarmClock.cancel();
   665             alarmClock.cancel();
   676         }
   666         }
   677 
   667 
   678         // Remove the timer notification from the timer table.
   668         // Remove the timer notification from the timer table.
   679         //
   669         //
   680         if (TIMER_LOGGER.isLoggable(Level.FINER)) {
   670         if (TIMER_LOGGER.isLoggable(Level.TRACE)) {
   681             StringBuilder strb = new StringBuilder()
   671             StringBuilder strb = new StringBuilder()
   682             .append("removing timer notification:")
   672             .append("removing timer notification:")
   683             .append("\n\tNotification source = ")
   673             .append("\n\tNotification source = ")
   684             .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getSource())
   674             .append(((TimerNotification)obj[TIMER_NOTIF_INDEX]).getSource())
   685             .append("\n\tNotification type = ")
   675             .append("\n\tNotification type = ")
   692             .append(obj[TIMER_PERIOD_INDEX])
   682             .append(obj[TIMER_PERIOD_INDEX])
   693             .append("\n\tNotification nb of occurrences = ")
   683             .append("\n\tNotification nb of occurrences = ")
   694             .append(obj[TIMER_NB_OCCUR_INDEX])
   684             .append(obj[TIMER_NB_OCCUR_INDEX])
   695             .append("\n\tNotification executes at fixed rate = ")
   685             .append("\n\tNotification executes at fixed rate = ")
   696             .append(obj[FIXED_RATE_INDEX]);
   686             .append(obj[FIXED_RATE_INDEX]);
   697             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   687             TIMER_LOGGER.log(Level.TRACE, strb::toString);
   698                     "removeNotification", strb.toString());
       
   699         }
   688         }
   700 
   689 
   701         timerTable.remove(id);
   690         timerTable.remove(id);
   702 
   691 
   703         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   692         TIMER_LOGGER.log(Level.TRACE, "timer notification removed");
   704                 "removeNotification", "timer notification removed");
       
   705     }
   693     }
   706 
   694 
   707     /**
   695     /**
   708      * Removes all the timer notifications corresponding to the specified type from the list of notifications.
   696      * Removes all the timer notifications corresponding to the specified type from the list of notifications.
   709      *
   697      *
   750 //             }
   738 //             }
   751             alarmClock.cancel();
   739             alarmClock.cancel();
   752         }
   740         }
   753 
   741 
   754         // Remove all the timer notifications from the timer table.
   742         // Remove all the timer notifications from the timer table.
   755         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   743         TIMER_LOGGER.log(Level.TRACE, "removing all timer notifications");
   756                 "removeAllNotifications", "removing all timer notifications");
       
   757 
   744 
   758         timerTable.clear();
   745         timerTable.clear();
   759 
   746 
   760         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   747         TIMER_LOGGER.log(Level.TRACE, "all timer notifications removed");
   761                 "removeAllNotifications", "all timer notifications removed");
       
   762         // Reset the counterID.
   748         // Reset the counterID.
   763         //
   749         //
   764         counterID = 0;
   750         counterID = 0;
   765 
   751 
   766         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
   752         TIMER_LOGGER.log(Level.TRACE, "timer notification counter ID reset");
   767                 "removeAllNotifications", "timer notification counter ID reset");
       
   768     }
   753     }
   769 
   754 
   770     // GETTERS AND SETTERS
   755     // GETTERS AND SETTERS
   771     //--------------------
   756     //--------------------
   772 
   757 
  1019             //  - the timer notification has not been removed from the timer table.
  1004             //  - the timer notification has not been removed from the timer table.
  1020             //
  1005             //
  1021             while ( (currentDate.after(date)) && (timerTable.containsKey(notifID)) ) {
  1006             while ( (currentDate.after(date)) && (timerTable.containsKey(notifID)) ) {
  1022 
  1007 
  1023                 if (currentFlag == true) {
  1008                 if (currentFlag == true) {
  1024                     if (TIMER_LOGGER.isLoggable(Level.FINER)) {
  1009                     if (TIMER_LOGGER.isLoggable(Level.TRACE)) {
  1025                         StringBuilder strb = new StringBuilder()
  1010                         StringBuilder strb = new StringBuilder()
  1026                         .append("sending past timer notification:")
  1011                         .append("sending past timer notification:")
  1027                         .append("\n\tNotification source = ")
  1012                         .append("\n\tNotification source = ")
  1028                         .append(notif.getSource())
  1013                         .append(notif.getSource())
  1029                         .append("\n\tNotification type = ")
  1014                         .append("\n\tNotification type = ")
  1036                         .append(obj[TIMER_PERIOD_INDEX])
  1021                         .append(obj[TIMER_PERIOD_INDEX])
  1037                         .append("\n\tNotification nb of occurrences = ")
  1022                         .append("\n\tNotification nb of occurrences = ")
  1038                         .append(obj[TIMER_NB_OCCUR_INDEX])
  1023                         .append(obj[TIMER_NB_OCCUR_INDEX])
  1039                         .append("\n\tNotification executes at fixed rate = ")
  1024                         .append("\n\tNotification executes at fixed rate = ")
  1040                         .append(obj[FIXED_RATE_INDEX]);
  1025                         .append(obj[FIXED_RATE_INDEX]);
  1041                         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
  1026                         TIMER_LOGGER.log(Level.TRACE, strb::toString);
  1042                                 "sendPastNotifications", strb.toString());
       
  1043                     }
  1027                     }
  1044                     sendNotification(date, notif);
  1028                     sendNotification(date, notif);
  1045 
  1029 
  1046                     TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
  1030                     TIMER_LOGGER.log(Level.TRACE, "past timer notification sent");
  1047                             "sendPastNotifications", "past timer notification sent");
       
  1048                 }
  1031                 }
  1049 
  1032 
  1050                 // Update the date and the number of occurrences of the timer notification.
  1033                 // Update the date and the number of occurrences of the timer notification.
  1051                 //
  1034                 //
  1052                 updateTimerTable(notif.getNotificationID());
  1035                 updateTimerTable(notif.getNotificationID());
  1105                     alarmClock = new TimerAlarmClock(this, period.longValue());
  1088                     alarmClock = new TimerAlarmClock(this, period.longValue());
  1106                     obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
  1089                     obj[ALARM_CLOCK_INDEX] = (Object)alarmClock;
  1107                     timer.schedule(alarmClock, alarmClock.timeout);
  1090                     timer.schedule(alarmClock, alarmClock.timeout);
  1108                   }
  1091                   }
  1109                 }
  1092                 }
  1110                 if (TIMER_LOGGER.isLoggable(Level.FINER)) {
  1093                 if (TIMER_LOGGER.isLoggable(Level.TRACE)) {
  1111                     TimerNotification notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];
  1094                     TimerNotification notif = (TimerNotification)obj[TIMER_NOTIF_INDEX];
  1112                     StringBuilder strb = new StringBuilder()
  1095                     StringBuilder strb = new StringBuilder()
  1113                     .append("update timer notification with:")
  1096                     .append("update timer notification with:")
  1114                     .append("\n\tNotification source = ")
  1097                     .append("\n\tNotification source = ")
  1115                     .append(notif.getSource())
  1098                     .append(notif.getSource())
  1123                     .append(period)
  1106                     .append(period)
  1124                     .append("\n\tNotification nb of occurrences = ")
  1107                     .append("\n\tNotification nb of occurrences = ")
  1125                     .append(nbOccurences)
  1108                     .append(nbOccurences)
  1126                     .append("\n\tNotification executes at fixed rate = ")
  1109                     .append("\n\tNotification executes at fixed rate = ")
  1127                     .append(fixedRate);
  1110                     .append(fixedRate);
  1128                     TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
  1111                     TIMER_LOGGER.log(Level.TRACE, strb::toString);
  1129                             "updateTimerTable", strb.toString());
       
  1130                 }
  1112                 }
  1131             }
  1113             }
  1132             else {
  1114             else {
  1133                 if (alarmClock != null) {
  1115                 if (alarmClock != null) {
  1134 //                     alarmClock.interrupt();
  1116 //                     alarmClock.interrupt();
  1209      * @param timeStamp The notification emission date.
  1191      * @param timeStamp The notification emission date.
  1210      * @param notification The timer notification to send.
  1192      * @param notification The timer notification to send.
  1211      */
  1193      */
  1212     void sendNotification(Date timeStamp, TimerNotification notification) {
  1194     void sendNotification(Date timeStamp, TimerNotification notification) {
  1213 
  1195 
  1214         if (TIMER_LOGGER.isLoggable(Level.FINER)) {
  1196         if (TIMER_LOGGER.isLoggable(Level.TRACE)) {
  1215             StringBuilder strb = new StringBuilder()
  1197             StringBuilder strb = new StringBuilder()
  1216             .append("sending timer notification:")
  1198             .append("sending timer notification:")
  1217             .append("\n\tNotification source = ")
  1199             .append("\n\tNotification source = ")
  1218             .append(notification.getSource())
  1200             .append(notification.getSource())
  1219             .append("\n\tNotification type = ")
  1201             .append("\n\tNotification type = ")
  1220             .append(notification.getType())
  1202             .append(notification.getType())
  1221             .append("\n\tNotification ID = ")
  1203             .append("\n\tNotification ID = ")
  1222             .append(notification.getNotificationID())
  1204             .append(notification.getNotificationID())
  1223             .append("\n\tNotification date = ")
  1205             .append("\n\tNotification date = ")
  1224             .append(timeStamp);
  1206             .append(timeStamp);
  1225             TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
  1207             TIMER_LOGGER.log(Level.TRACE, strb::toString);
  1226                     "sendNotification", strb.toString());
       
  1227         }
  1208         }
  1228         long curSeqNumber;
  1209         long curSeqNumber;
  1229         synchronized(this) {
  1210         synchronized(this) {
  1230             sequenceNumber = sequenceNumber + 1;
  1211             sequenceNumber = sequenceNumber + 1;
  1231             curSeqNumber = sequenceNumber;
  1212             curSeqNumber = sequenceNumber;
  1234             notification.setTimeStamp(timeStamp.getTime());
  1215             notification.setTimeStamp(timeStamp.getTime());
  1235             notification.setSequenceNumber(curSeqNumber);
  1216             notification.setSequenceNumber(curSeqNumber);
  1236             this.sendNotification((TimerNotification)notification.cloneTimerNotification());
  1217             this.sendNotification((TimerNotification)notification.cloneTimerNotification());
  1237         }
  1218         }
  1238 
  1219 
  1239         TIMER_LOGGER.logp(Level.FINER, Timer.class.getName(),
  1220         TIMER_LOGGER.log(Level.TRACE, "timer notification sent");
  1240                 "sendNotification", "timer notification sent");
       
  1241     }
  1221     }
  1242 }
  1222 }