geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject cvs commit: incubator-geronimo/modules/remoting/src/java/org/apache/geronimo/remoting/transport/async Correlator.java
Date Thu, 05 Feb 2004 03:24:26 GMT
adc         2004/02/04 19:24:26

  Modified:    modules/remoting/src/java/org/apache/geronimo/remoting/transport/async
                        Correlator.java
  Log:
  Removed strong references to keys
  
  Revision  Changes    Path
  1.2       +40 -12    incubator-geronimo/modules/remoting/src/java/org/apache/geronimo/remoting/transport/async/Correlator.java
  
  Index: Correlator.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/remoting/src/java/org/apache/geronimo/remoting/transport/async/Correlator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Correlator.java	16 Nov 2003 05:27:33 -0000	1.1
  +++ Correlator.java	5 Feb 2004 03:24:26 -0000	1.2
  @@ -56,22 +56,26 @@
    */
   package org.apache.geronimo.remoting.transport.async;
   
  +import java.lang.ref.ReferenceQueue;
  +import java.lang.ref.WeakReference;
  +import java.util.HashMap;
   import java.util.Map;
  -import java.util.WeakHashMap;
  +
  +import EDU.oswego.cs.dl.util.concurrent.Slot;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  -import EDU.oswego.cs.dl.util.concurrent.Slot;
   
   /**
    * Allows you to create a request to which
  - * you can at a later time wait for the response to 
  + * you can at a later time wait for the response to
    * arrive asynchrously.
  - * 
  + *
    * @version $Revision$ $Date$
    */
   public class Correlator {
  +    private final ReferenceQueue queue = new ReferenceQueue();
   
       static public class FutureResult extends Slot {
           private final HashKey key;
  @@ -89,8 +93,8 @@
   
       /**
        * Since a incrementing int is used as a key to a HashMap,
  -     * this class is used to calculate a hashCode that is more 
  -     * spred to get better hashing. 
  +     * this class is used to calculate a hashCode that is more
  +     * spred to get better hashing.
        */
       private static class HashKey {
           final int value;
  @@ -104,20 +108,31 @@
               rc *= Integer.MAX_VALUE / 7;
               hashCode = (int) (rc % Integer.MAX_VALUE);
           }
  +
           /**
            * Not a very proper equals since it takes
            * shortcuts, but since this class is not used
  -         * in a general case, it's ok. 
  +         * in a general case, it's ok.
            */
           public boolean equals(Object obj) {
               return ((HashKey) obj).value == value;
           }
  +
           public int hashCode() {
               return hashCode;
           }
       }
   
  -    private Map slots = new WeakHashMap(100);
  +    private class SlotReference extends WeakReference {
  +        private final Object key;
  +
  +        public SlotReference(Object key, Object lock) {
  +            super(lock, queue);
  +            this.key = key;
  +        }
  +    }
  +
  +    private Map slots = new HashMap(100);
       private int nextFutureResultID = 0;
       private Object nextFutureResultIDLock = new Object();
   
  @@ -131,7 +146,7 @@
           HashKey key = new HashKey(getNextFutureResultID());
           FutureResult s = new FutureResult(key);
           synchronized (slots) {
  -            slots.put(key, s);
  +            slots.put(key, new SlotReference(key, s));
           }
           if (log.isTraceEnabled())
               log.trace("Created Request: " + key.value);
  @@ -142,9 +157,13 @@
           if (log.isTraceEnabled())
               log.trace("Received resposne for request: " + id);
   
  -        FutureResult s;
  +        FutureResult s = null;
           synchronized (slots) {
  -            s = (FutureResult) slots.get(new HashKey(id));
  +            processQueue();
  +            SlotReference ref = (SlotReference) slots.get(new HashKey(id));
  +            if (ref != null) {
  +                s = (FutureResult) ref.get();
  +            }
           }
           if (s != null) {
               try {
  @@ -156,5 +175,14 @@
               log.trace("The request may have timed out.  Request slot was not found");
           }
   
  +    }
  +
  +    private void processQueue() {
  +        SlotReference slotRef;
  +        while ((slotRef = (SlotReference) queue.poll()) != null) {
  +            synchronized (slots) {
  +                slots.remove(slotRef.key);
  +            }
  +        }
       }
   }
  
  
  

Mime
View raw message