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

📄 index.xtp

📁 RESIN 3.2 最新源码
💻 XTP
字号:
<document><header><product>resin</product><type>tutorial</type><title>Resource Thread Management</title><description><p>Resources using threads will want to use JCA's work management API.The work management API lets Resin manage threads for theresource rather than forcing the resource to manage its own threads.Since Resin is in a better position to manager threads, using theWork API is not only a convenience, but is a cleaner and more reliableimplementation.</p></description> <tutorial-startpage>index.jsp</tutorial-startpage></header><body><summary/><s1 title="Files in this tutorial"><deftable><tr><td><viewfile-link file="WEB-INF/web.xml"/>    </td><td>Configures the WorkResource</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/WorkResource.java"/>    </td><td>The resource implementation launches the Work task and provides common state.</td></tr><tr><td><viewfile-link file="WEB-INF/classes/example/WorkTask.java"/>    </td><td>The work task sleeps and counts.</td></tr><tr><td><viewfile-link file="index.jsp"/>    </td><td>The starting page for the tutorial</td></tr></deftable></s1><s1 title="The Work task"><p>A <code>Work</code> task implements the<code>javax.resource.work.Work</code> interface.  Like the familiar<code>Runnable</code> interface, <code>Work</code> has apublic <code>run()</code> method as the main thread method.<code>Work</code> adds a <code>release()</code> method which allowsResin to notify the task when the resource should shut down.</p><p>The example task increments a counter and sleeps.The <code>run</code> method has the main loop.</p><p>The only complexity in the task implements the <code>release()</code>capability.  The <code>_isActive</code> flag indicates whether theserver is still alive.  It is important that <code>_isActive</code> isset before the work task is submitted to avoid timing issues.  Inother words, it would be a mistake to set <code>_isActive = true</code>in the <code>run()</code> method since Resin could call<code>release()</code> before the run thread started.</p><p>The task uses <code>wait(long)</code> for sleeping.The <code>release()</code> will wake the task to let it shut down.In a work task, the main blocking calls should have timeouts orwill be interruptable.  A real application like a chat server might bereading a socket or waiting for a new connection.  Those calls needtimeouts so the task can shut down the service at the appropriate time.</p><example title="WorkTask.java">import javax.resource.Work;public class WorkTask implements Work {  private volatile boolean _isActive = true;  private int _count;  int getCount()  {    return _count;  }  public void run()  {    while (_isActive) {      _count++;      try {        synchronized (this) {          wait(10000);        }      } catch (Throwable e) {      }    }  }  public void release()  {    _isActive = false;    try {      synchronized (this) {        notifyAll();      }    } catch (Throwable e) {    }  }</example></s1><s1 title="The Resource"><p>The <code>WorkResource</code> in this example is simple since it onlyneeds to launch the work task.  As with the previous examples, theresource extends <code>com.caucho.jca.AbstractResourceAdapter</code>to simplify the example.</p><example title="WorkResource.java">import com.caucho.jca.AbstractResourceAdapter;public class WorkResource extends AbstractResourceAdapter {  public void start(BootstrapContext ctx)    throws ResourceAdapterInternalException  {    WorkTask work = new WorkTask();    WorkManager workManager = ctx.getWorkManager();        try {      // Submits the work, but does not wait for the result.      // In other words, it spawns a new thread      workManager.startWork(work);    } catch (WorkException e) {      throw new ResourceAdapterInternalException(e);    }  }  public String toString()  {    return "WorkResource[" + _workTask.getCount() + "]";  }}</example></s1></body></document>

⌨️ 快捷键说明

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