resources.xtp
来自「RESIN 3.2 最新源码」· XTP 代码 · 共 947 行 · 第 1/2 页
XTP
947 行
<document> <header> <title>Scheduled Task</title> <type>contents</type> <description> <p>Resin's <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="<scheduled-task>"> version="Resin 3.1.6"><p><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="<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="<scheduled-task> schema">element scheduled-task { class? & cron? & delay? & init? & mbean-name? & method? & name? & period? & 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 <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"><web-app xmlns="http://caucho.com/ns/resin"> <scheduled-task class="qa.MyTask"> <cron>*/5</cron> </scheduled-task></web-app></example></s2><s2 title="task reference job configuration"><p>The task bean can also be passed to the <scheduled-task> usinga Resin-IoC EL reference. The name of the task bean would be definedpreviously, either in a <bean> or <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"><web-app xmlns="http://caucho.com/ns/resin"> <scheduled-task task="#{taskBean}"> <cron>0 0 *</cron> </scheduled-task></web-app></example></s2><s2 title="method reference job configuration"><p><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, <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"><web-app xmlns="http://caucho.com/ns/resin"> <bean name="myBean" class="qa.MyBean"/> <scheduled-task method="#{myBean.myMethod}"> <delay>10m</delay> <period>1h</period> </scheduled-task></web-app></example></s2><s2 title="url job configuration"><p>In a <web-app>, the <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 <web-app>which contains the <scheduled-task.</p><example title="Example: sunday cron url task"><web-app xmlns="http://caucho.com/ns/resin"> <scheduled-task url="/cron.php"> <cron>0 15 * * 0</cron> </scheduled-task></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"><web-app xmlns="http://caucho.com/ns/resin"> <resource type="com.caucho.resources.CronResource"> <init> <cron>*/15</cron> <work resin:type="example.PeriodicWork"> <foo>Custom Config</foo> </work> </init> </resource></web-app></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"><web-app xmlns="http://caucho.com/ns/resin"> <scheduled-task class="example.PeriodicWork"> <!-- every 5 minutes --> <cron>*/5</cron> </resource></web-app></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"><web-app xmlns="http://caucho.com/ns/resin"> <scheduled-task class="example.PeriodicWork"> <!-- every minute --> <cron>*</cron> <init> <message>Goodybye, World</message> </init> </scheduled-task></web-app></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 <message> * 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 + -
显示快捷键?