Changeset 6665


Ignore:
Timestamp:
Dec 17, 2014, 8:40:46 AM (8 years ago)
Author:
Nicklas Nordborg
Message:

References #1900 and #1901.

This should hopefully fix the ID issue for the job queue.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.4-stable/src/core/net/sf/basedb/core/signal/AbstractSignalReceiver.java

    r6444 r6665  
    2828import java.util.HashMap;
    2929import java.util.Map;
     30import java.util.WeakHashMap;
     31
     32import net.sf.basedb.core.Application;
    3033
    3134/**
     
    7679  */
    7780  private Map<String, SignalHandler> handlers;
     81  private Map<SignalHandler, String> handlerIds;
    7882  private String receiverId;
    7983  private Thread notifyThread;
     
    98102  {
    99103    handlers = Collections.synchronizedMap(new HashMap<String, SignalHandler>());
     104    handlerIds = new WeakHashMap<SignalHandler, String>();
    100105    this.receiverId = receiverId;
    101106    logger.info("Initializing signal receiver: id=" + receiverId);
     
    165170  {
    166171    if (notifyThread != null) throw new SignalException("Signal receiver is shutting down.");
     172    String localId = getLocalSignalHandlerId(handler);
    167173    String globalId = getGlobalSignalId(handler);
    168     String localId = getLocalSignalHandlerId(handler);
    169174    if (handlers != null)
    170175    {
    171       if (handlers.containsKey(localId))
    172       {
    173         throw new SignalException("A handler with ID '" + localId + "' is already registered: "
    174           + handlers.get(localId));
    175       }
    176       handlers.put(localId, handler);
     176      synchronized (handlers)
     177      {
     178        if (handlers.containsKey(localId))
     179        {
     180          throw new SignalException("A handler with ID '" + localId + "' is already registered: "
     181            + handlers.get(localId));
     182        }
     183        handlers.put(localId, handler);
     184      }
    177185    }
    178186
     
    180188      "; global id=" + globalId + "; local id=" + localId);
    181189    logger.debug("Current number of registered signal handlers: " +
    182       (handlers == null ? 0 : handlers.size()));
     190      (handlers == null ? "0/0" : handlers.size()+"/"+handlerIds.size()));
    183191    return globalId;
    184192  }
     
    196204    if (handlers != null)
    197205    {
    198       handlers.remove(localId);
     206      synchronized (handlers)
     207      {
     208        handlers.remove(localId);
     209        handlerIds.remove(handler);
     210      }
    199211      // If called from the close() method() this is waiting for
    200212      if (notifyThread != null && handlers.size() == 0)
     
    205217    }
    206218    logger.debug("Current number of registered signal handlers: " +
    207       (handlers == null ? 0 : handlers.size()));
     219      (handlers == null ? "0/0" : handlers.size()+"/"+handlerIds.size()));
    208220  }
    209221 
     
    274286  /**
    275287    Get the local signal handler id of the given signal handler.
    276     This implementation simply return the system hashcode for the
    277     handler. The returned ID must be unique among the registered
    278     signal handlers.
     288    This implementation generates a unique ID for the handler.
    279289    @param handler The handler to get the id for
    280290    @return The local handler id
     
    282292  protected String getLocalSignalHandlerId(SignalHandler handler)
    283293  {
    284     return String.valueOf(System.identityHashCode(handler));
     294    if (handlerIds == null) return String.valueOf(System.identityHashCode(handler));
     295    String id = handlerIds.get(handler);
     296    if (id == null)
     297    {
     298      synchronized (handlers)
     299      {
     300        while (id == null)
     301        {
     302          id = Application.generateRandomId(4);
     303          if (handlers.containsKey(id)) id = null;
     304        }
     305        handlerIds.put(handler, id);
     306      }
     307    }
     308    return id;
    285309  }
    286310 
Note: See TracChangeset for help on using the changeset viewer.