resources.xtp

来自「RESIN 3.2 最新源码」· XTP 代码 · 共 947 行 · 第 1/2 页

XTP
947
字号
<document>  <header>    <title>Scheduled Task</title>    <type>contents</type>    <description>    <p>Resin's &lt;scheduled-task> capability lets you schedule      events using a flexible cron-style trigger.  The task can be      any <code>Runnable</code> bean, a method specified by EL, or      a URL.</p>    </description>  </header>  <body><localtoc/><s1 title="&lt;scheduled-task>"> version="Resin 3.1.6"><p>&lt;scheduled-task> schedules a job to be executed at specific timesor after specific delays.  The times can be specified by a cron syntax orby a simple delay parameter.  The job can be either a <code>Runnable</code>bean, a method specified by an EL expression, or a URL.</p><p>When specified as an IoC bean, the bean task has full IoC capabilities,including injection, @TransactionAttribute aspects, interception and@Observes.</p><deftable title="&lt;scheduled-task> Attributes"><tr>  <th>Attribute</th>  <th>Description</th></tr><tr>  <td>class</td>  <td>the classname of the singleton bean to create</td></tr><tr>  <td>cron</td>  <td>a cron-style scheduling description</td></tr><tr>  <td>delay</td>  <td>a simple delay-based execution</td></tr><tr>  <td>init</td>  <td>IoC initialization for the bean</td></tr><tr>  <td>mbean-name</td>  <td>optional MBean name for JMX registration</td></tr><tr>  <td>method</td>  <td>EL expression for a method to be invoked as the task</td></tr><tr>  <td>name</td>  <td>optional IoC name for registering the task</td></tr><tr>  <td>period</td>  <td>how often the task should be invoked in simple mode</td></tr><tr>  <td>task</td>  <td>alternate task assignment for predefined beans</td></tr></deftable><def title="&lt;scheduled-task> schema">element scheduled-task {  class?  &amp; cron?  &amp; delay?  &amp; init?  &amp; mbean-name?  &amp; method?  &amp; name?  &amp; period?  &amp; task?}</def><s2 title="bean-style job configuration"><p>The most common and flexible job configuration uses standard IoCbean-style configuration.  The bean must implement <code>Runnable</code>.Like the &lt;bean> tag, the <var>class</var> attribute specifies the<code>Runnable</code> class, and any <var>init</var> section configuresthe bean using <a href="resin-ioc.xtp">Resin IoC</a> configuration.</p><example title="Example: 5min cron bean task">&lt;web-app xmlns="http://caucho.com/ns/resin">  &lt;scheduled-task class="qa.MyTask">    &lt;cron>*/5&lt;/cron>  &lt;/scheduled-task>&lt;/web-app></example></s2><s2 title="task reference job configuration"><p>The task bean can also be passed to the &lt;scheduled-task> usinga Resin-IoC EL reference.  The name of the task bean would be definedpreviously, either in a &lt;bean> or &lt;component> or picked up by classpathscanning.  Like the bean-style job configuration, the reference bean mustimplement <code>Runnable</code>.</p><example title="Example: midnight cron bean task">&lt;web-app xmlns="http://caucho.com/ns/resin">  &lt;scheduled-task task="#{taskBean}">    &lt;cron>0 0 *&lt;/cron>  &lt;/scheduled-task>&lt;/web-app></example></s2><s2 title="method reference job configuration"><p>&lt;scheduled-task> can execute a method on a defined bean as thescheduler's task.  The method is specified using EL reference syntax.At each trigger time, &lt;scheduled-task> will invoke the EL methodexpression.</p><p>In the following example, the task invokes <code>myMethod()</code>on the <var>myBean</var> singleton every 1 hour.</p><example title="Example: 1h period method task">&lt;web-app xmlns="http://caucho.com/ns/resin">  &lt;bean name="myBean" class="qa.MyBean"/>  &lt;scheduled-task method="#{myBean.myMethod}">    &lt;delay>10m&lt;/delay>    &lt;period>1h&lt;/period>  &lt;/scheduled-task>&lt;/web-app></example></s2><s2 title="url job configuration"><p>In a &lt;web-app>, the &lt;scheduled-task> can invoke a servlet URLat the trigger times.  The task uses the servlet <code>RequestDispatcher</code>and forwards to the specified URL.  The URL is relative to the &lt;web-app>which contains the &lt;scheduled-task.</p><example title="Example: sunday cron url task">&lt;web-app xmlns="http://caucho.com/ns/resin">  &lt;scheduled-task url="/cron.php">    &lt;cron>0 15 * * 0&lt;/cron>  &lt;/scheduled-task>&lt;/web-app></example></s2><s2 title="cron trigger syntax"><p>Some ascii art from the <a href="http://en.wikipedia.org/wiki/Crontab">wikipediacron entry</a></p><def title="cron fields"># +---------------- minute (0 - 59)# |  +------------- hour (0 - 23)# |  |  +---------- day of month (1 - 31)# |  |  |  +------- month (1 - 12)# |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)# |  |  |  |  |  *  *  *  *  *</def><deftable title="cron patterns"><tr>  <th>Pattern</th>  <th>Description</th></tr><tr>  <td>*</td>  <td>matches all time periods</td></tr><tr>  <td>15</td>  <td>matches the specific time, e.g. 15 for minutes</td></tr><tr>  <td>15,45</td>  <td>matches a list of times, e.g. every :15 and :45</td></tr><tr>  <td>*/5</td>  <td>matches every <var>n</var> times, e.g. every 5 minutes</td></tr><tr>  <td>1-5</td>  <td>matches a range of times, e.g. mon, tue, wed, thu, fri (1-5)</td></tr></deftable><p>Each field specifies a range of times to be executed.  The patternsallowed are:</p><deftable title="example ranges"><tr>  <th>range</th>  <th>explanation (using minutes as example)</th></tr><tr>  <td>*</td>  <td>run every minute</td></tr><tr>  <td>*/5</td>  <td>run every 5 minutes</td></tr><tr>  <td>0,5,50</td>  <td>run at :00, :05, :50 every hour</td></tr>  <tr><td>0-4</td>  <td>run at :00, :01, :02, :03, :04</td></tr><tr>  <td>0-30/2</td>  <td>run every 2 minutes for the first half hour</td></tr></deftable><p>The minutes field is always required, and the hours, days, andmonths fields are optional.</p><deftable title="example times"><tr>  <th>range</th>  <th>explanation</th></tr><tr>  <td>0 */3</td>  <td>run every 3 hours</td></tr><tr>  <td>15 2 *</td>  <td>run every day at 0215 local time</td></tr><tr>  <td>0 0 */3</td>  <td>run every third day at midnight</td></tr><tr>  <td>15 0 * * 6</td>  <td>run every Saturday at 0015</td></tr></deftable></s2><s2 title="com.caucho.resources.CronResource"><p>Often, applications need to run a task at specific times.  The<code>CronResource</code> provides a standard way of doing that.Applications need only create a standard<code>java.lang.Runnable</code> task and configure the CronResource.Resin configure's the work task with <a href="ioc-bean-patterns.xtp">bean-style configuration</a>.</p><example title="Task running every 15 minutes">&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;  &lt;resource type="com.caucho.resources.CronResource"&gt;    &lt;init&gt;      &lt;cron&gt;*/15&lt;/cron&gt;      &lt;work resin:type="example.PeriodicWork"&gt;        &lt;foo&gt;Custom Config&lt;/foo&gt;      &lt;/work&gt;    &lt;/init&gt;  &lt;/resource&gt;&lt;/web-app&gt;</example><deftable-parameters><tr><td>cron</td><td>Specifies the times the task should be run</td><td>required</td></tr><tr><td>work</td><td>Specifies application's work bean</td><td>required</td></tr></deftable-parameters><p>The cron specification follows the Unix crontab format.  The cronis composed of 5 fields: minutes, hours, day of month, month, and dayof week.</p><p>Each field specifies a range of times to be executed.  The patternsallowed are:</p><deftable title="example ranges"><tr><th>range</th><th>explanation (using minutes as example)</th></tr><tr><td>*</td><td>run every minute</td></tr><tr><td>*/5</td><td>run every 5 minutes</td></tr><tr><td>0,5,50</td><td>run at :00, :05, :50 every hour</td></tr><tr><td>0-4</td><td>run at :00, :01, :02, :03, :04</td></tr><tr><td>0-30/2</td><td>run every 2 minutes for the first half hour</td></tr></deftable><p>The minutes field is always required, and the hours, days, andmonths fields are optional.</p><deftable title="example times"><tr><th>range</th><th>explanation</th></tr><tr><td>0 */3</td><td>run every 3 hours</td></tr><tr><td>15 2 *</td><td>run every day at 0215 local time</td></tr><tr><td>0 0 */3</td><td>run every third day at midnight</td></tr><tr><td>15 0 * * 6</td><td>run every Saturday at 0015</td></tr></deftable></s2><s2 title="Hello, World example"><example title="Example: WEB-INF/web.xml">&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;  &lt;scheduled-task class="example.PeriodicWork"&gt;    &lt;!-- every 5 minutes --&gt;    &lt;cron&gt;*/5&lt;/cron&gt;  &lt;/resource&gt;&lt;/web-app&gt;</example><example title="Example: WEB-INF/classes/example/PeriodicWork.java">package example;import java.util.logging.Level;import java.util.logging.Logger;public class PeriodicWork implements Runnable {  static protected final Logger log =     Logger.getLogger(PeriodicWork.class.getName());  public PeriodicWork()  {    log.info("PeriodicWork: constructor");  }  /**   * Required implementation of java.lang.Runnable.run()   */  public void run()  {    log.info("PeriodicWork: run() Hello, World");  }}</example><results>[13:04:27.429] PeriodicWork: constructor[13:05:00.095] PeriodicWork: run() Hello, World[13:10:00.182] PeriodicWork: run() Hello, World</results></s2><s2 title="Example: bean-style configuration example"><example title="WEB-INF/web.xml">&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;  &lt;scheduled-task class="example.PeriodicWork"&gt;    &lt;!-- every minute --&gt;    &lt;cron&gt;*&lt;/cron&gt;    &lt;init&gt;      &lt;message&gt;Goodybye, World&lt;/message&gt;    &lt;/init&gt;  &lt;/scheduled-task&gt;&lt;/web-app&gt;</example><example title="Example: WEB-INF/classes/example/PeriodicWork.java">package example;import java.util.logging.Level;import java.util.logging.Logger;import javax.resource.spi.work.Work;public class PeriodicWork implements Work {  static protected final Logger log =     Logger.getLogger(PeriodicWork.class.getName());  String _message;  public PeriodicWork()  {    log.info("PeriodicWork: constructor");  }  /**   * Optional, called in response to presence of &lt;message&gt;   * configuration tag.    */  public void setMessage(String message)  {    log.info("PeriodicWork: setMessage");    _message = message;  }  /**   * Optional, called after bean is created and any setters   * from configuration are called.   */  @PostConstruct  public void init()    throws Exception  {    log.info("PeriodicWork: init()");    if (_message == null)      throw new Exception("`message' is required");  }  /**   * Required implementation of java.lang.Runnable.run()   */  public void run()  {    log.info("PeriodicWork: run() " + _message);  }  /**   * Implementation of javax.resource.spi.work.Work.release()   */  public void release()  {    log.info("PeriodicWork: release()");  }}</example><results>[13:04:27.429] PeriodicWork: constructor[13:04:27.429] PeriodicWork: setMessage[13:04:27.429] PeriodicWork: init()[13:05:00.095] PeriodicWork: run() Goodbye, World[13:06:00.182] PeriodicWork: run() Goodbye, World(close Resin)[13:06:00.345] PeriodicWork: release()</results></s2></s1> <!-- scheduled-task --><s1 name="concepts" title="RMI Resource"><p>The goal of RMI is to provide <var>services</var> to remote clients.  A remoteclient obtains and uses a <var>proxy object</var> that implements an<var>interface</var>.  The interface is the contract for the service, it is thedefinition of the methods that the service provides.</p><p>Because the client is using a proxy object, the actual execution of codeoccurs on the server.  A proxy object is placeholder that the client usesto cause execution of code on a server.</p><s2 name="concepts/registry" title="Registry">

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?