JavaTM Platform
Standard Ed. 6

javax.management.timer
类 Timer

java.lang.Object
  继承者 javax.management.NotificationBroadcasterSupport
      继承者 javax.management.timer.Timer
所有已实现的接口:
MBeanRegistration, NotificationBroadcaster, NotificationEmitter, TimerMBean

public class Timer
     
extends NotificationBroadcasterSupport
implements TimerMBean, MBeanRegistration

提供对计时器 MBean 的实现。计时器 MBean 将在指定的时间发出警告,以唤醒所有注册的侦听器来接收计时器通知。

此类管理一个过期的计时器通知列表。这是一种允许用户根据需要任意添加/移除通知的方法。当计时器发出计时器通知并过时后,会将该通知自动从计时器通知列表中移除。
可以将其他计时器通知添加到常规重复发送的通知中。

注:

  1. 当发送计时器通知时,计时器将更新通知序列号,而不考虑通知类型。
  2. 计时器服务依赖于加载 Timer 类的主机的系统日期。如果侦听器主机具有不同的系统日期,则侦听器可能会不合时宜地接收到通知。为了避免此类问题,要同步所有需要计时的主机的系统日期。
  3. 定期通知的默认行为是固定延迟执行,这一点已在 Timer 中指定。为了使用固定速率执行,要使用重载的 addNotification(String, String, Object, Date, long, long, boolean) 方法。
  4. 可能会在同一线程中执行所有这些通知侦听器。因此,必须快速执行以避免阻止其他侦听器或者打乱固定延迟执行的规律。请参见 NotificationBroadcasterSupport

从以下版本开始:
1.5

字段摘要
static long ONE_DAY
          一天内的毫秒数。
static long ONE_HOUR
          一小时内的毫秒数。
static long ONE_MINUTE
          一分钟内的毫秒数。
static long ONE_SECOND
          一秒钟内的毫秒数。
static long ONE_WEEK
          一周内的毫秒数。
 
构造方法摘要
Timer()
          默认的构造方法。
 
方法摘要
 Integer addNotification(String type, String message, Object userData, Date date)
          创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期以及 null 时段和出现数量的通知列表中。
 Integer addNotification(String type, String message, Object userData, Date date, long period)
          创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期和时段,以及 null 出现数量的通知列表中。
 Integer addNotification(String type, String message, Object userData, Date date, long period, long nbOccurences)
          创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期、时段和出现数量的通知列表中。
 Integer addNotification(String type, String message, Object userData, Date date, long period, long nbOccurences, boolean fixedRate)
          创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期、时段和出现数量的通知列表中。
 Vector<Integer> getAllNotificationIDs()
          获得所有在通知列表中注册的计时器通知标识符。
 Date getDate(Integer id)
          获得与计时器通知关联的日期的一个副本。
 Boolean getFixedRate(Integer id)
          获得此种标志的一个副本:该标志指示是以固定延迟 方式执行定期通知,还是以固定速率 方式执行。
 int getNbNotifications()
          获得在通知列表中注册的计时器通知数。
 Long getNbOccurences(Integer id)
          获得与计时器通知关联的剩余出现数量的一个副本。
 Vector<Integer> getNotificationIDs(String type)
          获得所有与指定类型对应的计时器通知的标识符。
 MBeanNotificationInfo[] getNotificationInfo()
          返回一个数组,指示此 MBean 可能发送的每个通知的 Java 类名和通知类型。
 String getNotificationMessage(Integer id)
          获得与指定标识符对应的计时器通知的详细消息。
 String getNotificationType(Integer id)
          获得与指定标识符对应的计时器通知类型。
 Object getNotificationUserData(Integer id)
          获得与指定标识符对应的计时器通知用户数据对象。
 Long getPeriod(Integer id)
          获得与计时器通知关联的时段(以毫秒为单位)的一个副本。
 boolean getSendPastNotifications()
          获得一个指示计时器是否发送过期通知的标志。
 boolean isActive()
          测试计时器 MBean 是否处于激活状态。
 boolean isEmpty()
          测试计时器通知列表是否为空。
 void postDeregister()
          允许计时器 MBean 在 MBean 服务器注销它之后执行任何所需的操作。
 void postRegister(Boolean registrationDone)
          允许计时器 MBean 在 MBean 服务器中注册后或注册失败后执行任何所需的操作。
 void preDeregister()
          允许计时器 MBean 在 MBean 服务器注销它之前执行任何所需的操作。
 ObjectName preRegister(MBeanServer server, ObjectName name)
          允许计时器 MBean 在 MBean 服务器中注册之前执行任何所需的操作。
 void removeAllNotifications()
          从通知列表中删除所有计时器通知并重置用来更新计时器通知标识符的计数器。
 void removeNotification(Integer id)
          从通知列表中移除与指定标识符对应的计时器通知。
 void removeNotifications(String type)
          从通知列表中移除与指定类型对应的所有计时器通知。
 void setSendPastNotifications(boolean value)
          设置指示计时器是否发送过期通知的标志。
 void start()
          启动计时器。
 void stop()
          停止计时器。
 
从类 javax.management.NotificationBroadcasterSupport 继承的方法
addNotificationListener, handleNotification, removeNotificationListener, removeNotificationListener, sendNotification
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

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 方法的有用常量。

另请参见:
常量字段值
构造方法详细信息

Timer

public Timer()
默认的构造方法。

方法详细信息

preRegister

public ObjectName preRegister(MBeanServer server,
                              ObjectName name)
                       throws Exception
允许计时器 MBean 在 MBean 服务器中注册之前执行任何所需的操作。

未在此上下文中使用。

指定者:
接口 MBeanRegistration 中的 preRegister
参数:
server - 将在其中注册计时器 MBean 的 MBean 服务器。
name - 计时器 MBean 的对象名。
返回:
已注册的计时器 MBean 的名称。
抛出:
Exception

postRegister

public void postRegister(Boolean registrationDone)
允许计时器 MBean 在 MBean 服务器中注册后或注册失败后执行任何所需的操作。

未在此上下文中使用。

指定者:
接口 MBeanRegistration 中的 postRegister
参数:
registrationDone - 指示该 MBean 是否已在 MBean 服务器中成功注册。false 值意味着注册阶段已失败。

preDeregister

public void preDeregister()
                   throws Exception
允许计时器 MBean 在 MBean 服务器注销它之前执行任何所需的操作。

停止计时器。

指定者:
接口 MBeanRegistration 中的 preDeregister
抛出:
Exception

postDeregister

public void postDeregister()
允许计时器 MBean 在 MBean 服务器注销它之后执行任何所需的操作。

未在此上下文中使用。

指定者:
接口 MBeanRegistration 中的 postDeregister

getNotificationInfo

public MBeanNotificationInfo[] getNotificationInfo()
从接口 NotificationBroadcaster 复制的描述

返回一个数组,指示此 MBean 可能发送的每个通知的 Java 类名和通知类型。

MBean 发送此数组中未描述的通知是合法的。但是,某些 MBean 服务器的客户端要想正常运行,可能要依赖完整的数组。

指定者:
接口 NotificationBroadcaster 中的 getNotificationInfo
覆盖:
NotificationBroadcasterSupport 中的 getNotificationInfo
返回:
可能的通知数组。

start

public void start()
启动计时器。

如果有一个或多个计时器通知早于通知列表中的时间,则将根据 sendPastNotifications 标志发送通知,然后根据其周期和剩余出现数量更新通知。如果计时器通知日期早于当前日期,则仅从通知列表中移除此通知。

指定者:
接口 TimerMBean 中的 start

stop

public void stop()
停止计时器。

指定者:
接口 TimerMBean 中的 stop

addNotification

public Integer addNotification(String type,
                               String message,
                               Object userData,
                               Date date,
                               long period,
                               long nbOccurences,
                               boolean fixedRate)
                        throws IllegalArgumentException
创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期、时段和出现数量的通知列表中。

如果要插入的计时器通知其日期早于当前的日期,则该方法具有把指定的日期当做当前日期的行为。
对于“一次性”通知,将立即发出该通知。
对于定期通知,将立即发出第一个通知,而后续通知将根据 period 参数指定的间隔提供。

注意,一旦将计时器通知添加到通知列表中,将不能更新其相关的日期、时段和出现数量。

如果是定期通知,则参数 fixedRate 的值将用来指定执行方案,如在 Timer 中指定的那样。

指定者:
接口 TimerMBean 中的 addNotification
参数:
type - 计时器通知类型。
message - 计时器通知详细消息。
userData - 计时器通知用户数据对象。
date - 发出通知时的日期。
period - 计时器通知的时段(以毫秒为单位)。
nbOccurences - 将发出的计时器通知的总数。
fixedRate - 如果为 true 并且通知是定期的,则使用 固定速率 执行方案安排该通知。如果为 false 并且通知是定期的,则使用 固定延迟 执行方案安排通知。如果通知不是定期的,则将被忽略。
返回:
新创建的计时器通知的标识符。
抛出:
IllegalArgumentException - 如果 date 为 null 或者 period 或出现的数量为负数。
另请参见:
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
创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期、时段和出现数量的通知列表中。

如果要插入的计时器通知其日期早于当前的日期,则该方法具有把指定的日期当做当前日期的行为。
对于“一次性”通知,将立即发出该通知。
对于定期通知,将立即发出第一个通知,而后续通知将根据 period 参数指定的间隔提供。

注意,一旦将计时器通知添加到通知列表中,将不能更新其相关的日期、时段和出现数量。

如果是定期通知,则使用固定延迟 执行方案,如在 Timer 中指定的那样。为了使用固定速率 执行方案,要使用 addNotification(String, String, Object, Date, long, long, boolean)

指定者:
接口 TimerMBean 中的 addNotification
参数:
type - 计时器通知类型。
message - 计时器通知详细消息。
userData - 计时器通知用户数据对象。
date - 发出通知时的日期。
period - 计时器通知的时段(以毫秒为单位)。
nbOccurences - 将发出的计时器通知的总数。
返回:
新创建的计时器通知的标识符。
抛出:
IllegalArgumentException - 如果 date 为 null 或者 period 或出现的数量为负数。
另请参见:
addNotification(String, String, Object, Date, long, long, boolean)

addNotification

public Integer addNotification(String type,
                               String message,
                               Object userData,
                               Date date,
                               long period)
                        throws IllegalArgumentException
创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期和时段,以及 null 出现数量的通知列表中。

通过使用固定延迟 执行方案,计时器通知将持续重复使用计时器时段,如在 Timer 中指定的那样。为了使用固定速率 执行方案,要使用 addNotification(String, String, Object, Date, long, long, boolean)

如果要插入的计时器通知其日期早于当前的日期,则该方法具有把指定的日期当做当前日期的行为。将立即发出第一个通知,而后续通知将根据 period 参数指定的间隔提供。

指定者:
接口 TimerMBean 中的 addNotification
参数:
type - 计时器通知类型。
message - 计时器通知详细消息。
userData - 计时器通知用户数据对象。
date - 发出通知时的日期。
period - 计时器通知的时段(以毫秒为单位)。
返回:
新创建的计时器通知的标识符。
抛出:
IllegalArgumentException - 如果 date 为 null 或者 period 或出现的数量为负数。

addNotification

public Integer addNotification(String type,
                               String message,
                               Object userData,
                               Date date)
                        throws IllegalArgumentException
创建具有指定 typemessageuserData 的新计时器通知,并将其插入到带有给定日期以及 null 时段和出现数量的通知列表中。

计时器通知将在指定的日期一次性处理。

如果要插入的计时器通知其日期早于当前的日期,则该方法具有把指定的日期当做当前日期的行为,并且将立即发出通知。

指定者:
接口 TimerMBean 中的 addNotification
参数:
type - 计时器通知类型。
message - 计时器通知详细消息。
userData - 计时器通知用户数据对象。
date - 发出通知时的日期。
返回:
新创建的计时器通知的标识符。
抛出:
IllegalArgumentException - 如果 date 为 null

removeNotification

public void removeNotification(Integer id)
                        throws InstanceNotFoundException
从通知列表中移除与指定标识符对应的计时器通知。

指定者:
接口 TimerMBean 中的 removeNotification
参数:
id - 计时器通知标识符。
抛出:
InstanceNotFoundException - 如果指定的标识符不能与此计时器 MBean 通知列表中的任何计时器通知对应。

removeNotifications

public void removeNotifications(String type)
                         throws InstanceNotFoundException
从通知列表中移除与指定类型对应的所有计时器通知。

指定者:
接口 TimerMBean 中的 removeNotifications
参数:
type - 计时器通知类型。
抛出:
InstanceNotFoundException - 如果指定类型不能与此计时器 MBean 通知列表中的任何计时器通知对应。

removeAllNotifications

public void removeAllNotifications()
从通知列表中删除所有计时器通知并重置用来更新计时器通知标识符的计数器。

指定者:
接口 TimerMBean 中的 removeAllNotifications

getNbNotifications

public int getNbNotifications()
获得在通知列表中注册的计时器通知数。

指定者:
接口 TimerMBean 中的 getNbNotifications
返回:
计时器通知数。

getAllNotificationIDs

public Vector<Integer> getAllNotificationIDs()
获得所有在通知列表中注册的计时器通知标识符。

指定者:
接口 TimerMBean 中的 getAllNotificationIDs
返回:
一个包含所有计时器通知标识符的 Integer 对象的向量。
如果不存在向此计时器 MBean 注册的计时器通知,则该向量为空。

getNotificationIDs

public Vector<Integer> getNotificationIDs(String type)
获得所有与指定类型对应的计时器通知的标识符。

指定者:
接口 TimerMBean 中的 getNotificationIDs
参数:
type - 计时器通知类型。
返回:
一个 Integer 对象的向量,该对象包含所有带指定 type 的计时器通知的标识符。
如果不存在向此带指定 type 的计时器 MBean 注册的计时器通知,则该向量为空。

getNotificationType

public String getNotificationType(Integer id)
获得与指定标识符对应的计时器通知类型。

指定者:
接口 TimerMBean 中的 getNotificationType
参数:
id - 计时器通知标识符。
返回:
计时器通知类型,如果标识符不与向此计时器 MBean 注册的任何计时器通知对应,则返回 null。

getNotificationMessage

public String getNotificationMessage(Integer id)
获得与指定标识符对应的计时器通知的详细消息。

指定者:
接口 TimerMBean 中的 getNotificationMessage
参数:
id - 计时器通知标识符。
返回:
计时器通知详细消息,如果标识符不与向此计时器 MBean 注册的任何计时器通知对应,则返回 null。

getNotificationUserData

public Object getNotificationUserData(Integer id)
获得与指定标识符对应的计时器通知用户数据对象。

指定者:
接口 TimerMBean 中的 getNotificationUserData
参数:
id - 计时器通知标识符。
返回:
计时器通知用户数据对象,如果标识符不与向此计时器 MBean 注册的任何计时器通知对应,则返回 null。

getDate

public Date getDate(Integer id)
获得与计时器通知关联的日期的一个副本。

指定者:
接口 TimerMBean 中的 getDate
参数:
id - 计时器通知标识符。
返回:
日期的一个副本,如果标识符不与向此计时器 MBean 注册的任何计时器通知对应,则返回 null。

getPeriod

public Long getPeriod(Integer id)
获得与计时器通知关联的时段(以毫秒为单位)的一个副本。

指定者:
接口 TimerMBean 中的 getPeriod
参数:
id - 计时器通知标识符。
返回:
时段的一个副本,如果标识符不与向此计时器 MBean 注册的任何计时器通知对应,则返回 null。

getNbOccurences

public Long getNbOccurences(Integer id)
获得与计时器通知关联的剩余出现数量的一个副本。

指定者:
接口 TimerMBean 中的 getNbOccurences
参数:
id - 计时器通知标识符。
返回:
剩余出现数量的一个副本,如果标识符不与向此计时器 MBean 注册的任何计时器通知对应,则返回 null。

getFixedRate

public Boolean getFixedRate(Integer id)
获得此种标志的一个副本:该标志指示是以 固定延迟 方式执行定期通知,还是以 固定速率 方式执行。

指定者:
接口 TimerMBean 中的 getFixedRate
参数:
id - 计时器通知标识符。
返回:
指示是以 固定延迟 方式执行定期通知,还是以 固定速率 方式执行的标志的一个副本。

getSendPastNotifications

public boolean getSendPastNotifications()
获得一个指示计时器是否发送过期通知的标志。
过期通知发送开/关标志的默认值为 false

指定者:
接口 TimerMBean 中的 getSendPastNotifications
返回:
过期通知发送开/关标志值。
另请参见:
setSendPastNotifications(boolean)

setSendPastNotifications

public void setSendPastNotifications(boolean value)
设置指示计时器是否发送过期通知的标志。
过期通知发送开/关标志的默认值为 false

指定者:
接口 TimerMBean 中的 setSendPastNotifications
参数:
value - 过期通知发送开/关标志值。
另请参见:
getSendPastNotifications()

isActive

public boolean isActive()
测试计时器 MBean 是否处于激活状态。当调用 start 方法时,计时器 MBean 被标记为激活。当调用 stop 方法时,该标记变为非激活。
激活开/关标志的默认值为 false

指定者:
接口 TimerMBean 中的 isActive
返回:
如果计时器 MBean 处于激活状态,则返回 true;否则返回 false

isEmpty

public boolean isEmpty()
测试计时器通知列表是否为空。

指定者:
接口 TimerMBean 中的 isEmpty
返回:
如果计时器通知列表为空,则返回 true,否则返回 false

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策