- java.lang.Object
-
- javax.management.NotificationBroadcasterSupport
-
- javax.management.timer.Timer
-
public class Timer extends NotificationBroadcasterSupport implements TimerMBean, MBeanRegistration
提供计时器MBean的实现。 计时器MBean在指定时间发出警报,该警报唤醒所有已注册的侦听器以接收计时器通知。此类管理已过时的计时器通知列表。 一种方法允许用户根据需要添加/删除任意数量的通知。 当计时器发出计时器通知并且过时时,它会自动从计时器通知列表中删除。
可以将附加计时器通知添加到定期重复通知中。注意:
- 发送定时器通知时,定时器会更新通知序列号,而不管通知类型如何。
- 计时器服务依赖于加载
Timer类的主机的系统日期。 如果主持人的系统日期不同,则听众可能会收到不合时宜的通知。 要避免此类问题,请同步所需主机的所有主机的系统日期。 - 定期通知的默认行为是固定延迟执行 ,如
Timer中所述 。 要使用固定速率执行 ,请使用重载的addNotification(String, String, Object, Date, long, long, boolean)方法。 - 通知侦听器可能都在同一个线程中执行。 因此,他们应该迅速执行,以避免阻止其他听众或扰乱固定延迟执行的规律性。 见
NotificationBroadcasterSupport。
- 从以下版本开始:
- 1.5
-
-
字段汇总
字段 变量和类型 字段 描述 static longONE_DAY一天内的毫秒数。static longONE_HOUR一小时内的毫秒数。static longONE_MINUTE一分钟内的毫秒数。static longONE_SECOND一秒钟内的毫秒数。static longONE_WEEK一周内的毫秒数。
-
构造方法摘要
构造方法 构造器 描述 Timer()默认构造函数。
-
方法摘要
所有方法 实例方法 具体的方法 变量和类型 方法 描述 IntegeraddNotification(String type, String message, Object userData, Date date)创建具有指定的新计时器通知type,message和userData并将其插入到与事件给定日期以及null时段和数量的通知列表中。IntegeraddNotification(String type, String message, Object userData, Date date, long period)创建具有指定的新计时器通知type,message和userData并将其插入到带有给定日期和时段,以及null出现数量的通知列表中。IntegeraddNotification(String type, String message, Object userData, Date date, long period, long nbOccurences)创建具有指定的新计时器通知type,message和userData并将其插入到与事件的定日期,时段和数量的通知列表中。IntegeraddNotification(String type, String message, Object userData, Date date, long period, long nbOccurences, boolean fixedRate)创建具有指定的新计时器通知type,message和userData并将其插入到与事件的定日期,时段和数量的通知列表中。Vector<Integer>getAllNotificationIDs()获取注册到通知列表中的所有计时器通知标识符。DategetDate(Integer id)获取与计时器通知关联的日期的副本。BooleangetFixedRate(Integer id)获取标志的副本,指示定期通知是以 固定延迟还是以 固定速率执行 。intgetNbNotifications()获取注册到通知列表中的计时器通知数。LonggetNbOccurences(Integer id)获取与计时器通知关联的剩余出现次数的副本。Vector<Integer>getNotificationIDs(String type)获取与指定类型对应的计时器通知的所有标识符。StringgetNotificationMessage(Integer id)获取与指定标识符对应的计时器通知详细消息。StringgetNotificationType(Integer id)获取与指定标识符对应的计时器通知类型。ObjectgetNotificationUserData(Integer id)获取与指定标识符对应的计时器通知用户数据对象。LonggetPeriod(Integer id)获取与计时器通知关联的句点(以毫秒为单位)的副本。booleangetSendPastNotifications()获取指示计时器是否发送过去通知的标志。booleanisActive()测试计时器MBean是否处于活动状态。booleanisEmpty()测试计时器通知列表是否为空。voidpostDeregister()允许计时器MBean在MBean服务器取消注册后执行所需的任何操作。voidpostRegister(Boolean registrationDone)允许计时器MBean在MBean服务器中注册后或注册失败后执行所需的任何操作。voidpreDeregister()允许计时器MBean在MBean服务器取消注册之前执行所需的任何操作。ObjectNamepreRegister(MBeanServer server, ObjectName name)允许计时器MBean在MBean服务器中注册之前执行所需的任何操作。voidremoveAllNotifications()从通知列表中删除所有计时器通知,并重置用于更新计时器通知标识符的计数器。voidremoveNotification(Integer id)从通知列表中删除与指定标识符对应的计时器通知。voidremoveNotifications(String type)从通知列表中删除与指定类型对应的所有计时器通知。voidsetSendPastNotifications(boolean value)设置指示计时器是否发送过去通知的标志。voidstart()启动计时器。voidstop()停止计时器。-
声明方法的类 javax.management.NotificationBroadcasterSupport
addNotificationListener, handleNotification, sendNotification
-
声明方法的类 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
声明方法的接口 javax.management.NotificationBroadcaster
getNotificationInfo, removeNotificationListener
-
声明方法的接口 javax.management.NotificationEmitter
removeNotificationListener
-
-
-
-
字段详细信息
-
ONE_SECOND
public static final long ONE_SECOND
一秒钟内的毫秒数。addNotification方法的有用常量。- 另请参见:
- 常数字段值
-
ONE_MINUTE
public static final long ONE_MINUTE
一分钟内的毫秒数。addNotification方法的有用常量。- 另请参见:
- 常数字段值
-
ONE_HOUR
public static final long ONE_HOUR
一小时内的毫秒数。addNotification方法的有用常量。- 另请参见:
- 常数字段值
-
ONE_DAY
public static final long ONE_DAY
一天内的毫秒数。addNotification方法的有用常量。- 另请参见:
- 常数字段值
-
ONE_WEEK
public static final long ONE_WEEK
一周内的毫秒数。addNotification方法的有用常量。- 另请参见:
- 常数字段值
-
-
方法详细信息
-
preRegister
public ObjectName preRegister(MBeanServer server, ObjectName name) throws 异常
允许计时器MBean在MBean服务器中注册之前执行所需的任何操作。在此上下文中未使用。
- Specified by:
-
preRegister在界面MBeanRegistration - 参数
-
server- 将在其中注册计时器MBean的MBean服务器。 -
name- 计时器MBean的对象名称。 - 结果
- 已注册的计时器MBean的名称。
- 异常
-
异常- 如果出现问题
-
postRegister
public void postRegister(Boolean registrationDone)
允许计时器MBean在MBean服务器中注册后或注册失败后执行所需的任何操作。在此上下文中未使用。
- Specified by:
-
postRegister接口MBeanRegistration - 参数
-
registrationDone- 指示MBean是否已在MBean服务器中成功注册。 值false表示注册阶段已失败。
-
preDeregister
public void preDeregister() throws 异常允许计时器MBean在MBean服务器取消注册之前执行所需的任何操作。停止计时器。
- Specified by:
-
preDeregister在界面MBeanRegistration - 异常
-
异常- 如果出现问题
-
postDeregister
public void postDeregister()
允许计时器MBean在MBean服务器取消注册后执行所需的任何操作。在此上下文中未使用。
- Specified by:
-
postDeregister在界面MBeanRegistration
-
start
public void start()
启动计时器。如果在通知列表中的时间之前有一个或多个计时器通知,则根据
sendPastNotifications标志发送通知,然后根据其周期和剩余发生次数进行更新。 如果计时器通知日期仍然早于当前日期,则只会从通知列表中删除此通知。- Specified by:
-
start在界面TimerMBean
-
stop
public void stop()
停止计时器。- Specified by:
-
stop接口TimerMBean
-
addNotification
public Integer addNotification(String type, String message, Object userData, Date date, long period, long nbOccurences, boolean fixedRate) throws IllegalArgumentException
创建具有指定的新计时器通知type,message和userData并将其插入到与事件的定日期,时段和数量的通知列表中。如果要插入的计时器通知的日期在当前日期之前,则该方法的行为就像指定的日期是当前日期一样。
对于一次性通知,通知会立即发送。
对于定期通知,第一个通知立即传递,后续的通知按周期参数指定。请注意,一旦将计时器通知添加到通知列表中,就无法更新其关联日期,期间和发生次数。
在定期通知的情况下,参数fixedRate的值用于指定执行方案,如
Timer中所指定。- Specified by:
-
addNotification在界面TimerMBean - 参数
-
type- 计时器通知类型。 -
message- 计时器通知详细消息。 -
userData- 计时器通知用户数据对象。 -
date- 通知发生的日期。 -
period- 计时器通知的周期(以毫秒为单位)。 -
nbOccurences- 将发出计时器通知的总数。 -
fixedRate- 如果true并且通知是定期的,则使用固定速率执行方案调度通知。 如果false并且通知是周期性的,则通过固定延迟执行方案来调度通知。 如果通知不是定期的,则忽略。 - 结果
- 新创建的计时器通知的标识符。
- 异常
-
IllegalArgumentException- 日期为null或期间或发生次数为负数。 - 另请参见:
-
addNotification(String, String, Object, Date, long, long)
-
addNotification
public Integer addNotification(String type, String message, Object userData, Date date, long period, long nbOccurences) throws IllegalArgumentException
创建具有指定的新计时器通知type,message和userData并将其插入到与事件的定日期,时段和数量的通知列表中。如果要插入的计时器通知的日期在当前日期之前,则该方法的行为就像指定的日期是当前日期一样。
对于一次性通知,通知会立即发送。
对于定期通知,第一个通知立即传递,后续的通知按周期参数指定。请注意,一旦将计时器通知添加到通知列表中,就无法更新其关联日期,期间和发生次数。
在定期通知的情况下,使用固定延迟执行方案,如
Timer中所述 。 要使用固定速率执行方案,请改用addNotification(String, String, Object, Date, long, long, boolean)。- Specified by:
-
addNotification接口TimerMBean - 参数
-
type- 计时器通知类型。 -
message- 计时器通知详细消息。 -
userData- 计时器通知用户数据对象。 -
date- 通知发生的日期。 -
period- 计时器通知的周期(以毫秒为单位)。 -
nbOccurences- 将发出计时器通知的总数。 - 结果
- 新创建的计时器通知的标识符。
- 异常
-
IllegalArgumentException- 日期为null或者出现的次数或次数为负数。 - 另请参见:
-
addNotification(String, String, Object, Date, long, long, boolean)
-
addNotification
public Integer addNotification(String type, String message, Object userData, Date date, long period) throws IllegalArgumentException
创建具有指定的新计时器通知type,message和userData并将其插入到带有给定日期和时段,以及null出现数量的通知列表中。定时器通知将使用固定延迟执行方案使用定时器周期连续重复,如
Timer中所述 。 要使用固定速率执行方案,请改用addNotification(String, String, Object, Date, long, long, boolean)。如果要插入的计时器通知的日期在当前日期之前,则该方法的行为就像指定的日期是当前日期一样。 第一个通知立即传递,后续的通知按周期参数指定。
- Specified by:
-
addNotification接口TimerMBean - 参数
-
type- 计时器通知类型。 -
message- 计时器通知详细消息。 -
userData- 计时器通知用户数据对象。 -
date- 通知发生的日期。 -
period- 计时器通知的周期(以毫秒为单位)。 - 结果
- 新创建的计时器通知的标识符。
- 异常
-
IllegalArgumentException- 日期为null或期间为负数。
-
addNotification
public Integer addNotification(String type, String message, Object userData, Date date) throws IllegalArgumentException
创建具有指定的新计时器通知type,message和userData并将其插入到与事件给定日期以及null时段和数量的通知列表中。计时器通知将在指定日期处理一次。
如果要插入的计时器通知的日期在当前日期之前,则该方法的行为就像指定的日期是当前日期并立即传递通知一样。
- Specified by:
-
addNotification在界面TimerMBean - 参数
-
type- 计时器通知类型。 -
message- 计时器通知详细消息。 -
userData- 计时器通知用户数据对象。 -
date- 通知发生的日期。 - 结果
- 新创建的计时器通知的标识符。
- 异常
-
IllegalArgumentException- 日期是null。
-
removeNotification
public void removeNotification(Integer id) throws InstanceNotFoundException
从通知列表中删除与指定标识符对应的计时器通知。- Specified by:
-
removeNotification接口TimerMBean - 参数
-
id- 计时器通知标识符。 - 异常
-
InstanceNotFoundException- 指定的标识符与此计时器MBean的通知列表中的任何计时器通知都不对应。
-
removeNotifications
public void removeNotifications(String type) throws InstanceNotFoundException
从通知列表中删除与指定类型对应的所有计时器通知。- Specified by:
-
removeNotifications在界面TimerMBean - 参数
-
type- 计时器通知类型。 - 异常
-
InstanceNotFoundException- 指定的类型与此计时器MBean的通知列表中的任何计时器通知都不对应。
-
removeAllNotifications
public void removeAllNotifications()
从通知列表中删除所有计时器通知,并重置用于更新计时器通知标识符的计数器。- Specified by:
-
removeAllNotifications在界面TimerMBean
-
getNbNotifications
public int getNbNotifications()
获取注册到通知列表中的计时器通知数。- Specified by:
-
getNbNotifications接口TimerMBean - 结果
- 计时器通知的数量。
-
getAllNotificationIDs
public Vector<Integer> getAllNotificationIDs()
获取注册到通知列表中的所有计时器通知标识符。- Specified by:
-
getAllNotificationIDs在界面TimerMBean - 结果
-
包含所有计时器通知标识符的
Integer对象的向量。
如果没有为此计时器MBean注册计时器通知,则向量为空。
-
getNotificationIDs
public Vector<Integer> getNotificationIDs(String type)
获取与指定类型对应的计时器通知的所有标识符。- Specified by:
-
getNotificationIDs接口TimerMBean - 参数
-
type- 计时器通知类型。 - 结果
-
Integer对象的向量,包含指定的type的计时器通知的所有标识符。
如果没有为具有指定的type此计时器MBean注册计时器通知,则向量为空。
-
getNotificationType
public String getNotificationType(Integer id)
获取与指定标识符对应的计时器通知类型。- Specified by:
-
getNotificationType在界面TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 计时器通知类型,如果标识符未映射到为此计时器MBean注册的任何计时器通知,则返回null。
-
getNotificationMessage
public String getNotificationMessage(Integer id)
获取与指定标识符对应的计时器通知详细消息。- Specified by:
-
getNotificationMessage接口TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 计时器通知详细消息,如果标识符未映射到为此计时器MBean注册的任何计时器通知,则返回null。
-
getNotificationUserData
public Object getNotificationUserData(Integer id)
获取与指定标识符对应的计时器通知用户数据对象。- Specified by:
-
getNotificationUserData接口TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 计时器通知用户数据对象,如果标识符未映射到为此计时器MBean注册的任何计时器通知,则为null。
-
getDate
public Date getDate(Integer id)
获取与计时器通知关联的日期的副本。- Specified by:
-
getDate接口TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 日期的副本,如果标识符未映射到为此计时器MBean注册的任何计时器通知,则返回null。
-
getPeriod
public Long getPeriod(Integer id)
获取与计时器通知关联的句点(以毫秒为单位)的副本。- Specified by:
-
getPeriod接口TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 句点的副本,如果标识符未映射到为此计时器MBean注册的任何计时器通知,则返回null。
-
getNbOccurences
public Long getNbOccurences(Integer id)
获取与计时器通知关联的剩余出现次数的副本。- Specified by:
-
getNbOccurences在界面TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 剩余出现次数的副本,如果标识符未映射到为此计时器MBean注册的任何计时器通知,则为null。
-
getFixedRate
public Boolean getFixedRate(Integer id)
获取标志的副本,指示定期通知是以 固定延迟还是以 固定速率执行 。- Specified by:
-
getFixedRate接口TimerMBean - 参数
-
id- 计时器通知标识符。 - 结果
- 标志的副本,指示定期通知是以 固定延迟还是以 固定速率执行 。
-
getSendPastNotifications
public boolean getSendPastNotifications()
获取指示计时器是否发送过去通知的标志。
过去通知发送开/关标志的默认值为false。- Specified by:
-
getSendPastNotifications接口TimerMBean - 结果
- 过去的通知发送开/关标志值。
- 另请参见:
-
setSendPastNotifications(boolean)
-
setSendPastNotifications
public void setSendPastNotifications(boolean value)
设置指示计时器是否发送过去通知的标志。
过去通知发送开/关标志的默认值为false。- Specified by:
-
setSendPastNotifications接口TimerMBean - 参数
-
value- 过去的通知发送开/关标志值。 - 另请参见:
-
getSendPastNotifications()
-
isActive
public boolean isActive()
- Specified by:
-
isActive接口TimerMBean - 结果
-
true如果计时器MBean处于活动状态,false。
-
isEmpty
public boolean isEmpty()
测试计时器通知列表是否为空。- Specified by:
-
isEmpty接口TimerMBean - 结果
-
true如果计时器通知列表为空,否则为false。
-
-