- java.lang.Object
-
- javax.management.NotificationBroadcasterSupport
-
- javax.management.timer.Timer
-
- All Implemented Interfaces:
-
MBeanRegistration,NotificationBroadcaster,NotificationEmitter,TimerMBean
public class Timer extends NotificationBroadcasterSupport implements TimerMBean, MBeanRegistration
提供定时器MBean的实现。 定时器MBean在指定的时间发出警报,唤醒所有注册的收听者接收定时器通知。该类管理日期计时器通知的列表。 一种方法允许用户根据需要添加/删除尽可能多的通知。 当定时器发出定时器通知并变得过时时,它将自动从定时器通知列表中移除。
额外的定时器通知可以添加到定期重复的通知中。注意:
- 当发送定时器通知时,定时器更新通知序列号,而不管通知类型如何。
- 计时器服务依赖于装载
Timer类的主机的系统日期。 如果主机具有不同的系统日期,则侦听器可能会收到不及时的通知。 为了避免这种问题,请同步需要时间的所有主机的系统日期。 - 定期通知的默认行为是固定延迟执行 ,如
Timer中所述 。 为了使用固定速率执行 ,请使用重载的addNotification(String, String, Object, Date, long, long, boolean)方法。 - 通知侦听器可能全部在同一个线程中执行。 因此,应该迅速执行,避免阻止其他听众或扰乱固定延期执行的规律性。 见
NotificationBroadcasterSupport。
- 从以下版本开始:
- 1.5
-
-
Field Summary
Fields Modifier and Type Field 描述 static longONE_DAY一天中的毫秒数。static longONE_HOUR一小时内的毫秒数。static longONE_MINUTE一分钟内的毫秒数。static longONE_SECOND一秒钟的毫秒数。static longONE_WEEK一周内的毫秒数。
-
构造方法摘要
构造方法 Constructor 描述 Timer()默认构造函数。
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 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)获取与指定类型相对应的定时器通知的所有标识符。MBeanNotificationInfo[]getNotificationInfo()返回一个数组,指示MBean可能发送的每个通知,通知的Java类的名称和通知类型。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()停止定时器-
Methods inherited from class javax.management.NotificationBroadcasterSupport
addNotificationListener, handleNotification, removeNotificationListener, removeNotificationListener, sendNotification
-
-
-
-
字段详细信息
-
ONE_SECOND
public static final long ONE_SECOND
一秒钟的毫秒数。 有用的常数为addNotification方法。- 另请参见:
- Constant Field Values
-
ONE_MINUTE
public static final long ONE_MINUTE
一分钟内的毫秒数。 有用的常数为addNotification方法。- 另请参见:
- Constant Field Values
-
ONE_HOUR
public static final long ONE_HOUR
一小时内的毫秒数。 有用的常数为addNotification方法。- 另请参见:
- Constant Field Values
-
ONE_DAY
public static final long ONE_DAY
一天中的毫秒数。 有用的常数为addNotification方法。- 另请参见:
- Constant Field Values
-
ONE_WEEK
public static final long ONE_WEEK
一周内的毫秒数。 有用的常数为addNotification方法。- 另请参见:
- Constant Field Values
-
-
方法详细信息
-
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
-
getNotificationInfo
public MBeanNotificationInfo[] getNotificationInfo()
描述从接口NotificationBroadcaster复制返回一个数组,指示MBean可能发送的每个通知,通知的Java类的名称和通知类型。
MBean发送不在此阵列中描述的通知并不是非法的。 但是,MBean服务器的某些客户端可能依赖于正确运行的阵列。
- Specified by:
-
getNotificationInfo在接口NotificationBroadcaster - 重写:
-
getNotificationInfo在NotificationBroadcasterSupport - 结果
- 可能通知的数组。
-
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并将其插入到与事件的定日期,时段和数量的通知列表中。如果要插入的计时器通知的日期在当前日期之前,该方法的行为就像指定的日期是当前日期一样。
对于一次性通知,通知将立即发送。
对于定期通知,第一个通知立即发送,后续的通知按照period参数指定。请注意,一旦定时器通知已添加到通知列表中,则无法更新其关联的日期,周期和出现次数。
在定期通知的情况下,参数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并将其插入到与事件的定日期,时段和数量的通知列表中。如果要插入的计时器通知的日期在当前日期之前,该方法的行为就像指定的日期是当前日期一样。
对于一次性通知,通知将立即发送。
对于定期通知,第一个通知立即发送,后续的通知按照period参数指定。请注意,一旦定时器通知已添加到通知列表中,则无法更新其关联的日期,周期和出现次数。
在定期通知的情况下,使用
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)。如果要插入的计时器通知的日期在当前日期之前,该方法的行为就像指定的日期是当前日期一样。 第一个通知立即发送,随后的通知按照period参数指定。
- 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的定时器通知的所有标识符。
如果没有为此定时器MBean注册定时器通知指定的type,向量为空。
-
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否则。
-
-