📄 index.xtp
字号:
estimatedAverageTime ${mbean.estimatedAverageTime} ...</example></s1><s1 title="CronResource: timed execution of the PeriodicTask"><p>Depending on the nature of the PeriodicTask, it may be appropriate to configurethe task to automatically run at a specified time, or at a specified interval.</p><p>The <a href="doc|resources.xtp">CronResource</a> is used to configurethe timed execution of the PeriodicTask.</p><example title="WEB-INF/web.xml" language="java"> <bean class="com.caucho.resources.CronResource"> <init> <cron>*</cron> <work>${PeriodicTask}</work> </init> </bean></example></s1><s1 title="Filter: redirecting users when the task is running"><p>Again depending on the nature of the PeriodicTask, it may be appropriate foraccess to the web application to be limited while the task is performed. Afilter is used to provide this functionality, the filter intercepts requests and if the task is active, redirects to a page giving a "temporarilyunavailable" message.</p><example title="WEB-INF/web.xml" language="java"> <filter> <filter-name>PeriodicTaskFilter</filter-name> <filter-class>example.PeriodicTaskFilter</filter-class> <init> <periodic-task>${PeriodicTask}</periodic-task> <!-- optional url, if not specified a 503 response is sent. --> <url>/unavailable.jsp</url> </init> </filter> <filter-mapping> <!-- regexp to match all urls except /admin and /index.xtp--> <url-regexp>^(?!/admin|/index.xtp)+</url-regexp> <filter-name>PeriodicTaskFilter</filter-name> </filter-mapping></example><example title="WEB-INF/classes/example/PeriodicTaskFilter.java" language="java"> public void setPeriodicTask(PeriodicTask periodicTask) { _periodicTask = periodicTask; }</example><example title="WEB-INF/classes/example/PeriodicTaskFilter.java" language="java"> public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { if (_periodicTask.isActive()) { dispatch( (HttpServletRequest) request, (HttpServletResponse) response); } else { chain.doFilter(request,response); } }</example></s1><s1 title="PeriodicTaskServlet: an administration interface"><p>The PeriodicTaskServlet provides an html interface to the PeriodicTask. Itshows the current status of the task, and provides a button to "Run" the task.</p><p>The PeriodicTaskServlet needs an instance of PeriodicTask to operate on. The Servlet provides a setter:</p><example title="WEB-INF/classes/example/PeriodicTaskServlet.java" language="java"> public void setPeriodicTask(PeriodicTask periodicTask) { _periodicTask = periodicTask; }</example><p>The configuration of the servlet in web.xml uses the <code>${...}</code>function to get a reference to the PeriodicTask resource previously stored inwith the <a config-tag="bean"/> configuration. </p><example title="WEB-INF/web.xml" language="java"> <servlet> <servlet-name>PeriodicTaskServlet</servlet-name> <servlet-class>example.PeriodicTaskServlet</servlet-class> <init> <periodic-task>${PeriodicTask}</periodic-task> </init> </servlet> <servlet-mapping> <url-pattern>/admin/periodictask</url-pattern> <servlet-name>PeriodicTaskServlet</servlet-name> </servlet-mapping></example> </s1><s1 name="dependency-injection" title="Dependency Injection/Inversion of Control"><p><i>Dependency injection</i> is a term used to describe a separation between theimplementation of an object and the construction of an object it depends on, andthe ability for a container (like Resin) to resolve the dependency.</p> <p>In this tutorial, many components including the administration servlet, thefilter, and the CronResource, depend upon a <code>PeriodicTask</code>.Each of these components that depend upon the PeriodidicTask provide a setter:</p><example title="setter"> PeriodicTask _periodTask; public void setPeriodicTask(PeriodicTask periodicTask) { _periodicTask = periodicTask; }</example><p>The container (Resin), injects the object.</p><example title="container injection"> <init> <periodic-task>${PeriodicTask}</periodic-task> </init></example><p>The simplicity of the code shows immediate benefits. There is no dependencyon the environment (needing an <i>application</i> object for example), and noneed for cumbersome or error prone code in each component.</p><p>There are other benefits as well. Since the container instantiates and setsthe object, there is more flexibility in the configuration. The followingexample shows the use of two distinct periodic tasks.</p><p>An example illustrates some of the flexibility of dependency injection.</p><p>The first task (Foo) is only run manually, it is not run at a timed interval sothe CronResource is not used for it. Neither task causes the application tobecome unavailable, so the PeriodicTaskFilter is not used. </p><example title="two distinct periodic tasks"><b>WEB-INF/classes/example/PeriodicTaskFoo.java</b>public class PeriodicTaskFoo extends PeriodicTask { protected void performTask() throws Exception { ... }}<b>WEB-INF/classes/example/PeriodicTaskBar.java</b>public class PeriodicTaskBar extends PeriodicTask { protected void performTask() throws Exception { ... }}<b>WEB-INF/web.xml</b> <!-- TASK FOO --> <bean class="example.PeriodicTaskFoo" name="PeriodicTaskFoo"> <init> <estimated-average-time>15</estimated-average-time> </init> </bean> <servlet> <servlet-name>PeriodicTaskFooServlet</servlet-name> <servlet-class>example.PeriodicTaskServlet</servlet-class> <init> <periodic-task>${PeriodicTaskFoo}</periodic-task> </init> </servlet> <servlet-mapping> <url-pattern>/admin/example/foo</url-pattern> <servlet-name>PeriodicTaskFooServlet</servlet-name> </servlet-mapping> <!-- TASK BAR --> <bean class="example.PeriodicTaskBar" name="PeriodicTaskBar"> <init> <estimated-average-time>15</estimated-average-time> </init> </bean> <bean class="example.PeriodicTaskBar" name="PeriodicTaskBar"> <init> <estimated-average-time>15</estimated-average-time> </init> </bean> <servlet> <servlet-name>PeriodicTaskBarServlet</servlet-name> <servlet-class>example.PeriodicTaskServlet</servlet-class> <init> <periodic-task>${PeriodicTaskBar}</periodic-task> </init> </servlet> <servlet-mapping> <url-pattern>/admin/example/bar</url-pattern> <servlet-name>PeriodicTaskBarServlet</servlet-name> </servlet-mapping> <!-- bar runs every minute --> <bean class="com.caucho.resources.CronResource"> <init> <cron>*</cron> <work>${PeriodicTaskBar}</work> </init> </bean></example></s1> <!-- dependency-injection --><s1 title="See Also"><dl><dt><a href="doc|resources.xtp">CronResource</a></dt><dd>Documentation for the usage of CronResource</dd></dl></s1></body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -