⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.xtp

📁 RESIN 3.2 最新源码
💻 XTP
📖 第 1 页 / 共 2 页
字号:
<document><header><product>resin</product><title>PeriodicTask Tutorial</title><type>tutorial</type><tutorial-startpage>index.jsp</tutorial-startpage><description><p>This tutorial demonstrates the creation of a PeriodicTask that performs a taskat intervals and collects statistics on it's performance.  An administrationinterface to the task is provided, and while the task is active the user isshown a "temporarily unavailable" page.  </p><p>The code for this tutorial provides a full featured example that can be usedas a cut-and-paste source for solving real problems. The PeriodicTask keepsdetailed statistics, an administration interface is provided with a servlet,and the task can be executed either manually or automatically at timedintervals.</p></description></header><body><localtoc/><s1 title="Files in this tutorial"><deftable>  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTask.java"/>    </td><td>The implementation of the class that performs the task</td></tr>  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTaskMBean.java"/>    </td><td>The Jmx MBean interface to the class that performs the task</td>  </tr>  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTaskServlet.java"/>    </td><td>An administrators view of the PeriodicTask.    </td>  </tr>  <tr><td><viewfile-link file="WEB-INF/classes/example/PeriodicTaskFilter.java"/>    </td><td>A filter that responds with a "temporarily unavailable" page when the        task is running.</td>  </tr>  <tr><td><viewfile-link file="WEB-INF/web.xml"/>    </td><td>web-app configuration</td>  </tr>  <tr><td><viewfile-link file="index.jsp"/>    </td><td>A page representative of any page in the application.</td>  </tr>  <tr><td><viewfile-link file="unavailable.jsp"/>    </td><td>A page to show when the web application is unavailable because         the task is active.</td>  </tr></deftable></s1><s1 title="bean Configuration"><p>The <a config-tag="bean"/> tag is used to configure an instance ofPeriodicTask, and store it with the Resin-IoC/WebBeans registry.</p><p>PeriodicTask is implemented as a regular Java object that follows theJavaBeans convention for setters and getters. The configuration of the object  occurs in the web.xml, using Resin's <a href="doc|resin-ioc.xtp">Bean-styleinitialization</a>.  </p><example title="WEB-INF/resin-web.xml" language="java">&lt;web-app xmlns="http://caucho.com/ns/resin">  &lt;bean class="example.PeriodicTask">    &lt;init&gt;      &lt;estimated-average-time&gt;5&lt;/estimated-average-time&gt;    &lt;/init&gt;  &lt;/bean&gt;&lt;/web-app></example><p>If only this step were performed, the object would sit in the JVM memory,performing no function.  The rest of this tutorial details various ways thatthe PeriodicTask can be utilized.  </p></s1><s1 title="Using PeriodicTask for real work"><p>This tutorial performs no real work in it's task, it sleeps for 10 seconds toimitate a task that takes ten seconds.  The code can be used withoutmodification to perform real tasks for your web application.  All of thestatistics gathering and other functionality is available for the derivedclass.</p><s2 title="Extend PeriodicTask"><p>The following example extends PeriodicTask to create a task thatvacuum's a postgress database.</p><example title="WEB-INF/classes/example/VacuumTask.java">package example;import java.util.logging.Level;import java.util.logging.Logger;import java.sql.*;import javax.sql.*;public class VacuumTask extends PeriodicTask {  static protected final Logger log =     Logger.getLogger(VacuumTask.class.getName());  private DataSource _dataSource;  public void setDataSource(DataSource dataSource)  {     _dataSource = dataSource;  }  public void init()    throws Exception  {    if (_dataSource == null)      throw new Exception("'data-source' is required.");  }  protected void performTask()    throws Exception  {    Connection conn = null;    try {      conn = _dataSource.getConnection();      Statement stmt = conn.createStatement();      stmt.executeUpdate("VACUUM FULL ANALYZE;");      stmt.close();    } finally {      try {        if (conn != null)          conn.close();      } catch (SQLException ex) {        log.warning(ex.toString());      }    }  }}</example><p>The VacuumTask is configured to run automatically every night at midnight.Since the database is integral to the application, the PeriodicTaskFilter isused to redirect users to an "unavailable" page while the task active.The PeriodicTaskServlet is used to provide an administration interface to thetask at the url <code>/admin/example/vacuum</code>.  </p><p>A <a config-tag="security-constraint"/> is used to limit access to the<code>/admin/</code> portion of the website.  An <a config-tag="authenticator"/> is used to specify the user names and passwords.</p><example title="WEB-INF/web.xml">&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;  &lt;bean class="example.VacuumTask">    &lt;init&gt;      &lt;estimated-average-time&gt;30&lt;/estimated-average-time&gt;    &lt;/init&gt;  &lt;/bean&gt;  &lt;servlet-mapping url-pattern='/admin/example/vacuum'&gt;    &lt;servlet-class&gt;example.PeriodicTaskServlet&lt;/servlet-class&gt;    &lt;init&gt;      &lt;periodic-task&gt;${vacuumTask}&lt;/periodic-task&gt;    &lt;/init&gt;  &lt;/servlet&gt;  &lt;bean class="com.caucho.resources.CronResource"&gt;    &lt;init&gt;      &lt;!-- run every day at 0215 local time --&gt;      &lt;cron&gt;15 2 *&lt;/cron&gt;      &lt;work&gt;${vacuumTask}&lt;/work&gt;    &lt;/init&gt;  &lt;/bean&gt;  &lt;filter url-regexp="^(?!/admin)+"&gt;    &lt;filter-class&gt;example.PeriodicTaskFilter&lt;/filter-class&gt;    &lt;init&gt;      &lt;periodic-task&gt;${VacuumTask}&lt;/periodic-task&gt;      &lt;url&gt;/unavailable.jsp&lt;/url&gt;    &lt;/init&gt;  &lt;/filter&gt;  &lt;!-- require 'admin' role --&gt;  &lt;security-constraint&gt;    &lt;web-resource-collection&gt;      &lt;web-resource-name&gt;Admin&lt;/web-resource-name&gt;      &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt;    &lt;/web-resource-collection&gt;    &lt;auth-constraint&gt;      &lt;role-name&gt;admin&lt;/role-name&gt;    &lt;/auth-constraint&gt;  &lt;/security-constraint&gt; &lt;authenticator uri="xml:"&gt;    &lt;init&gt;      &lt;!-- user 'admin' with password 'excellent' is in admin role --&gt;      &lt;user&gt;admin:FKT/gZPYJ5TIA5uA434mgA==:admin&lt;/user&gt;    &lt;/init&gt;  &lt;/authenticator&gt;&lt;/web-app&gt;</example></s2></s1><s1 title="Jmx: a management interface for the resource"><p><a href="doc|jmx.xtp">Jmx</a> provides a mechanism for management of objectson servers.  The PeriodicTask implements the PeriodicTaskMBean interface, the PeriodicTaskMBean interface is the Jmx agent's view of the resource.</p><p>Jmx uses a naming scheme to store objects for later retrieval.  The Jmx nameto store the object under is specified with the <a config-tag="mbean-name"/>child of <a config-tag="bean"/>.  </p><example title="Example: WEB-INF/resin-web.xml" language="java">&lt;web-app xmlns="http://caucho.com/ns/resin">  &lt;bean class="example.PeriodicTask" name="PeriodicTask"            mbean-interface="example.PeriodicTaskMBean"            <b>mbean-name="type=PeriodicTask,name=PeriodicTask"&gt;</b>    &lt;init&gt;      &lt;estimated-average-time&gt;5&lt;/estimated-average-time&gt;    &lt;/init&gt;  &lt;/bean&gt;&lt;/web-app></example><p>A simple Jmx lookup of all <code>type=PeriodicTask</code> objects isdone in admin/mbean.jmx: </p><example title="Example: admin/mbean.jsp">  ObjectName query = new ObjectName("resin:type=PeriodicTask,*");  pageContext.setAttribute("mbeans", Jmx.query(query));  ...&lt;c:forEach var="mbean" items="${mbeans}"&gt;

⌨️ 快捷键说明

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