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

📄 poolmanager.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   /**   * Release a resource explicitly from 'busy' into the 'idle' pool.   * @param instanceId The unique resource ID   * @exception XmlBlasterException   */   public void release(String instanceId) throws XmlBlasterException {      synchronized (meetingPoint) {         swap(findLow(instanceId), false);      }      //System.out.println(ME + ": Resource <" + instanceId + "> released explicitly");      // dumpState(instanceId);   }   /**   *  Generate a unique resource ID <br>   *   *  @instanceId USE_HASH_CODE=null   ResourceWrapper generates a simple ID = resource.hashCode()<br />   *              "..."                Your supplied ID is used<br />   *              GENERATE_RANDOM=""   A random ID is generated, unique in universe and over time   *  @return unique ID or null   *  @exception XmlBlasterException random generator   */   private String createId(String instanceId) throws XmlBlasterException {      if (instanceId == null || instanceId.equals(GENERATE_RANDOM) == false)         return instanceId; // ResourceWrapper generates a simple id      try {         String ip;         try {            java.net.InetAddress addr = java.net.InetAddress.getLocalHost();            ip = addr.getHostAddress();         }         catch (Exception e) {            // System.err.println(ME + ": Can't determin your IP address");            ip = "localhost";         }         // This is a real random, but probably not necessary here:         // Random random = new java.security.SecureRandom();         java.util.Random ran = new java.util.Random(); // is more or less currentTimeMillis         // Note: We should include the process ID from this JVM on this host to be granted unique         //   <IP-Address>-<InstanceName>-<TimestampMilliSec>-<RandomNumber>-<LocalCounter>         return ip + "-" + poolName + "-" + System.currentTimeMillis() + "-" + ran.nextInt() + "-" + (counter++);      }      catch (Exception e) {         String text = "Can't generate a unique instanceId: " + e.toString();         throw new XmlBlasterException("ResourceNoId", text);      }   }   /**   * Idle - busy swapper.   */   private void swap(ResourceWrapper rw, boolean toBusy) {      if (rw == null) {         System.err.println("##########INTERNAL PROBLEM IN PollManager");         return;      }      if (toBusy) {         idle.removeElementAt(idle.size() - 1);         busy.put(rw.getInstanceId(), rw);         rw.toBusy();         callback.idleToBusy(rw.getResource());         //System.err.println("Swapped to busy:" + rw.getInstanceId());      }      else { // recycling ...         busy.remove(rw.getInstanceId());         idle.addElement(rw);         rw.toIdle();         callback.busyToIdle(rw.getResource());         //System.err.println("Swapped to idle:" + rw.getInstanceId());      }   }   /**   * Find a resource in busy list.   * @param instanceId The unique resource ID   * @return The handle containing the resource.   */   private ResourceWrapper findBusySilent(String instanceId) {      if (instanceId == null)         return null;      return (ResourceWrapper) busy.get(instanceId);   }   /**   * Find a resource in idle list.   * <p />   * Note that this is currently a linear search (not high performing).   * @param instanceId The unique resource ID   * @return The handle containing the resource.   */   private ResourceWrapper findIdleSilent(String instanceId) {      if (instanceId == null)         return null;      for (int ii = 0; ii < idle.size(); ii++) {         ResourceWrapper rw = (ResourceWrapper) idle.elementAt(ii);         if (rw.getInstanceId().equals(instanceId))            return rw;      }      return null;   }   /**   * Find a resource in busy list.   * @param instanceId The unique resource ID   * @return Handle containing resource   * @exception XmlBlasterException "ResourceNotFound"   */   private ResourceWrapper findLow(String instanceId) throws XmlBlasterException {      if (instanceId == null) {         String text = "Your resource ID is null";         throw new XmlBlasterException("ResourceNotFound", text);      }      ResourceWrapper rw = findBusySilent(instanceId);      if (rw == null) {         String text = "Resource '" + instanceId + "' is invalid, timed out?";         throw new XmlBlasterException("ResourceNotFound", text);      }      return rw;   }   /**   * Test if the resource is busy.   *   * @param instanceId The unique resource ID   * @return true - is in 'busy' state<br />   *         false - is in 'idle' state or unknown   */   public boolean isBusy(String instanceId) {      ResourceWrapper rw = findBusySilent(instanceId);      return (rw != null);   }   /**   * Restart countdown for resource life cycle.   * <p />   * Rewind the timeout for 'busy' to 'idle' transition.   * @param instanceId The unique resource ID   * @exception XmlBlasterException ResourceNotFound   */   public void busyRefresh(String instanceId) throws XmlBlasterException {      synchronized (meetingPoint) {         ResourceWrapper rw = findLow(instanceId);         rw.touchBusy();      }   }   /**   * Number of resources in the 'busy' list.   * @return Number of 'busy' resources   */   public int getNumBusy() {      return busy.size();   }   /**   * Number of resources in the 'idle' list.   * @return Number of 'idle' resources   */   public int getNumIdle() {      return idle.size();   }   /**   * Cleanup.   */   protected void finalize() throws Throwable {      destroy();   }   /**   * Dump the current state of this pool.   */   public String getState() {      return "Overview for '" + ME + "':" + " BUSY=" + busy.size() + " IDLE=" + idle.size() + " MAX_AVAILABLE=" + (maxInstances - busy.size());   }   /**   * Dump state of this object into a XML ASCII string.   * <p />   * @return internal state of this PoolManager as a XML ASCII string   */   public final String toXml() {      return toXml((String) null);   }   /**   * Dump state of this object into a XML ASCII string.   * <p />   * @param extraOffset indenting of tags for nice output   * @return internal state of this PoolManager as a XML ASCII string   */   public final String toXml(String extraOffset) {      StringBuffer buf = new StringBuffer();      String offset = "\n";      if (extraOffset == null)         extraOffset = "";      offset += extraOffset;      synchronized (meetingPoint) {         buf.append(offset).append("<").append(ME).append(" maxInstances='").append(maxInstances);         buf.append("' busyToIdleTimeout='").append(busyToIdleTimeout);         buf.append("' idleToEraseTimeout='").append(idleToEraseTimeout).append("'>");         buf.append(offset).append("   <busy num='").append(busy.size()).append("'>");         for (Enumeration e = busy.elements(); e.hasMoreElements();) {            ResourceWrapper rw = (ResourceWrapper) e.nextElement();            buf.append(rw.toXml(extraOffset + "   "));         }         buf.append(offset).append("   </busy>");         buf.append(offset).append("   <idle num='").append(idle.size()).append("'>");         for (int ii = 0; ii < idle.size(); ii++) {            ResourceWrapper rw = (ResourceWrapper) idle.elementAt(ii);            buf.append(rw.toXml(extraOffset + "   "));         }         buf.append(offset).append("   </idle>");         buf.append(offset).append("</" + ME + ">");      }      return buf.toString();   }   /**   * Recycle busy resource after timeout.   * <p />   * This method is a callback from ResourceWrapper   * @parameter userData The ResourceWrapper object   */   void timeoutBusyToIdle(ResourceWrapper rw) {      // System.out.println(ME + ": Resource '" + rw.getInstanceId() + "' is recycled from 'busy' state, was not in use since " + TimeHelper.millisToNice(rw.busyElapsed()));      synchronized (meetingPoint) {         swap(rw, false);      }      // dumpState(rw.getInstanceId());   }   /**   * Erase an idle resource after timeout.   * <p />   * This method is a callback from ResourceWrapper   * @parameter userData The ResourceWrapper object   */   void timeoutIdleToErase(ResourceWrapper rw) {      // System.out.println(ME + ": Resource '" + rw.getInstanceId() + "' is erased from 'idle' state after timeout");      erase(rw);   }   /**   * Explicitly remove a resource.   * <p />   * It is erased if it is found in the idle list or even when it is busy.   * @param instanceId The unique resource ID   */   public void erase(String instanceId) {      if (instanceId == null || instanceId.length() < 1)         return;      synchronized (meetingPoint) {         ResourceWrapper rw = findBusySilent(instanceId);         if (rw == null) {            rw = findIdleSilent(instanceId);         }         erase(rw);      }   }   /**   * Remove a resource.   * <p />   * Remove it from the 'idle' or the 'busy' list.   * @param rw The resource wrapper object   */   private void erase(ResourceWrapper rw) {      if (rw == null)         return;      synchronized (meetingPoint) {         if (busy.get(rw.getInstanceId()) != null) {            swap(rw, false); // move to idle ... calls busyToIdle() callback            //busy.remove(rw.getInstanceId());         }         idle.remove(rw);         callback.toErased(rw.getResource());         rw.destroy();      }      // dumpState(rw.getInstanceId());   }   /**   * Cleanup everything.   */   public void destroy() {      // System.out.println(ME + ": Freeing all resources, " + busy.size() + " are busy and " + idle.size() + " idle.");      synchronized (meetingPoint) {         java.util.Enumeration e = busy.elements();         for (; e.hasMoreElements();) {            erase((ResourceWrapper) e.nextElement());         }         busy.clear();         for (int ii = 0; ii < idle.size(); ii++) {            ResourceWrapper rw = (ResourceWrapper) idle.elementAt(ii);            callback.toErased(rw.getResource());            rw.destroy();         }         idle.removeAllElements();      }   }}

⌨️ 快捷键说明

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